2
votes

I'm trying to clean the following empty cells marked in red from this internal table before I display it in an ALV.

If a cell is found to be blank, look for any cells underneath that have value and move up.

I am struggling to figure out what is the best way in code to perform this.

Any help would be great.

Before and After

2
Do you get the data in this form or do you get separate tables for left and right? - vwegert
seperate tables. - Malorrr
Then there's something wrong with your merging logic. Please add a MCVE that includes your code for further investigation. - vwegert

2 Answers

1
votes

It is undoubtedly that something is wrong with your merging logic, however your task is quite interesting and this is one of the possible ways it can be solved.

I took your structure and made an assumption that none of the rows in your table is fully filled, i.e. either first three columns are filled (struct_left) or last three (struct_right). This is how I feel it from your screenshots.

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
votes

You can sort the internal table and store it in a temp internal table, and swap them. For instance:

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[].

OR, you can loop through the fieldcatalog, set "no_display" or "no_out" field to 'X'.