Sap Script
Sap Script
Often there are instances where an output from a SAP program is required on a physical paper in a
pre-designed format. Using normal ABAP code this is not possible. Instead SAP provides an object
called SAPSCRIPT to generate such kind of documents which can contain logos, tables and other
objects and which can look like pre-printed documents.
This article focuses on the design and use of Layout sets in ABAP programs to generate beautified
output in SAP.
A layout set is a template designed in SAP to place the stream of data coming from a SAP program
on different parts of a physical page. The designer needs to lay out the various elements that need to
be printed on the page and store it as an object in the SAP system. An ABAP program will
subsequently call this object to generate an instance of the template – thus generating an output
document from the program.
Language
Language in which the data coming on to the layout set will be printed. Generally, this will be the
language that has been set up as default in the SAP system
Header
Section to define the various attributes of the layout set on a global level. Changing these attributes
will affect all the components of the layout set.
Administration Information
This shows the information about the layout set – details of the designer, details of changes
occurring to the design, development class of the layout set and the language details for the
layout set.
Standard Attributes
Font Attributes
Here the various attributes and the base font applicable to the document can be
defined. This font setting can be overridden at a lower level using the character
strings
Paragraphs
Used to define the start and end positions for the different texts and objects that need to be printed on
the output document.
Character Strings
Used to define the fonts and the formatting and printing styles for each and every character that
needs to be printed on the output document. The start of the character string is indicated by <string
name>, while the end of the character string is indicated by </>
Pages
The designer needs to organize the template as a series of pages. When an actual output document
is printed, it will refer to each page for putting the data coming from the ABAP program. The order of
pages is also taken from the template i.e the layout set defined.
Windows
Various parts of the output document can be conveniently organized on the pages using windows.
Thus the data stream coming from the ABAP program can be logically grouped into various parts and
can be placed on different locations on a page
There are 2 main types of windows that can be used in a layout set:
MAIN - A layout set can have only one MAIN window, which is created by default. This window can
flow over multiple pages.
CONSTANT - A layout set can have any number of constant windows. A constant window can
be used once per page.
Text Elements
Any text that needs to be written on the output document should be placed within a text element. This
includes constant text as well as variable data like internal table data coming from the ABAP program.
The fields of various tables defined in the ABAP program will be included under these text elements.
These fields are carriers of data. Every field should be included in a pair of & characters. (e.g. &aufk-
aufnr&)
Page Windows
Choose the window and click the Text Elements button to go to the Layout Set Editor. This consists
of 2 parts.
The small space on the left is for specifying the type of command, while the window adjacent to it is
for writing the command or the text that needs to go under a text element.
The various types of commands that can be used within a layout set are tabulated below
Command Purpose
* Default paragraph
= Extended Line
( Raw Line
/ Line Feed
/: Command Line
/* Comment Line
/E Text Element
<PN> This is either the name of the paragraph that should be applicable from that line of the layout set
The steps for including graphical elements in the layout set are as follows
The graphical element (like company logo) should be in valid graphic file format like .bmp or
.jpg
Use appropriate software to convert the above file into a .TIFF file
The SAP system suggests a name for the file ( like ZHEX-MARCO-* ). The * indicates the
type of file. For e.g. if the file contains a logo then the name can be ZHEX-MACRO-LOGO
Running the program will convert this .TIFF file into a text element
Incorporate this converted logo in the appropriate window under the appropriate text element
by giving
SAPscript - Commands
The commands that are commonly used in a sap script are as follows -:
Command Use
New-page <page name> Prints the text following this command on a new page (when a page
name is specified then that page is taken as the next page)
Protect….... Endprotect This acts like a conditional page break. Putting the text within this
command prevents the breaking of the text across multiple pages.
If there is not enough space for the entire paragraph to be printed in
the space remaining on the page, then the entire paragraph is
printed on the next page
Box <xpos> <ypos> <width> <height> The BOX command draws a box as per the specifications. The x y
<frame> <intensity> co-ordinates are for the upper left corner relative to the values in
the position command.
Position <xorigin> <yorigin> <window>
<page> POSITION command is used to set the x y co-ordinates with
respect to the start position of the window.
Size <width> <height> <window> <page>
SIZE command is used to specify the size of the box that we need
to draw.
IF ….. END IF This allows the conditional printing of the text on the output
document. The various conditional operators that can be used are
as follows
= EQ Equal to
NOT, AND, OR
The best way to explain the various steps in designing a SAP script is to visualize the creation
of a document. The entire process is explained as a series of steps.
Let us assume that we need to design the document Invoice.doc for the ABC Company Limited.
1. Understand the structure of the document that needs to be generated page by page.
3. Decide the FIRST page of the document and the pages that are going to follow.
4. Find out the various fonts and styles (bold, italics, etc) that are used in the document.
5. Also try to group the data printed on the document into logical parts.
6. Create all the character strings that have been used in the document
7. Create all the paragraphs that have been used in the document
8. Create all CONSTANT the windows that have been uniquely identified in the document
10. Define the pages that form the parts of the document
14. Use function module WRITE_FORM to write the text elements in various windows
Always remember to check and activate the layout set when any change is done to it,
otherwise the change will not appear on the output document that is printed!!!
Standard attributes
Font attributes
Characters Attributes
Paragraphs Attributes
P1 Default Paragraph
Standard attributes
Line spacing 1.00 LN
Left margin 1.00 CM
Alignment Left-aligned
Font attributes
Font family TIMES
Font size 12.0 Point
P2 Header Paragraph
Standard attributes
Line spacing 1.00 LN
Left margin 4.50 CM
Alignment Left aligned
Font attributes
Font family TIMES
Font size 18.0 Point
Bold Yes
P3 Underlined paragraph
Standard attributes
Line spacing 1.00 LN
Alignment Left aligned
Font attributes
Font family TIMES
Font size 12.0 Point
Underlined Yes
Windows Attributes
MAIN Main window
Window type MAIN
HEADER Main window
Window type CONSTANT
FOOTER Main window
Window type CONSTANT
Pages Attributes
Element HEADER
/: POSITION XORIGIN 2 CM YORIGIN '-0.5 CM'
/: BOX XPOS 1 CM YPOS 1 CM WIDTH 18 CM HEIGHT 1 CM FRAME 10 TW INTENSITY 10
/
/
/
P2 ,,<B>TEST PURCHASE ORDER</>
/
/
MAIN
Element MAIN
P1 <B>Customer/Supplier:</>,,&KNA1-NAME1&
/
P1 <B>PO No:</>,,&EKPO-EBELN&
/
P1 <B>Part No:</>,,&MAKT-MATNR&
/
P1 <B>Description:</>,,&MAKT-MAKTX&
/
P1 <B>Quantity:,,</>&EKPO-MENGE&
/
P1 <B>Sign:</>&uline(81)&
/
P1 <B>Date:</>&EKKO-AEDAT&
FOOTER
Element FOOTER
/: POSITION XORIGIN 2 CM YORIGIN '-0.5 CM'
/: BOX XPOS 1 CM YPOS 1 CM WIDTH 18 CM HEIGHT 1 CM FRAME 10 TW INTENSITY 10
/
/
/
P2 ,,<B>PLEASE SIGN THE PO BEFORE DISPATCH</>
/
/
REPORT ZPSAPSCRIPT.
TABLES : EKKO,
EKPO,
KNA1,
USR01,
MARA,
MAKT.
CLEAR EKPO.
SELECT SINGLE * FROM EKPO
WHERE EBELN = P_EBELN AND
EBELP = P_EBELP.
CLEAR KNA1.
SELECT SINGLE NAME1 FROM KNA1
INTO KNA1-NAME1
WHERE KUNNR = EKPO-KUNNR.
CLEAR MAKT.
SELECT SINGLE MAKTX FROM MAKT
INTO MAKT-MAKTX
WHERE MATNR = EKPO-MATNR AND
SPRAS = SY-LANGU.
CLEAR USR01.
SELECT SINGLE * FROM USR01 WHERE BNAME = SY-UNAME.