Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 10548

Re: Combine Two dynamic tables

$
0
0

Try this subroutine.

 

Logic in subroutine create new internal table, comparing data and header internal table, to find out optimum length required to fit in the contents.  All fields in final internal table has type C. It will take care out output format of date and time field if any in data internal table.

 

Simply make a call to routine

 

DATA : ref_table TYPE REF TO data .
FIELD-SYMBOLS : <fs_table> TYPE table .

PERFORM merge_tables USING f_sheet_row gi_header CHANGING ref_table .

ASSIGN ref_table->* TO <fs_table> .

 

 

FORM merge_tables USING f_sheet_row TYPE table
                         gi_header   TYPE table
                   CHANGING ref_table TYPE REF TO data .

* Read gi_header and create internal table with component just enough long to
* fit description
   DATA : lo_header_table_descr TYPE REF TO cl_abap_tabledescr  ,
          lo_header_line_type   TYPE REF TO cl_abap_structdescr ,
          lo_data_table_descr   TYPE REF TO cl_abap_tabledescr  ,
          lo_data_line_type     TYPE REF TO cl_abap_structdescr ,
          lo_newtab_table_descr TYPE REF TO cl_abap_tabledescr  ,
          lo_newtab_line_descr  TYPE REF TO cl_abap_structdescr ,
          lo_element_descr      TYPE REF TO cl_abap_elemdescr   ,
          ls_newtab_comp        TYPE        abap_componentdescr ,
          li_newtab_comps       TYPE        abap_component_tab  ,
          ls_component          TYPE        abap_compdescr      .

   DATA : lv_header_string      TYPE string ,
          lv_length             TYPE i      .

   DATA : ref_line_header     TYPE REF TO data ,
          ref_line_data       TYPE REF TO data ,
          ref_line_newtab     TYPE REF TO data .

   FIELD-SYMBOLS : <fs_header>   TYPE ANY     ,
                   <fs_data_wa>  TYPE ANY     ,
                   <fs_data_fld> TYPE ANY     ,
                   <fs_new_tab>  TYPE table   ,
                   <fs_new_wa>   TYPE ANY     ,
                   <fs_new_fld>  TYPE ANY     .


* Get runtime information of internal tables
   lo_data_table_descr ?= cl_abap_typedescr=>describe_by_data( f_sheet_row ) .
   lo_data_line_type   ?= lo_data_table_descr->get_table_line_type(  ) .

   lo_header_table_descr ?= cl_abap_typedescr=>describe_by_data( gi_header ) .
   lo_header_line_type   ?= lo_header_table_descr->get_table_line_type( ) .

* Sanity test
* Number of lines in header table should be equal to number of
* columns in data table
   IF LINES( lo_data_line_type->components ) <> LINES( gi_header ) .
     MESSAGE 'Error' TYPE 'A'.
   ENDIF.

* Create work area of header table
   CREATE DATA ref_line_header TYPE HANDLE lo_header_line_type .
   ASSIGN ref_line_header->* TO <fs_header> .

* Create work are of data table
   CREATE DATA ref_line_data TYPE HANDLE lo_data_line_type .
   ASSIGN ref_line_data->* TO <fs_data_wa> .

* Loop on header internal table to get length of each header, based on this
* information new table will be created to accomodate data without any truncation
   LOOP AT gi_header ASSIGNING <fs_header> .
     CLEAR lv_header_string .
     lv_header_string = <fs_header> .
     lv_length        = STRLEN( lv_header_string ) .  "header text length

     READ TABLE lo_data_line_type->components INTO ls_component INDEX sy-tabix .
     CLEAR ls_newtab_comp .

*   Also get length of data field, in case that is more than length of header text
     ASSIGN COMPONENT ls_component-name OF STRUCTURE <fs_data_wa> TO <fs_data_fld> .
     lo_element_descr ?= cl_abap_typedescr=>describe_by_data( <fs_data_fld> ).
     IF lo_element_descr->output_length > lv_length .
       lv_length = lo_element_descr->output_length .
     ENDIF.

     ls_newtab_comp-name = ls_component-name .
     ls_newtab_comp-type ?= cl_abap_elemdescr=>get_c( lv_length ) .

     INSERT ls_newtab_comp INTO TABLE li_newtab_comps .
   ENDLOOP .

* Create new table based on new structure
   lo_newtab_line_descr  = cl_abap_structdescr=>create( li_newtab_comps ).
   lo_newtab_table_descr = cl_abap_tabledescr=>create( lo_newtab_line_descr ) .

   CREATE DATA ref_table TYPE HANDLE lo_newtab_table_descr .
   ASSIGN ref_table->* TO <fs_new_tab> .

* Fill header data into new table
   CREATE DATA ref_line_newtab TYPE HANDLE lo_newtab_line_descr .
   ASSIGN ref_line_newtab->* TO <fs_new_wa> .

   APPEND INITIAL LINE TO <fs_new_tab> .
   READ TABLE <fs_new_tab> ASSIGNING <fs_new_wa> INDEX sy-tabix .

   LOOP AT gi_header ASSIGNING <fs_header> .
     ASSIGN COMPONENT sy-tabix OF STRUCTURE <fs_new_wa> TO <fs_new_fld>.
     <fs_new_fld> = <fs_header> .
   ENDLOOP .

* Now fill content

   LOOP AT f_sheet_row ASSIGNING <fs_data_wa> .

     APPEND INITIAL LINE TO <fs_new_tab> .
     READ TABLE <fs_new_tab> ASSIGNING <fs_new_wa> INDEX sy-tabix .

*   Loop at components of data table
     LOOP AT lo_data_line_type->components INTO ls_component .
       ASSIGN COMPONENT ls_component-name  OF STRUCTURE <fs_data_wa> TO <fs_data_fld> .
       ASSIGN COMPONENT ls_component-name  OF STRUCTURE <fs_new_wa>  TO <fs_new_fld> .
       WRITE <fs_data_fld> TO <fs_new_fld> . "this will take care of conversion exit
     ENDLOOP.

   ENDLOOP .

ENDFORM.                    "merge_tables

 

Regards,

Pawan


Viewing all articles
Browse latest Browse all 10548

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>