formatting output with write
TRANSCRIPT
-
8/3/2019 Formatting Output With WRITE
1/73
ITB258 - ABAP Programming
Lecture Week 5
Formatting Output WithWRITE
Program Modularisation
-
8/3/2019 Formatting Output With WRITE
2/73
Objectives
This lecture aims to introduce thereader to
the ABAP WRITE Statement methods for formatting output
positioning output on the screen
formatting options
lines and blank lines on the screen techniques for program modularisation
creating Forms & Functions
passing parameters to forms and functions
-
8/3/2019 Formatting Output With WRITE
3/73
Basic Form of WRITE
WRITE f.
outputs content of field f to the currentlist in its default output format
basic ABAP statement for outputtingdata
additions to the basic WRITE statementgive control over screen position, field length, decimals,
exponent, format mask, colour
-
8/3/2019 Formatting Output With WRITE
4/73
Default Outputs example
report zadamswk501 .
data: char(10) type c value 'ABCDE',
numc(10) type n value '12345',
integer type i value 12345,
packed type p value 12345,float type f value 12345,
time type t value '085922',
date type d value '20010326',
hex type x value 'ABCD'.
write: / ' ----+----1----+----2----+',/ ' 1234567890123456789012345',
/ 'char: ', char, /, / 'numc: ', numc, /,
/ 'int: ', integer, /, / 'pack: ', packed, /,
/ 'float:', float, /, / 'time: ', time, /,
/ 'date: ', date, /, / 'hex: ', hex.
-
8/3/2019 Formatting Output With WRITE
5/73
-
8/3/2019 Formatting Output With WRITE
6/73
Positioning Output
WRITE [AT] [/] [pos] [(len)] f. / denotes new line
pos horizontal position
always output at that position regardless of whetherthere is enough space or other fields overwritten
(len)
output length
iflen is too short
numeric fields truncated (left), asterisk displayed
all others truncated (right) with no indication
-
8/3/2019 Formatting Output With WRITE
7/73
data: word(16) value '0123456789ABCDEF',
col type i value 8,
len type i value 5.
write /5(12) word.
write at col(len) word.
write /(3) 12345.
write: /5(8) word, 25(2) word.
Some Examples ofpos
(len)
0123456789AB0120123489AB0120123489AB0120123489AB
*45
01234567 01
-
8/3/2019 Formatting Output With WRITE
8/73
Formatting Options - all data
typesWRITE f option1 option2 ....
LEFT-JUSTIFIED Output is left justified
RIGHT-JUSTIFIED Output is right justified CENTERED Output is centered
UNDER g Output starts under field g
NO-GAP Blank after field fis omitted
USING EDIT MASKm Specifies a format template m
USING NO EDIT MASK Deactivates a template
NO-ZERO All zero field replaced byblanks
COLOR c Color of output specified by c
-
8/3/2019 Formatting Output With WRITE
9/73
Formatting Options -
examplesDATA: word(10) value 'text',
num(10) type n value 12345.
WRITE: / 'Left Justified :', word LEFT-JUSTIFIED,
/ 'Right Justified :', word RIGHT-JUSTIFIED,
/ 'Centered :', word CENTERED,
/ 'one', 'two', 'three', 'four',
/ word UNDER 'three',
/ word USING EDIT MASK '_:_%_8_!',/ 'one', 'two' NO-GAP, 'three', 'four',
/ num,
/ num NO-ZERO.
-
8/3/2019 Formatting Output With WRITE
10/73
Formatting Options - numeric
fields only NO-SIGN Leading sign is not output
DECIMALS d Output has d digits after decimal
EXPONENT e Type f fields exponent defined in e
ROUND r Type p fields are divided by 10**(r)and then rounded
CURRENCY cAccording to definition ofc in tableTCURX
UNIT u Number of decimal places is fixed
according to the definition ofu in
table T006 for type p fields
F tti O ti
-
8/3/2019 Formatting Output With WRITE
11/73
Formatting Options -examples
data: packed type p value '-12345.678',
float type f value '-12345.678'.
write: / 'Packed', 40 'Float',
/ packed, 40 float, 70 'default',
/ packed NO-SIGN, 40 float NO-SIGN, 70 'no-sign',/ packed DECIMALS 4, 40 float DECIMALS 4, 70 'decimals 4',
/ packed DECIMALS 2, 40 float DECIMALS 2, 70 'decimals 2',
/ packed DECIMALS 0, 40 float DECIMALS 0, 70 'decimals 0',
/ packed ROUND 2, 40 float ROUND 2, 70 'round 2',
/ packed ROUND 0, 40 float ROUND 0, 70 'round 0',
/ packed EXPONENT 2, 40 float EXPONENT 2, 70 'exponent 2',
/ packed EXPONENT 0, 40 float EXPONENT 0, 70 'exponent 0',
/ packed CURRENCY 'USD', 40 float CURRENCY 'USD', 70 'currency',
/ packed UNIT 'KM', 40 float UNIT 'KM', 70 'unit' .
-
8/3/2019 Formatting Output With WRITE
12/73
-
8/3/2019 Formatting Output With WRITE
13/73
Formatting Options - date
fieldsWRITE f [date formatstring]
data: today type d value '20040402'.
write: / today,
/(10) today,
/ today DD/MM/YY,
/ today MM/DD/YY,
/ today DD/MM/YYYY,/ today MM/DD/YYYY,
/ today MMDDYY,
/ today DDMMYY,
/ today YYMMDD,
/(10) today using edit mask '__/__/____'.
-
8/3/2019 Formatting Output With WRITE
14/73
System User Profile Own Data -Defaults
-
8/3/2019 Formatting Output With WRITE
15/73
Formatting Options:
Colours
COLOR sets the background colour of a list line.
The colour can be specified as a pre-definedconstant:
WRITE var COLOR COL_TOTAL.
or a digit from 0 to 7:
WRITE var COLOR 3.or the value of a data element (using '='):
data row_colour = 3.WRITE var COLOR = row_colour.
WRITE f COLOR [color constant | literal |= var]
-
8/3/2019 Formatting Output With WRITE
16/73
Available Colours
Digit Colour Constant Recommended
for
Actual
Colour0 COL_BACKGROUND Background
(default)Light Grey
1 COL_HEADING Headings Blue
2 COL_NORMAL List Bodies Blue Grey
3 COL_TOTAL Totals Yellow
4 COL_KEY Key Columns Cyan
5 COL_POSITIVE PositiveThresholds
Green
6 COL_NEGATIVE NegativeThresholds
Red
7 COL_GROUP Grouping Orange
-
8/3/2019 Formatting Output With WRITE
17/73
Formatting Options:
ColoursThere are 2 options that affect the way
a colour is displayed:
INTENSIFIED [ON | OFF] ON (default) bright colours
OFF less intense colours (faded)
INVERSE [ON | OFF]
ON
colour used for foreground text on defaultbackground
OFF (default) colour used for background
-
8/3/2019 Formatting Output With WRITE
18/73
Colour Format Example
data: number type i value 0.
while number < 8.
write: /(4) number, 7 sy-vline,
' Intensified ON ' COLOR = number, sy-vline,
' Intensified OFF ' COLOR = number INTENSIFIED OFF,
sy-vline, ' Inverse ' COLOR = number INVERSE.
number = number + 1.
endwhile.
-
8/3/2019 Formatting Output With WRITE
19/73
FORMAT
FORMAT option1 [ON|OFF] option2 [ON|OFF] ...
formatting ON applies to all subsequent output until
turned off using the OFF optionFORMAT option1 = var1 option2 = var2 ...
var interpreted as a number
should be of type i
var= 0 has same effect as OFF option var> 0 has same effect as ON option
with COLOR option acts like corresponding colournumber
-
8/3/2019 Formatting Output With WRITE
20/73
FORMAT formatting options used in a WRITE
statement overwrite the correspondingsettings of a previously issued FORMAT
statement for the current output for each new event the system resets all
formatting options to their default values all options have a default value of OFF except
the INTENSIFIED option
FORMAT RESET sets all formatting options to OFF in one go
-
8/3/2019 Formatting Output With WRITE
21/73
tables sflight.
data sum type i.
top-of-page.FORMAT COLOR COL_HEADING.write: 'DATE', 20 'SEATS OCCUPIED', 50 'SEATS AVAILABLE'.
start-of-selection.select * from sflight where carrid = 'AA'.
if sflight-seatsocc = sflight-seatsmax. "if fully booked
FORMAT COLOR COL_NEGATIVE.else.
FORMAT COLOR COL_NORMAL.endif.write: / sflight-fldate UNDER 'DATE',
sflight-seatsocc UNDER 'SEATS OCCUPIED',
sflight-seatsmax UNDER 'SEATS AVAILABLE'.sum = sum + sflight-seatsocc.endselect.
end-of-selection.uline.write: 'Total Bookings:' INTENSIFIED OFF,
sum UNDER sflight-seatsocc COLOR COL_TOTAL.
-
8/3/2019 Formatting Output With WRITE
22/73
-
8/3/2019 Formatting Output With WRITE
23/73
Writing Symbols and Icons
There are about 200 pre-defined symbolsand icons available for writing to the outputlist.
To write a symbol (small line drawing):
WRITE symbol AS SYMBOLTo write an icon:
WRITE icon AS ICON Must use statement:
include | |
Full list in help for WRITE (F1)
-
8/3/2019 Formatting Output With WRITE
24/73
Symbols and Iconsexample
INCLUDE .
WRITE / 'SYMBOLS' COLOR COL_HEADING.
WRITE: / SYM_PHONE AS SYMBOL,
/ SYM_NOTE AS SYMBOL,
/ SYM_FAX AS SYMBOL.
SKIP.
WRITE / 'ICONS' COLOR COL_HEADING.
WRITE: / ICON_CALCULATION AS ICON,
/ ICON_PRINT AS ICON,/ ICON_RED_LIGHT AS ICON,
/ ICON_GREEN_LIGHT AS ICON,
/ ICON_TIME_ZONE AS ICON,
/ ICON_WS_PLANE AS ICON.
-
8/3/2019 Formatting Output With WRITE
25/73
-
8/3/2019 Formatting Output With WRITE
26/73
Creating Blank Lines
SET BLANK LINES [ON | OFF] OFF
system suppresses blank lines created by
WRITESKIP [n]
ifn greater than lines remaining onpage
produces page footer, throws to new page at the beginning of a new page
ignored except if page created by NEW-PAGE or if page is the first of a list level
If last output statement of last list page
-
8/3/2019 Formatting Output With WRITE
27/73
data blank_line(50).
write: / blank_line.
write sy-uline.
set blank lines on.
write: / blank_line.write sy-uline.
skip 2. write sy-uline.
skip to line 10. write 'line 10'.skip to line 20. write 'line 20'.
skip to line 15. write 'line 15'.
-
8/3/2019 Formatting Output With WRITE
28/73
Page and Line Breaks
NEW-PAGE....NO-TITLE new page without standard list
header
...WITH-TITLE new page with standard list header
...NO-HEADING new page without column headers
...WITH-HEADING new page with column headers
...LINE-COUNT lin new page with lin lines per page
...LINE-SIZE col new page with col columns per
line
NEW-LINE.
-
8/3/2019 Formatting Output With WRITE
29/73
System Fields for Lists
SY-PAGNO Current page number
SY-LINNI Current line number
SY-COLNO Column number of current cursor position
SY-TITLE Title that appears in the title bar of thedisplay window. Can be manipulated by:
maintaining text elements
using SET TITLEBAR .
SY-SROWS Current number of lines in displaywindow
SY-SCOLS Current number of columns in displaywindow
-
8/3/2019 Formatting Output With WRITE
30/73
Program Modularisation
-
8/3/2019 Formatting Output With WRITE
31/73
Why Modularise?
improve program structure
make the program easier to maintain andupdate
improve readability
reduce redundancy
allow for component reuse
event processing ABAP is an event driven language. The
processing associated with each event is writtenin a program module
-
8/3/2019 Formatting Output With WRITE
32/73
-
8/3/2019 Formatting Output With WRITE
33/73
Subroutine Syntax
FORM subr [TABLES formal table list]
[USING formal input list]
[CHANGING formal output list]ENDFORM.
PERFORM subr [TABLES actual table list]
[USING actual input list]
[CHANGING actual output list]
NOTE: No comma in parameters list
-
8/3/2019 Formatting Output With WRITE
34/73
Passing Data ByParameters
formal parameters - defined within FORM actual parameters - specified with PERFORM
parameter types
input - used to pass data to subroutines output- used to pass data from subroutines
input/output- pass data to & from subroutines
call by reference
call by value
normal scoping rules apply to calling &called data items
Global & Local
-
8/3/2019 Formatting Output With WRITE
35/73
Global & LocalData
REPORT ZSAPTEST.REPORT ZSAPTEST.* Global Data* Global Data
TABLES: ...TABLES: ...
DATA: X, Y.DATA: X, Y.
* Subroutine Call* Subroutine CallPERFORMPERFORM namename USING X Y.USING X Y.
* Subroutine* Subroutine
FORMFORM namename USING A B.USING A B.
* Local Data* Local Data
DATA: S TYPE i.DATA: S TYPE i.
* Statements* Statements
ENDFORM.ENDFORM.
TABLESTABLES
Global DataGlobal DataFormal ParametersFormal Parameters
Local DataLocal Data
TABLESTABLES
Global DataGlobal Data
-
8/3/2019 Formatting Output With WRITE
36/73
Passing Data By
Parameters passing by reference USING or CHANGING
USING for input parameters that are not
changed
CHANGING for output parameters that arechanged
passing by value FORM subr USINGVALUEVALUE((ff))
-
8/3/2019 Formatting Output With WRITE
37/73
report zpass_by_ref.
parameters: number type i default 10.
data: factorial type i value 0.
PERFORM fact USING number CHANGING factorial.
write: / 'factorial of ', (5) number, 'is ', (8) factorial.
FORM fact USING p_number
CHANGING p_factorial.
p_factorial = 1.
while p_number ge 1.
p_factorial = p_factorial * p_number.
p_number = p_number - 1.endwhile.
endform. " fact
-
8/3/2019 Formatting Output With WRITE
38/73
report zpass_by_ref.
parameters: number type i default 10.
data: factorial type i value 0.
PERFORM fact USING number CHANGING factorial.
write: / 'factorial of ', (5) number, 'is ', (8) factorial.
FORM fact USING value(p_number)
CHANGING p_factorial.
p_factorial = 1.
while p_number ge 1.
p_factorial = p_factorial * p_number.
p_number = p_number - 1.endwhile.
endform. " fact
-
8/3/2019 Formatting Output With WRITE
39/73
Terminating Subroutines
Unconditional termination EXIT in the subroutine
control leaves the subroutine andprocessing continues immediately afterthe calling PERFORM statement
Conditional termination CHECKcond in the subroutine
ifcond is not satisfied, same as above
-
8/3/2019 Formatting Output With WRITE
40/73
Function Modules
classified in function groups group of functions that serve similar
purposes eg, calendar functions
stored in the function library
main difference between subroutines
and functions is a clearly definedinterface for passing data betweenprogram and function
-
8/3/2019 Formatting Output With WRITE
41/73
Functions vs. Forms
Function modules have a special screenused for defining parameters -
parameters are not defined via ABAP/4statements.
Different syntax is used to call afunction module than to call a
subroutine. Leaving a function module is
accomplished via the raise statementinstead ofcheck or exit.
-
8/3/2019 Formatting Output With WRITE
42/73
Function BuilderFunction Builder
FM Group: FIBUFM_01 ...
FM_02 ...
FM Group: ZIBU
FM_03
FM_04 ...
Maintaining FunctionMaintaining Function
ModulesModulesFM_02
Interface
Import
Export
TablesExceptions
Program
Documentation
Administration
Using FunctionUsing Function
ModulesModules
REPORT ...
TABLES:
CALL FUNCTION
FM_02
EXPORTING
IMPORTING...
B ildi F ti
-
8/3/2019 Formatting Output With WRITE
43/73
Building a Function
-
8/3/2019 Formatting Output With WRITE
44/73
-
8/3/2019 Formatting Output With WRITE
45/73
45
-
8/3/2019 Formatting Output With WRITE
46/73
46
-
8/3/2019 Formatting Output With WRITE
47/73
Testing Function Modules
from the Function Builder: InitialScreenchoose Single test
assign values to the importparameters
press execute to display the Test
Function Module: Results Screen system provides results as well as
a runtime analysis of execution
time
-
8/3/2019 Formatting Output With WRITE
48/73
48
-
8/3/2019 Formatting Output With WRITE
49/73
49
-
8/3/2019 Formatting Output With WRITE
50/73
Calling Functions
CALL FUNCTION function_name
[EXPORTING f1=a1 fn=an]
[IMPORTING f1=a1 fn=an]
[CHANGING f1=a1 fn=an]
[TABLES f 1=a1 fn=an]
[EXCEPTIONS e1=r1 en=rn ]
[ERROR_MESSAGE = E]
[OTHERS = o]].
-
8/3/2019 Formatting Output With WRITE
51/73
Calling Functions
EXPORTING f1=a1 fn=an enables the passing of actual
parameters to formal inputparametersof the function
IMPORTING f1=a1 fn=an enables the passing of formal output
parameters of the function back to thecorresponding actual parameters of the
calling program
-
8/3/2019 Formatting Output With WRITE
52/73
Calling Functions
CHANGING f1=a1 fn=an enables the passing of actual parameters
to the formal parameters and, afterprocessing the system passes the(changed) formal parameters back to theactual parameters
TABLES f1=a1 fn=an enables the passing of internal tables
between actual and formal parameters
tables are alwa s assed b reference
-
8/3/2019 Formatting Output With WRITE
53/73
Calling Function example
parameters: num1 type i default 2,
num2 type i default 3.
data result type p decimals 2.
CALL FUNCTION 'Z_DIVIDE' EXPORTING p1 = num1 p2 = num2
IMPORTING p3 = result.
write: / num1, '/', num2, '=', result.
function z_divide.* IMPORTING P1 TYPE I DEFAULT 1 P2 TYPE I
* EXPORTING P3 TYPE P
p3 = p1 / p2.
endfunction.
2 / 3 = 0.67
-
8/3/2019 Formatting Output With WRITE
54/73
Calling FunctionsEXCEPTIONS f1=a1 fn=an
functions are defined with exceptions
the calling program determines whether
and which exceptions it is to handle itself the OTHERS clause covers all exceptions
not explicitly specified
if an error occurs in the function executionit is either handled in the function orcontrol immediately returns to the callingprogram (if the exception is specified inthe EXCEPTIONS parameter of the call)
X
-
8/3/2019 Formatting Output With WRITE
55/73
ABAP Function Module: FILL_SEATTABABAP Function Module: FILL_SEATTAB
ExceptionsExceptions
NO_ENTRYNO_ENTRY
X
call function 'fill_seattab'call function 'fill_seattab'EXPORTING year = yearEXPORTING year = yearTABLES seattab = itabTABLES seattab = itabEXCEPTIONS no_entry = 01EXCEPTIONS no_entry = 01
others = 02.others = 02.
case sy-subrc.case sy-subrc.when 1. write No entry.when 1. write No entry.when 2. write Other error.when 2. write Other error.
endcase.endcase.
Exception defined in
function definition
Exception condition
checked on return
from function
Usingf i di id
-
8/3/2019 Formatting Output With WRITE
56/73
Usingraise
function z_divide.
* IMPORTING P1 TYPE I DEFAULT 1
* P2 TYPE I
* EXPORTING P3 TYPE P
* EXCEPTIONS DIV_BY_ZERO
if p2 = 0.
raise div_by_zero.
endif.p3 = p1 / p2.
endfunction.
parameters: num1 type i default 2,num2 type i default 3.
data result type p decimals 2.
CALL FUNCTION 'Z_DIVIDE'
EXPORTING p1 = num1 p2 = num2
IMPORTING p3 = result
EXCEPTIONS div_by_zero = 01.
if sy-subrc = 1.
write 'Error: Divide by Zero'.
else
write: / num1, '/', num2, '=',
result.
endif.
-
8/3/2019 Formatting Output With WRITE
57/73
EditPattern
-
8/3/2019 Formatting Output With WRITE
58/73
58
-
8/3/2019 Formatting Output With WRITE
59/73
Remote Function Calls
Remote Function Calls offer an easy way to implement communication
between application programs via ABAP functioncalls
do not have to worry about incompatibilitiesbetween
hardware platforms
operating systems
relieves the programmer from technicalconsiderations of communication protocols
-
8/3/2019 Formatting Output With WRITE
60/73
Remote Function Calls
Local Function Call called function resides on the same machine
as the calling program
Remote Function Call called function resides on a different
machine to the calling program
machines must be connected via a network useful for instance if there are manybranches/offices where some form of centralisedreporting is required
-
8/3/2019 Formatting Output With WRITE
61/73
Remote Function Calls
Syntax similar to local function callexcept for the addition of an extraparameter DESTINATION dest. machine name
inclusion of the destination parameteralerts the system to the fact that the
called function does not reside locally,but on a system whose name isdestination machine name
Remote Function Call -
-
8/3/2019 Formatting Output With WRITE
62/73
Remote Function Call Example
parameters: date1 type d,
date2 type d.
data: local_agency(20),
local_bookings like sbook occurs 0
with header line.
CALL FUNCTION 'GET_LOCAL_BOOKINGS'
DESTINATION 'PARIS'
EXPORTING from_date = date1to_date = date2
IMPORTING agency = local_agency
TABLES bookings = local_bookings.
Remote F nction Calls
-
8/3/2019 Formatting Output With WRITE
63/73
Remote Function Calls
Valid destinations are defined in tableRFCDES
-
8/3/2019 Formatting Output With WRITE
64/73
64
-
8/3/2019 Formatting Output With WRITE
65/73
-
8/3/2019 Formatting Output With WRITE
66/73
Remotely Callable
Functions Function must be designated as
supporting remote function calls in theRemote System ABAP automatically generates a piece of code
that serves as the entry routine (called a stub)for the incoming call
stub receives the incoming data and routes it tothe desired function
function is carried out and results sent back tothe user
When an RFC is issued
-
8/3/2019 Formatting Output With WRITE
67/73
When an RFC is issued...
Runtime system converts data to machine independent
representation
sends the data over the comms line
Partner (remote) system resolves inconsistencies in terms of internal data representation
different code pages
begins new session session stays alive as long as the calling program lives
invokes called function parameters passed by value only
sends data back to the calling system
-
8/3/2019 Formatting Output With WRITE
68/73
Handling Communication
Errors Exception conditions communication_failure
error on the comms line during callexecution
system_failureany runtime error on the partner system
function does not exist
function not declared as remotely callable
can add message fld_name to theEXCEPTION clause to receive errormessage text associated with theerror
-
8/3/2019 Formatting Output With WRITE
69/73
parameters: date1 type d,
date2 type d.
data: local_agency(20), sys_msg(80), comms_msg(80),local_bookings like sbook occurs 0 with header line.
CALL FUNCTION 'GET_LOCAL_BOOKINGS'
DESTINATION 'PARIS'
EXPORTING from_date = date1
to_date = date2IMPORTING agency = local_agency
TABLES bookings = local_bookings
EXCEPTIONS system_failure = 1 message sys_msg
communication_failure = 2 message comms_msg.
case sy-subrc.
when 1. write: / System Error:, sys_msg.
when 2. write: / Comms Error:, comms_msg.
endcase.
-
8/3/2019 Formatting Output With WRITE
70/73
Testing Remote Function
CallsThe special destination NONE can
be used to test remote function
calls: Create function on local machine
Activate it as supporting remote calls
Call it using 'NONE'
-
8/3/2019 Formatting Output With WRITE
71/73
Conclusion
This lecture covered topics associatedwith formatting output using the WRITE statement
colours position and length
lines and blank lines
subroutines in ABAP
FORM ENDFORM, PERFORM Parameter passing
CALL FUNCTION
Remote Function Calls (RFCs)
-
8/3/2019 Formatting Output With WRITE
72/73
Associated Reading Textbook
Ch 3.4.4 Output Formatting
Ch 3.2.12 Subroutines
Ch 3.9 Function Modules On Line Help
R/3 LibraryBC..ABAP Workbench BC ABAP Users Guide
The ABAP Programming Language Modularization Modularizing ABAP Programs
Source Code Modules, Subroutines, Function Modules Basic Statements
Processing Data Basic form of the WRITE TO Statement Writing Values With Offset Specifications
ABAP User Interfaces Lists Creating Simple Lists With The WRITE Statement
-
8/3/2019 Formatting Output With WRITE
73/73
Pre-Reading
Textbook Ch 3.4.2 (p 211): The Events Concept
Ch 3.4.3 (p 213): Selections andParametersSelection Screens
Using Variants
Ch 3.5 (p 243): Interactive Reports