SAP/ABAP

Get Structure/Table sub-components

Denise 2014. 9. 12. 10:26

1. 인터널 테이블이나 스트럭쳐의 필드리스트를 가져올 때 사용. 

2. 용도 : 동적으로 필드제어가 필요할때. ( ex> 일별 데이터 출력, 집계, 음수 표시, 특정필드 Enabled/Disabled... )
FUNCTION zdk_get_components .
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     REFERENCE(IT_DATA) TYPE  ANY
*"     REFERENCE(IV_INCLUDE_ONLY) TYPE  CHAR01 OPTIONAL
*"  EXPORTING
*"     REFERENCE(ET_COMPS) TYPE  ABAP_COMPONENT_TAB
*"----------------------------------------------------------------------

  DATA : lo_tadesc TYPE REF TO cl_abap_tabledescr
       , lo_stdesc TYPE REF TO cl_abap_structdescr.
  DATA : lv_type   TYPE c.

  CLEAR : et_comps[].
  DESCRIBE FIELD it_data TYPE lv_type.

  CASE lv_type.
    WHEN 'h'.
      lo_tadesc ?= cl_abap_typedescr=>describe_by_data( it_data ).
      lo_stdesc ?= lo_tadesc->get_table_line_type( ).
    WHEN 'u' OR 'v'.
      lo_stdesc ?= cl_abap_typedescr=>describe_by_data( it_data ).
  ENDCASE.

  PERFORM get_subcomponent USING    lo_stdesc
                                    iv_include_only
                           CHANGING et_comps[].

ENDFUNCTION.
*&---------------------------------------------------------------------*
*&      form  get_subcomponent
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->STRUCTDESCR  text
*      -->LT_COMPS     text
*----------------------------------------------------------------------*
FORM get_subcomponent USING    po_descr
                                 TYPE REF TO cl_abap_structdescr
                               pv_include TYPE char01
                      CHANGING pt_comps   TYPE abap_component_tab.

  DATA : lo_tadesc     TYPE REF TO cl_abap_tabledescr
       , lo_stdesc     TYPE REF TO cl_abap_structdescr
       , lo_tydesc     TYPE REF TO cl_abap_typedescr.

  DATA : lt_comps      LIKE pt_comps WITH HEADER LINE
       , lt_comps_sub  LIKE pt_comps WITH HEADER LINE
       , ls_comps      LIKE LINE OF pt_comps.

  DATA : lv_class      TYPE abap_abstypename.

  CHECK po_descr IS NOT INITIAL.

  lt_comps[] = po_descr->get_components( ).

  LOOP AT lt_comps.
    IF pv_include IS NOT INITIAL.
      CASE lt_comps-as_include.
        WHEN 'X'.
          PERFORM get_line_desc    USING    lt_comps-type
                                   CHANGING lo_stdesc.
          PERFORM get_subcomponent USING    lo_stdesc
                                            pv_include
                                   CHANGING pt_comps.
        WHEN OTHERS.
          APPEND lt_comps TO pt_comps.
      ENDCASE.
    ELSE.
      PERFORM get_class_name USING    lt_comps-type
                             CHANGING lv_class.

      CASE lv_class.
        WHEN 'CL_ABAP_STRUCTDESCR'
          OR 'CL_ABAP_TABLEDESCR'.
          PERFORM get_line_desc    USING    lt_comps-type
                                   CHANGING lo_stdesc.
          PERFORM get_subcomponent USING    lo_stdesc
                                            pv_include
                                   CHANGING pt_comps.
        WHEN OTHERS.
          APPEND lt_comps TO pt_comps.
      ENDCASE.
    ENDIF.
  ENDLOOP.

ENDFORM.   "get_subcomponent
*&---------------------------------------------------------------------*
*&      Form  get_line_desc
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PO_TYPEDESC  text
*      -->PO_LINEDESC  text
*----------------------------------------------------------------------*
FORM get_line_desc  USING    po_type  TYPE REF TO cl_abap_datadescr
                    CHANGING po_line  TYPE REF TO cl_abap_structdescr.

  DATA : lv_class      TYPE abap_abstypename.
  DATA : lo_tadesc TYPE REF TO cl_abap_tabledescr.
  PERFORM get_class_name USING    po_type
  CHANGING lv_class.

  CASE lv_class.
    WHEN 'CL_ABAP_STRUCTDESCR'.
      po_line   ?= po_type.
    WHEN 'CL_ABAP_TABLEDESCR'.
      lo_tadesc ?= po_type.
      po_line   ?= lo_tadesc->get_table_line_type( ).
    WHEN OTHERS.
      EXIT.
  ENDCASE.

ENDFORM.   "get_line_desc
*&---------------------------------------------------------------------*
*&      Form  get_class_name
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->PO_TYPE    text 
*      -->PV_CLASS   text
*----------------------------------------------------------------------*
FORM get_class_name  USING    po_type  TYPE REF TO cl_abap_datadescr
                     CHANGING pv_class.

  DATA : lv_class      TYPE abap_abstypename.
  lv_class = cl_abap_classdescr=>get_class_name( po_type ).
  REPLACE FIRST OCCURRENCE OF '\CLASS=' IN lv_class
  WITH space.
  CONDENSE lv_class.
  pv_class = lv_class.

ENDFORM.            "get_class_name

 

 

'SAP > ABAP' 카테고리의 다른 글

Convert Internal date to External date ( date, month )  (0) 2014.09.24
Function for Stock  (0) 2014.09.19
Excel DATEVALUE to DATE conversion  (0) 2014.08.29
excel download ( OLE based )  (0) 2014.02.18
[Macro] Range 선언  (0) 2014.02.18