0

我目前正在尝试使用动态创建的内部表创建报告(每次的列数可能不同)。有没有办法在填充给定表的结构时处理生成的列?

这是我正在使用的代码:

FIELD-SYMBOLS: <fcat>         TYPE lvc_s_fcat,
              <fcat_aus>     TYPE ANY TABLE.

   IF so_datum-high <> ''.
     DATA(lv_month_diff) = so_datum-high - so_datum-low.
   ELSE.
     DATA(lv_month) = so_datum-low.
   ENDIF.

   APPEND INITIAL LINE TO gt_fcat ASSIGNING <fcat>.
   <fcat>-fieldname = 'MATNR'.
   <fcat>-tabname   = 'GZ_TABLE'.
   <fcat>-ref_field = 'MATNR'.
   <fcat>-ref_table = 'MAKT'.

   APPEND INITIAL LINE TO gt_fcat ASSIGNING <fcat>.
   <fcat>-fieldname = 'MAKTX'.
   <fcat>-tabname   = 'GZ_TABLE'.
   <fcat>-ref_field = 'MAKTX'.
   <fcat>-ref_table = 'MAKT'.

   DATA(lv_counter) = 1.

   DO 10 TIMES.
     DATA(lv_fieldname_qt)  = 'MOQ' &&  lv_counter.
     DATA(lv_fieldname_fqt) = 'MFQ' &&  lv_counter.

     lv_counter = lv_counter + 1.

     APPEND INITIAL LINE TO gt_fcat ASSIGNING <fcat>.
     <fcat>-fieldname = lv_fieldname_qt.
     <fcat>-tabname   = 'GZ_TABLE'.
     <fcat>-ref_field = 'MNG01'.
     <fcat>-ref_table = 'MDEZ'.

     APPEND INITIAL LINE TO gt_fcat ASSIGNING <fcat>.
     <fcat>-fieldname = lv_fieldname_fqt.
     <fcat>-tabname   = 'GZ_TABLE'.
     <fcat>-ref_field = 'MNG01'.
     <fcat>-ref_table = 'MDEZ'.

   ENDDO.

 CALL METHOD cl_alv_table_create=>create_dynamic_table
   EXPORTING
     it_fieldcatalog           = gt_fcat
   IMPORTING
     ep_table                  = gz_table
   EXCEPTIONS
     generate_subpool_dir_full = 1
     OTHERS                    = 2.

 ASSIGN gz_table->* to <fcat_aus>. 

也许你们中的一个人有一个想法。提前致谢!

4

2 回答 2

1

使用ASSIGN COMPONENT语句动态访问结构组件。查看 ABAP 文档 (F1) 了解更多详细信息。您可以通过索引或字段名称指定组件。

于 2020-12-18T10:59:38.007 回答
0

这是一个完成 Thomas 回答的示例(请注意,我没有解释如何动态创建表,CREATE DATA因为这不是您的问题,这里它是用 2 个组件静态创建的,我只解释如何通过引用它来填充内部表动态):

TYPES: BEGIN OF ty_line,
         comp1 TYPE i,
         text_component TYPE string,
       END OF ty_line,
       ty_itab TYPE STANDARD TABLE OF ty_line WITH DEFAULT KEY.
DATA r_itab TYPE REF TO DATA.
DATA r_line TYPE REF TO DATA.
FIELD-SYMBOLS <itab> TYPE STANDARD TABLE.
FIELD-SYMBOLS <line> TYPE ANY.
FIELD-SYMBOLS <component> TYPE ANY.

" create an internal table, here it's the static way, but you may also use CREATE DATA 
" to create it dynamically
CREATE DATA r_itab TYPE ty_itab.
ASSIGN r_itab->* TO <itab>.

" now let's fill it dynamically, first define a line
CREATE DATA r_line LIKE LINE OF <itab>.
ASSIGN r_line->* TO <line>.
ASSIGN COMPONENT 1 OF <line> TO <component>. " access to COMP1 component
IF sy-subrc = 0.
  <component> = 30.
ENDIF.
ASSIGN COMPONENT 'TEXT_COMPONENT' OF <line> TO <component>.
IF sy-subrc = 0.
  <component> = 'text'.
ENDIF.

" now add the line
INSERT <line> INTO TABLE <itab>.

请注意,可以访问整行ASSIGN COMPONENT 0 ...(如果内部表没有组件,则特别有用)。

更多信息:

于 2020-12-18T19:28:11.400 回答