Sunday, December 5, 2010

ABAP: Convert Spool List to ALV Grid

Hello,
At times we need to submit programs in background mode(batch jobs) , but the output needs to be displayed in the form of ALVs.
Here is sample code for the same.

Convert Spool List to ALV Grid



FUNCTION ztest_conv_spool_list_alv.


*"----------------------------------------------------------------------


*"*"Local Interface:


*" IMPORTING


*" REFERENCE(I_SPOOLN) TYPE CHAR10


*" REFERENCE(I_KEEP_SUM_LINE) TYPE BOOLEAN DEFAULT 'X'


*" REFERENCE(I_START_TABLE) TYPE INT4 DEFAULT 1


*"----------------------------------------------------------------------


TYPE-POOLS: slis.


TABLES: tsp01.


DATA: BEGIN OF gtbl_data OCCURS 0,


data(2048) TYPE c,


END OF gtbl_data.


DATA: BEGIN OF gtbl_col OCCURS 0,


col(255) TYPE c,


END OF gtbl_col.


DATA gv_line TYPE i.


DATA gv_length TYPE i.


DATA gv_cnt TYPE i.


DATA gv_dec TYPE i.


DATA gv_row(2048) TYPE c. "string.


DATA gv_row_d(2048) TYPE c. "string.


DATA gv_spoolnum TYPE tsp01-rqident.


DATA gtbl_fieldcat TYPE slis_t_fieldcat_alv.


DATA gstr_fieldcat LIKE LINE OF gtbl_fieldcat.


DATA gstr_data LIKE LINE OF gtbl_data.


DATA gstr_data_d LIKE LINE OF gtbl_data.


DATA gtbl_match TYPE match_result_tab.


DATA gtbl_match_last TYPE match_result_tab.


DATA gtbl_fcat TYPE lvc_t_fcat.


DATA gstr_fcat LIKE LINE OF gtbl_fcat.

DATA gref_data TYPE REF TO data.


DATA gref_new_line TYPE REF TO data.


FIELD-SYMBOLS: <fs_data> TYPE REF TO data.


FIELD-SYMBOLS: <fs_dyntable> TYPE STANDARD TABLE.


FIELD-SYMBOLS: <fs_match> LIKE LINE OF gtbl_match.


FIELD-SYMBOLS: <fs_dynline> TYPE ANY.


FIELD-SYMBOLS: <fs_dynstruct> TYPE ANY.


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


" Read the spool


gv_spoolnum = i_spooln.


CALL FUNCTION 'RSPO_RETURN_ABAP_SPOOLJOB'


EXPORTING


rqident = gv_spoolnum


TABLES


buffer = gtbl_data.


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


" Pre-check to make sure we have a list


" simply checking the first char of the


" first and the last line


READ TABLE gtbl_data INTO gstr_data INDEX 1.


IF sy-subrc = 0.


IF gstr_data-data(1) <> '-'.


MESSAGE 'Spool does not contain any ALV list' TYPE 'E'.

ELSE.


" Number of rows


gv_line = LINES( gtbl_data ).


" Read the last line


READ TABLE gtbl_data INTO gstr_data INDEX gv_line.


IF sy-subrc = 0.


IF gstr_data-data(1) <> '-'.


MESSAGE 'Spool does not contain any ALV list' TYPE 'E'.


ENDIF.


ENDIF.


ENDIF.


ENDIF.


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


" Start with the N table in the spool.


IF i_start_table > 1.


FREE gv_line.


gv_dec = i_start_table.


SUBTRACT 1 FROM gv_dec.


gv_dec = gv_dec * 2.


" Find the end of the table N - 1


LOOP AT gtbl_data INTO gstr_data WHERE data(1) = '-'.


ADD 1 TO gv_line.


IF gv_line = gv_dec.


gv_dec = sy-tabix.


EXIT.


ENDIF.


ENDLOOP.


" Delete the rows up table N


DELETE gtbl_data FROM 1 TO gv_dec.


ENDIF.


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


" Check how many ALV list are in the spool


" and make sure if more than one the # of columns are matching


FREE gv_line .


LOOP AT gtbl_data INTO gstr_data WHERE data(1) = '-'.


ADD 1 TO gv_line.


gv_cnt = gv_line MOD 2.


" The column headings are on odd number in our find counter


IF gv_cnt <> 0.


" Save the find counter value


gv_cnt = gv_line.


" Update index to point to column heading row and read


gv_line = sy-tabix + 1.


READ TABLE gtbl_data INDEX gv_line INTO gstr_data.


gv_row = gstr_data-data.


" Find the columns: position and length


FIND ALL OCCURRENCES OF '
' IN gv_row RESULTS gtbl_match.


" Compare the previous heading w/ current


IF gtbl_match[] <> gtbl_match_last[] AND sy-tabix > 2.


