Copyright 2024 - BV TallVision IT

Assign a color to your cell in ALV and access another dimension to ALV reporting. With easy ALV it's even easier: 

Setting colors is done by passing an extra attribute with color information to the ALV class. Color information is arranged per field and per line. Effectively any cell can be assigned a color. When whole rows are to be painted, all the fields on the row should be assigned their color. And the same applies to column coloring: simply assign the field (cell) color on every line in the report. Focus on the COLORS field which is available in the data table, hence color assignments become part of the data (colors type lvc_t_scol,).

The demo report output:

The source code for this demo report:

report ZDEMO_ALV_6_CELL_COLORS.

include ZABAPCADABRA_EASY_ALV.
types: begin of ty_content,
         SPRSL type t100-SPRSL,
         ARBGB type t100-ARBGB,
         MSGNR type t100-MSGNR,
         TEXT type t100-TEXT,
         color_txt type string,
         colors type lvc_t_scol,
       end of ty_content.
data: gt_data type table of ty_content,
      gw_data type ty_content,
      gw_color_field type lvc_s_scol,
      gw_color type lvc_s_colo,
      go_alv type ref to lcl_easy_alv,               "Define
      go_salv type ref to cl_salv_table,
      go_columns_table type ref to cl_salv_columns_table.

start-of-selection.

  select * from t100
    up to 20 rows
    into CORRESPONDING FIELDS OF table gt_data.

* Set up test data, and assign colors to individual fields
  data: lv_single_character type c.
  loop at gt_data into gw_data.
    lv_single_character = sy-tabix mod 4.
    clear: gw_color_field, gw_color.
    gw_color_field-fname = 'SPRSL'.
    gw_color-col = sy-tabix mod 8.
    gw_color_field-color = gw_color.
    append gw_color_field to gw_data-colors.
    gw_color_field-fname = 'TEXT'.
    gw_color-int = 1.
    gw_color_field-color = gw_color.
    append gw_color_field to gw_data-colors.
    gw_color_field-fname = 'COLOR_TXT'.
    gw_color-int = 0.
    gw_color-inv = 1.
    gw_color_field-color = gw_color.
    append gw_color_field to gw_data-colors.

    gw_data-color_txt = gw_color-col.
    concatenate 'Colornumber = ' gw_data-color_txt
      into gw_data-color_txt separated by space.

    modify gt_data from gw_data.
  endloop.

  create object go_alv.                                    "Create
  go_alv->set_alv_from_template(
    changing content = gt_data
             salv_table = go_salv ).
* A bit of logic to inform the ALV object which column is to be
* used for color settings:
      try.
        go_columns_table = go_salv->get_columns( ).
        go_columns_table->set_color_column( value = 'COLORS' ).
      catch cx_salv_data_error.
* Field was not valid
    endtry.

  go_alv->set_header_line( 'Please note: information indicated' ).
  go_alv->set_header_line( 'by color should be available in an ').
  go_alv->set_header_line( 'alternative way as well ! ' ).
  go_alv->display( ).                                      "Report