ITAB所有字段值累加模版

REPORT z_jason_test_addfield .

DATA: BEGIN OF itab OCCURS 0,
      f1 TYPE i,
      f2 TYPE i,
      f3 TYPE i,
      f4 TYPE i,
      END OF itab.
DATA: total TYPE i.
PERFORM. writeitab.
PERFORM. addfield TABLES itab
                 CHANGING total.
WRITE:/ total.
*---------------------------------------------------------------------*
*       FORM. writeitab                                                *
*---------------------------------------------------------------------*
FORM. writeitab.
  itab-f1 = 1.
  itab-f2 = 2.
  itab-f3 = 3.
  itab-f4 = 4.
  APPEND itab.
  itab-f1 = 2.
  itab-f2 = 3.
  itab-f3 = 4.
  itab-f4 = 5.
  APPEND itab.
  itab-f1 = 2.
  itab-f2 = 3.
  itab-f3 = 4.
  itab-f4 = 5.
  APPEND itab.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  addfield
*&---------------------------------------------------------------------*
FORM. addfield TABLES itab
              CHANGING tmp TYPE i.
  DATA: BEGIN OF headtab OCCURS 0 ,
          length    TYPE i ,
          decimals  TYPE i,
          type_kind TYPE c,
          name(30)  TYPE c,
        END OF headtab.
  DATA: sum TYPE i,
        n TYPE i,
        count TYPE i ,
        count2 LIKE count,
        descr_ref TYPE REF TO cl_abap_structdescr.
  FIELD-SYMBOLS: TYPE abap_compdescr .
  FIELD-SYMBOLS TYPE ANY.
  descr_ref ?= cl_abap_typedescr=>describe_by_data( itab ).
  LOOP AT descr_ref->components ASSIGNING .
    MOVE-CORRESPONDING TO headtab.
    APPEND headtab.
  ENDLOOP.
  DESCRIBE TABLE headtab LINES n.
  LOOP AT itab.
    count = n.
    DO count TIMES.
      ASSIGN COMPONENT count OF STRUCTURE itab TO .
      sum = sum + .
      count = count - 1.
    ENDDO.
    total = total + sum.
    CLEAR sum.
  ENDLOOP.
ENDFORM.                    " addfield
请使用浏览器的分享功能分享到微信等