Showing posts with label Sapscript. Show all posts
Showing posts with label Sapscript. Show all posts

Thursday, July 30, 2009

Display background (water mark) image using SAPscript

Joyjit Ghosh, Kolkata

Detail Steps:

Step 1: Create a standard text (transaction SO10) and insert the background image in it


Step 2: Now open the layout set (transaction SE71) in which background image needs to be displayed and do the required changes as mentioned below.

Note: Here I am using an existing custom sapscript layout where all windows, pages and page windows are already created except the background image. And for this demo I am showing the changes required for inserting the background image in the 1st page.

Goto “Page window” of the 1st page and create another page window of type ‘MAIN’. And its attributes must be same as the existing “MAIN” window.





Now open the Text Elements of the main window in edit mode and in the 1st line of the Sapscript editor insert the standard text (created above) that contains the background image.




Now in the 2nd line insert the special command NEW-WINDOW


Definition of NEW-WINDOW command:

Each page can consist of up to 99 main windows. Each main window is assigned a consecutive identifying number (0..98), and the windows are filled in this order. This feature enables SAPscript to print labels and to output multi-column text. When one main window fills up, the next main window on that page is taken, if there is a next one. A page break is inserted after the last main window.

You can use the NEW-WINDOW command to call the next main window explicitly, even if the current main window is not yet full. If you are in the last main window of the page, the command has the same effect as the NEW-PAGE command

Now activate the form

Step 3: Maintain required configuration for triggering the form

Step 4: Now trigger the form and see the print preview


Result:

Form is displayed with background image in print preview.





Monday, May 18, 2009

Passing table data to the layout without changing the driver program

By Joyjit Ghosh

I have seen a typical requirement from client that SAP script layout need to be changed (additional data need to be displayed) without modifying the driver program (mainly standard SAP program). This tip will show us how to pass table data (multiple records at a time) to layout without changing the driver program.

Step1. Create a standard text from SO10.

Create a blank standard text. This will store the table data

Step2. Create a subroutine pool and a routine in it that can be called from SAP script.

From transaction SE38 create a subroutine pool.

Now create subroutine with proper interface to fetch the data from the table.

Step3. Within this routine write the logic to fetch the table data and populate the standard text.

***************************************************************
* Fetch table data and upload the data in proper format to the
* standard text
***************************************************************
DATA: i_zemployee TYPE STANDARD TABLE OF zemployee INITIAL SIZE 0,
w_zemployee TYPE zemployee,
i_text TYPE STANDARD TABLE OF tline INITIAL SIZE 0,
w_header LIKE thead,
w_text TYPE tline.
CONSTANTS: c_par TYPE char2 VALUE ',,'. " Sign for tabs
* Fetch data for employee
SELECT * FROM zemployee
INTO TABLE i_zemployee.
  IF sy-subrc = 0.
*  Create text table
LOOP AT i_zemployee INTO w_zemployee.
*     Store default paragraph format
w_text-tdformat = '*'.
*     Add all the required fields separated by tab
CONCATENATE w_zemployee-empno w_zemployee-empname
INTO w_text-tdline
SEPARATED BY c_par.
*     Store table data
APPEND w_text TO i_text.
    ENDLOOP.
    check sy-subrc = 0.
*   Populate header info
*   Text object
w_header-tdobject = 'TEXT'.
*   Standard text name
w_header-tdname = 'Z_TABLE_DATA'.
*   Text id
w_header-tdid = 'ST'.
*   Language
w_header-tdspras = 'E'.
*  Populate the standard text with table data
CALL FUNCTION 'SAVE_TEXT'
EXPORTING
header = w_header
insert = 'X'
savemode_direct = 'X'
TABLES
lines = i_text
EXCEPTIONS
id = 1
language = 2
name = 3
object = 4
OTHERS = 5
.
IF sy-subrc <> 0.
    ENDIF.
  ENDIF. 

Step4. Call the routine and standard text from the SAP script layout.

Note: For the sake of simplicity this tip is shown in a custom layout that is called from a custom report.

