清理内部表中的空单元格

时间:2017-07-11 13:59:31

标签: abap

在我在ALV中显示之前,我试图清除以下内部表中标有红色的空单元格。

如果发现一个单元格为空白,请查找下面有值的单元格并向上移动。

我正在努力弄清楚代码执行此操作的最佳方式。

任何帮助都会很棒。

Before and After

2 个答案:

答案 0 :(得分:1)

毫无疑问,你的合并逻辑出了问题,但是你的任务非常有趣,这是可以解决的一种方法。

我采用了您的结构,并假设表中行的已完全填充,即 前三列已填充(struct_left最后三个(struct_right)。这就是我从你的截图中感受到的。

REPORT z_sections.
TYPES:
      BEGIN OF struct_left,  " left structure
        LEFTDAMAGED TYPE c LENGTH 1,
        LEFTDAMAGEDDESC TYPE c LENGTH 3,
        LEFTDAMAGEDDESCT TYPE c LENGTH 30,
      END OF struct_left,
      BEGIN OF struct_right,   " right structure
        RIGHTDAMAGED TYPE c LENGTH 1,
        RIGHTDAMAGEDDESC TYPE c LENGTH 3,
        RIGHTDAMAGEDDESCT TYPE c LENGTH 30,
      END OF STRUCT_right.
      TYPES BEGIN OF ty_table.
        INCLUDE TYPE struct_left.
        INCLUDE TYPE struct_right.
       TYPES END OF ty_table.

DATA: lt_current_table TYPE TABLE OF ty_table INITIAL SIZE 100,
      ls_current_table LIKE LINE OF lt_current_table,
      i TYPE i.

FIELD-SYMBOLS: <fld> TYPE clike.
DATA: r_random TYPE REF TO cl_abap_random_packed,
      seed      TYPE i.

seed = cl_abap_random=>seed( ).
CALL METHOD cl_abap_random_packed=>create
  EXPORTING
    seed = seed
    min  = -999999999999999
    max  = 999999999999999
  RECEIVING
    prng = r_random.

DEFINE randomize. " filling row with random data
    ASSIGN COMPONENT &1 OF STRUCTURE &2 TO <fld>.
    <fld> = r_random->get_next( ).
    &1 = &1 + 1.
    ASSIGN COMPONENT &1 OF STRUCTURE &2 TO <fld>.
    <fld> = r_random->get_next( ).
    &1 = &1 + 1.
    ASSIGN COMPONENT &1 OF STRUCTURE &2 TO <fld>.
    <fld> = r_random->get_next( ).
END-OF-DEFINITION.

START-OF-SELECTION.
* filling table with random stuff
DO 100 TIMES.
    CLEAR ls_current_table.
    IF sy-index MOD 3 = 0.
        i = 1.
        randomize i ls_current_table.
    ELSE.
        i = 4.
        randomize i ls_current_table.
    ENDIF.
    APPEND ls_current_table TO lt_current_table.
ENDDO.

DATA: ls_left TYPE struct_left,
      ls_right TYPE struct_right.

DATA lt_new LIKE lt_current_table.

* collapsing table
LOOP AT lt_current_table ASSIGNING FIELD-SYMBOL(<fs_current>) WHERE leftdamaged IS NOT INITIAL.
      DELETE lt_current_table WHERE leftdamaged IS INITIAL AND leftdamageddesc IS INITIAL AND leftdamageddesct IS INITIAL AND
      rightdamaged IS INITIAL AND rightdamageddesc IS INITIAL AND rightdamageddesct IS INITIAL. " remove empty lines
      MOVE-CORRESPONDING <fs_current> TO ls_left.

      READ TABLE lt_current_table ASSIGNING FIELD-SYMBOL(<fs_right>) WITH KEY leftdamaged = ''.
      IF <fs_right> IS ASSIGNED.
        MOVE-CORRESPONDING <fs_right> TO ls_right.
        CLEAR: <fs_right>.
      ENDIF.
      CLEAR: <fs_current>.

      IF ls_left IS NOT INITIAL AND ls_right IS NOT INITIAL.
        CLEAR: ls_current_table.
        MOVE-CORRESPONDING ls_left TO ls_current_table.
        MOVE-CORRESPONDING ls_right TO ls_current_table.
        APPEND ls_current_table TO lt_new.
        CLEAR: ls_left, ls_right.
       ENDIF.
ENDLOOP.

答案 1 :(得分:-1)

您可以对内部表进行排序并将其存储在临时内部表中,然后进行交换。例如:

data: lt_itab_temp like table of lt_itab.
move lt_itab[] to lt_itab_temp[].
clear:lt_itab[],lt_itab.
sort lt_itab_temp descending by rightdamagedesc rightdamagedesct.
move lt_itab_temp[] to lt_itab[].

或者,您可以遍历fieldcatalog,设置&#34; no_display&#34;或&#34; no_out&#34;字段到&#39; X&#39;。