Skip to content

Instantly share code, notes, and snippets.

@Gong-Bao-Chicken
Created February 26, 2025 08:45
Show Gist options
  • Save Gong-Bao-Chicken/037dc0b7d0868548d0b6d816e69e59d6 to your computer and use it in GitHub Desktop.
Save Gong-Bao-Chicken/037dc0b7d0868548d0b6d816e69e59d6 to your computer and use it in GitHub Desktop.
Colors of SAP ALV Grid Cells
*&---------------------------------------------------------------------*
*& Report z_print_alv_cell_colors
*&---------------------------------------------------------------------*
*& Report to print all alv cell colors
*&---------------------------------------------------------------------*
REPORT z_print_alv_cell_colors.
TYPES:
BEGIN OF ts_data,
name TYPE string, " name of constant
col TYPE lvc_col, " color 1-7
int TYPE lvc_int, " intensified 0-1
inv TYPE lvc_inv, " inverse 0-1
scol TYPE lvc_t_scol, " table for cell coloring
END OF ts_data,
tt_data TYPE TABLE OF ts_data.
DATA:
ls_data TYPE ts_data,
lt_data TYPE tt_data,
lv_index TYPE i,
ls_color TYPE lvc_s_scol,
lt_color TYPE lvc_t_scol,
lo_table TYPE REF TO cl_salv_table,
lo_columns TYPE REF TO cl_salv_columns_table,
lo_column TYPE REF TO cl_salv_column_list.
* rows with possible colors
DO 8 TIMES.
ls_color-color-col = sy-index - 1.
DO 2 TIMES.
lv_index = sy-index - 1.
ls_color-color-int = lv_index. " 0-1
DO 1 TIMES.
lv_index = sy-index - 1.
ls_color-color-inv = lv_index. " 0-1
APPEND ls_color TO lt_color.
CASE ls_color-color-col.
WHEN 0. ls_data-name = 'COL_BACKGROUND '.
WHEN 1. ls_data-name = 'COL_HEADING '.
WHEN 2. ls_data-name = 'COL_NORMAL '.
WHEN 3. ls_data-name = 'COL_TOTAL '.
WHEN 4. ls_data-name = 'COL_KEY '.
WHEN 5. ls_data-name = 'COL_POSITIVE '.
WHEN 6. ls_data-name = 'COL_NEGATIVE '.
WHEN 7. ls_data-name = 'COL_GROUP '.
ENDCASE.
ls_data-col = ls_color-color-col.
ls_data-int = ls_color-color-int.
ls_data-inv = ls_color-color-inv.
ls_data-scol = lt_color.
APPEND ls_data TO lt_data.
CLEAR lt_color.
ENDDO.
ENDDO.
ENDDO.
* Create alv
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = lo_table
CHANGING
t_table = lt_data.
lo_columns = lo_table->get_columns( ).
* set description of column
lo_column ?= lo_columns->get_column( 'COL' ).
lo_column->set_short_text( 'COL' ).
lo_column ?= lo_columns->get_column( 'INT' ).
lo_column->set_short_text( 'INT' ).
lo_column ?= lo_columns->get_column( 'INV' ).
lo_column->set_short_text( 'INV' ).
* set the column with the information about color of rows and fields
lo_columns->set_color_column( 'SCOL' ).
lo_table->display( ).
CATCH cx_salv_msg. " cl_salv_table=>factory
WRITE: / 'cx_salv_msg exception'.
STOP.
CATCH cx_salv_data_error. " cl_salv_filters->add_filter()
WRITE: / 'cx_salv_data_error'.
STOP.
CATCH cx_salv_not_found. " cl_salv_columns_table->get_column()
WRITE: / 'cx_salv_not_found exception'.
STOP.
ENDTRY.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment