从字段符号中获取字段名

Getting the fieldnames from the field-symbols

我需要获取field-symbol中的字段名称<itab>,以便我可以将名称用于ALV的字段目录。

所以我使用了 cl_abap_structdescr 但它总是给我一个错误。 我用内部 table 尝试了这个,我得到了预期的结果,但我必须使用字段符号而不是内部 table.

ASSIGN lo_itab->* TO <itab>

data: go_struct type ref to cl_abap_structdescr,
      gt_comp   type abap_component_tab,
      gs_comp   type abap_componentdescr.


  go_struct ?= cl_abap_typedescr=>describe_by_data( <itab> ).
  gt_comp = go_struct->get_components( ).

  loop at gt_comp into gs_comp.

      PERFORM fill_fieldcat USING : 
     gs_comp-name      ''       gs_comp-name
   .
  endloop.

这是错误;

当您将引用类型分配给另一个引用类型时,您会得到转储。定义结构类型并传递代替,就像我在下面的示例中所做的那样。你不会得到任何转储。

data: go_struct type ref to cl_abap_structdescr,
      gt_comp   type abap_component_tab,
      gs_comp   type abap_componentdescr.

  DATA ls_spfli TYPE spfli.
  go_struct ?= cl_abap_typedescr=>describe_by_data( ls_spfli ).
  gt_comp = go_struct->get_components( ).

  loop at gt_comp into gs_comp.

*      PERFORM fill_fieldcat USING :
*     gs_comp-name      ''       gs_comp-name
*   .
  endloop.

因为<itab>显然是一个内部的table,它的类型是"table",而不是"structure"! (另见简短转储,它说 describe_by_data 返回了一个类型 cl_abap_tabledescr,它与目标 go_struct 的类型不兼容,即 cl_abap_structdescr

所以你必须先获取它的table类型,然后获取它的行的类型(我这里假设它是一个结构化类型,但在某些其他情况下可能是其他类型)。

data: go_table type ref to cl_abap_tabledescr.
      go_struct type ref to cl_abap_structdescr,
      gt_comp   type abap_component_tab,
      gs_comp   type abap_componentdescr.

go_table ?= cl_abap_typedescr=>describe_by_data( <itab> ).
go_struct ?= go_table->get_table_line_type( ).
gt_comp = go_struct->get_components( ).
...