MESSAGE 'Spool contains more than one ALV list where column headings are different.' TYPE 'E'.


ENDIF.


FREE: gtbl_match_last, gv_row.


gtbl_match_last[] = gtbl_match[].


FREE gtbl_match.


" Get back the find counter value


gv_line = gv_cnt.


ENDIF.


ENDLOOP.


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


" Read column heading row


READ TABLE gtbl_data INDEX 2 INTO gstr_data.


gv_row = gstr_data-data.


" Read also the first data row


" Here we are assuming tha the first row all fields


" filled in; will use this to determine mumeric or char


READ TABLE gtbl_data INDEX 4 INTO gstr_data_d.


gv_row_d = gstr_data_d-data.


" Find out the columns


FIND ALL OCCURRENCES OF '
' IN gv_row RESULTS gtbl_match.


FREE: gv_cnt, gv_line.


" Setup the field catalog for itab


LOOP AT gtbl_match ASSIGNING <fs_match>.


IF sy-tabix > 1.


" Field length


gv_length = <fs_match>offset gv_line + 1.


" Update counter used for column heading


ADD 1 TO gv_cnt.


" Used for dynamic itab


gstr_fcat-datatype = 'C'.


gstr_fcat-fieldname = gv_cnt.


gstr_fcat-intlen = gv_length.


gstr_fcat-tabname = ''.


" Debug and you will see why...


SUBTRACT 1 FROM gv_length.


" Used for ALV grid


gstr_fieldcat-reptext_ddic = gv_row+gv_line(gv_length).


gstr_fieldcat-tabname = ''.


gstr_fieldcat-fieldname = gv_cnt.


gstr_fieldcat-just = 'L'.


gstr_fieldcat-outputlen = gv_length.


APPEND gstr_fcat TO gtbl_fcat.


APPEND gstr_fieldcat TO gtbl_fieldcat.


FREE: gstr_fcat, gstr_fieldcat, gv_dec.


ENDIF.


" Start position of next column


gv_line = <fs_match>-offset + 1.


ENDLOOP.


ASSIGN gref_data TO <fs_data>.


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


" Create a dynamic table based on the number of columns above


CALL METHOD cl_alv_table_create=>create_dynamic_table


EXPORTING


it_fieldcatalog = gtbl_fcat


IMPORTING


ep_table = <fs_data>


EXCEPTIONS


generate_subpool_dir_full = 1


OTHERS = 2.


ASSIGN <fs_data>->* TO <fs_dyntable>.


" Create a new mem area


CREATE DATA gref_new_line LIKE LINE OF <fs_dyntable>.


" Now point our <FS_*> to the mem area


ASSIGN gref_new_line->* TO <fs_dynstruct>.


ASSIGN gref_new_line->* TO <fs_dynline>.


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


" Remove column headings that appears in the middle


" which are caused due to spool page-break


LOOP AT gtbl_data INTO gstr_data_d FROM 4 WHERE data = gstr_data-data.


DELETE gtbl_data.


ENDLOOP.


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


" Push data to itab


LOOP AT gtbl_data INTO gstr_data.


" The first 3 rows are col heading related


IF sy-tabix > 3 AND (


gstr_data-data(2) <> '
-' AND " Column heading row


gstr_data-data(2) <> '--' " End of list row


).


REPLACE FIRST OCCURRENCE OF '
' IN gstr_data-data WITH space.


SPLIT gstr_data AT '
' INTO TABLE gtbl_col.


gv_cnt = 0.


" Split above makes each column to a row


" Get each column


LOOP AT gtbl_col.


ADD 1 TO gv_cnt.


ASSIGN COMPONENT gv_cnt OF STRUCTURE <fs_dynstruct> TO <fs_dynline>.


" Remove space front/end


CONDENSE gtbl_col-col.


MOVE gtbl_col-col TO <fs_dynline>.


ENDLOOP.


APPEND <fs_dynstruct> TO <fs_dyntable>.


gv_cnt = 0.


FREE: gtbl_col.


ENDIF.


FREE gstr_data.


ENDLOOP.


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


" Sum line flag, keep or delete


IF i_keep_sum_line IS INITIAL.


LOOP AT <fs_dyntable> ASSIGNING <fs_dynline>.


IF <fs_dynline>(1) = '*'.


DELETE <fs_dyntable>.


ENDIF.


ENDLOOP.


ENDIF.


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


" Display


CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


EXPORTING


it_fieldcat = gtbl_fieldcat


TABLES


t_outtab = <fs_dyntable>.


ENDFUNCTION.









2 comments:

  1. Spool is nothing but when you schedule a program/transaction in background mode it stores the results(lists/ALVS) in some temporary files(TemSe) in the shared memory.This data can be used for printing.

    ReplyDelete