/*   Call the routine
/: PERFORM FETCH_TABLE_DATA IN PROGRAM Z_SUBROUTINE_POOL
/: USING &INVAR1&
/: CHANGING &OUTVAR1&
/: ENDPERFORM
/*   Now call the standard text
/: INCLUDE Z_TABLE_DATA OBJECT TEXT ID ST LANGUAGE EN

Step5. Test the SAP script form

Activate the SAP script debugger


Run the custom report to test the SAPscript layout.

*&---------------------------------------------------------------*
*& Report Z_TEST_SAPCSRIPT *
*&---------------------------------------------------------------*
REPORT  Z_TEST_SAPCSRIPT                        .
start-of-selection.
CALL FUNCTION 'OPEN_FORM'
EXPORTING
DEVICE = 'PRINTER'
DIALOG = 'X'
FORM = 'Z_DEMO_LAYOUT'
LANGUAGE = SY-LANGU
EXCEPTIONS
CANCELED = 1
DEVICE = 2
FORM = 3
OPTIONS = 4
UNCLOSED = 5
MAIL_OPTIONS = 6
ARCHIVE_ERROR = 7
INVALID_FAX_NUMBER = 8
MORE_PARAMS_NEEDED_IN_BATCH = 9
SPOOL_ERROR = 10
CODEPAGE = 11
OTHERS = 12
.
IF sy-subrc = 0.
CALL FUNCTION 'WRITE_FORM'
EXPORTING
ELEMENT = '001'
FUNCTION = 'SET'
TYPE = 'BODY'
WINDOW = 'MAIN'
* IMPORTING
* PENDING_LINES =
EXCEPTIONS
ELEMENT = 1
FUNCTION = 2
TYPE = 3
UNOPENED = 4
UNSTARTED = 5
WINDOW = 6
BAD_PAGEFORMAT_FOR_PRINT = 7
SPOOL_ERROR = 8
CODEPAGE = 9
OTHERS = 10
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
 CALL FUNCTION 'CLOSE_FORM'
EXCEPTIONS
UNOPENED = 1
BAD_PAGEFORMAT_FOR_PRINT = 2
SEND_ERROR = 3
SPOOL_ERROR = 4
CODEPAGE = 5
OTHERS = 6
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDIF.

After execution of this report SAPscript debugger is triggered.

As shown below layout is calling the code written in the routine

Standard text is populated with the data fetched from table

Output of SAP script:

Table entries:

Sunday, July 13, 2008

Download document server image

By Joyjit Ghosh,
Kolkata, India.

*&---------------------------------------------------------------------

*& Report Z_DOWNLOAD_BDS_GRAPHICS

*&---------------------------------------------------------------------

*& Download image stored in document server

*&---------------------------------------------------------------------

REPORT z_download_bds_graphics .

***********************************************************************

* Variable declaration

***********************************************************************

DATA: v_graphic_size TYPE i,

v_graphic_xstr TYPE xstring,

v_graphic_conv TYPE i,

v_graphic_offs TYPE i,

v_file TYPE string.

***********************************************************************

* Table declaration

***********************************************************************

DATA: BEGIN OF i_graphic_table OCCURS 0,

line(255) TYPE x,

END OF i_graphic_table.

***********************************************************************

* Structure declaration

***********************************************************************

DATA: st_stxbitmaps TYPE stxbitmaps.

***********************************************************************

* Selection screen

***********************************************************************

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

PARAMETERS: p_object LIKE st_stxbitmaps-tdobject DEFAULT 'GRAPHICS'

MODIF ID abc ,

p_name LIKE st_stxbitmaps-tdname,

p_id LIKE st_stxbitmaps-tdid DEFAULT 'BMAP'

MODIF ID abc ,

p_type LIKE st_stxbitmaps-tdbtype,

p_dir TYPE localfile.

SELECTION-SCREEN END OF BLOCK b1.

***********************************************************************

* At Selection-screen output event

***********************************************************************

AT SELECTION-SCREEN OUTPUT.

LOOP AT SCREEN.

IF screen-group1 = 'ABC' .

screen-input = '0'.

MODIFY SCREEN.

ENDIF.

ENDLOOP.

***********************************************************************

* At Selection-screen on value-request event

***********************************************************************

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_dir.

DATA: l_folder TYPE string.

CALL METHOD cl_gui_frontend_services=>directory_browse

EXPORTING

window_title = 'Select Folder'

initial_folder = 'C:\'

CHANGING

selected_folder = l_folder

EXCEPTIONS

cntl_error = 1

error_no_gui = 2

not_supported_by_gui = 3

OTHERS = 4.

IF sy-subrc = 0.

p_dir = l_folder.

ENDIF.

***********************************************************************

* Start-of-selection event

***********************************************************************

START-OF-SELECTION.

st_stxbitmaps-tdobject = p_object.

st_stxbitmaps-tdname = p_name.

st_stxbitmaps-tdid = p_id.

st_stxbitmaps-tdbtype = p_type.

* Get the bmp image from BDS in hex string format

CALL METHOD cl_ssf_xsf_utilities=>get_bds_graphic_as_bmp

EXPORTING

p_object = st_stxbitmaps-tdobject

p_name = st_stxbitmaps-tdname

p_id = st_stxbitmaps-tdid

p_btype = st_stxbitmaps-tdbtype

RECEIVING

p_bmp = v_graphic_xstr

EXCEPTIONS

not_found = 1

internal_error = 2

OTHERS = 3.

IF sy-subrc = 0.

* Find the length of hex string

v_graphic_size = xstrlen( v_graphic_xstr ).

CHECK v_graphic_size > 0.

v_graphic_conv = v_graphic_size.

v_graphic_offs = 0.

* Populate internal table from this hex string

WHILE v_graphic_conv > 255.

i_graphic_table-line = v_graphic_xstr+v_graphic_offs(255).

APPEND i_graphic_table.

v_graphic_offs = v_graphic_offs + 255.

v_graphic_conv = v_graphic_conv - 255.

ENDWHILE.

i_graphic_table-line = v_graphic_xstr+v_graphic_offs(v_graphic_conv).

APPEND i_graphic_table.

* Prepare file name and file path

CONCATENATE p_dir '\' p_name '.BMP' INTO v_file.

* Download image

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

bin_filesize = v_graphic_size

filename = v_file

filetype = 'BIN'

TABLES

data_tab = i_graphic_table

EXCEPTIONS

file_write_error = 1

no_batch = 2

gui_refuse_filetransfer = 3

invalid_type = 4

no_authority = 5

unknown_error = 6

header_not_allowed = 7

separator_not_allowed = 8

filesize_not_allowed = 9

header_too_long = 10

dp_error_create = 11

dp_error_send = 12

dp_error_write = 13

unknown_dp_error = 14

access_denied = 15

dp_out_of_memory = 16

disk_full = 17

dp_timeout = 18

file_not_found = 19

dataprovider_exception = 20

control_flush_error = 21

OTHERS = 22.

IF sy-subrc = 0.

WRITE: 'File downloaded successfully'(002).

ELSE.

WRITE: 'Error during file download'(003).

ENDIF.

ELSE.

CASE sy-subrc.

WHEN 1.

WRITE: 'Image not found'(004).

WHEN OTHERS.

WRITE: 'Error in Image retrieval'(005).

ENDCASE.

ENDIF.