Cell Color in ABAP ALV Grid Report

0
3027

[adsenseyu2]

Hello everyone, In this tutorial we will learn how to do cell color in ABAP ALV Grid Report.

Step-by-Step Procedure

  1. Create an ABAP Program in SE38 transaction.
  2. Add an field called “CELLCOLOR” of type LVC_T_SCOL(table type) in your final internal table.
  3. Populate the field CELLCOLOR based on your business requirement.
  4. Assign CELLCOLOR to COLTAB_FIELDNAME of ALV Grid Layout structure.
  5. Create an ALV report using the function module REUSE_ALV_GRID_DISPLAY.

Business Scenario

In this demo example, we want to color the cell when quantity of purchase order is less than the 5.00.

Code

*-------------------------------------------------------------*
*                 www.saplearners.com                         *
*-------------------------------------------------------------*
* Cell Color in ABAP ALV Report                               *
*-------------------------------------------------------------*

*--- Table declaration
TABLES: ekko.


*-- Type pool declaration
TYPE-POOLS: slis.


*--- Selection screen
SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.


*--- Type declaration
TYPES: BEGIN OF lty_ekpo,
       ebeln  TYPE char30,  " Document no.
       ebelp  TYPE ebelp,   " Item no
       matnr  TYPE matnr,   " Material no
       werks  TYPE werks_d, " Plant
       ntgew  TYPE entge,   " Net weight
       gewei  TYPE egewe,   " Unit of weight
       cellcolor TYPE lvc_t_scol, "Cell Color
       END OF lty_ekpo.


*--- Internal table declaration
DATA: lt_ekpo            TYPE STANDARD TABLE OF lty_ekpo,
      lt_fieldcat        TYPE slis_t_fieldcat_alv.


*--- Work area declaration
DATA: wa_ekpo            TYPE lty_ekpo,
      wa_layout          TYPE slis_layout_alv,
      wa_cellcolor       TYPE lvc_s_scol,
      lv_index           TYPE sy-tabix.


*--- Start-of-selection event
START-OF-SELECTION.

* Select data from ekpo
  SELECT ebeln " Doc no
         ebelp " Item
         matnr " Material
         werks " Plant
         ntgew " Quantity
         gewei " Unit
         FROM ekpo
         INTO CORRESPONDING FIELDS OF TABLE lt_ekpo
         WHERE ebeln IN s_ebeln
         AND ntgew NE '0.00'.
  IF sy-subrc = 0.
    SORT lt_ekpo BY ebeln ebelp matnr .
  ENDIF.

*--- Set Cell colors
*    COL = 1 to 7,
*    INT = 1 = Intensified on, 0 = Intensified off
*    INV = 1 = Text Color, 0 = Background Color

* Color the Quantity cell where quantity is less than 5
   LOOP AT lt_ekpo INTO wa_ekpo.
    lv_index = sy-tabix.
  IF wa_ekpo-ntgew < 5.
    wa_cellcolor-fname = 'NTGEW'.
    wa_cellcolor-color-col = 6.
    wa_cellcolor-color-int = '1'.
    wa_cellcolor-color-inv = '0'.
    APPEND wa_cellcolor TO wa_ekpo-cellcolor.
    CLEAR: wa_cellcolor.
    MODIFY lt_ekpo FROM wa_ekpo INDEX lv_index TRANSPORTING cellcolor.
      ENDIF.
   ENDLOOP.

*--- Field Catalog
  PERFORM f_field_catalog.

*--- Layout
  PERFORM f_build_layout.


END-OF-SELECTION.

* Perform to display ALV report
  PERFORM f_alv_report_display.


*&---------------------------------------------------------------------*
*&      Form  sub_field_catalog
*&---------------------------------------------------------------------*
*       Build Field Catalog
*----------------------------------------------------------------------*
*       No Parameter
*----------------------------------------------------------------------*
FORM f_field_catalog .

  DATA: lwa_fcat TYPE slis_fieldcat_alv.

*  Build Field Catalog
  lwa_fcat-col_pos        =  1.           "Column
  lwa_fcat-fieldname      =  'EBELN'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'Doc. No'.    "Field Text
  APPEND lwa_fcat TO lt_fieldcat.

  lwa_fcat-col_pos        =  2.           "Column
  lwa_fcat-fieldname      =  'EBELP'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'Item No'.   "Field Text
  APPEND lwa_fcat TO lt_fieldcat.
  CLEAR:lwa_fcat.


  lwa_fcat-col_pos        =  3.           "Column
  lwa_fcat-fieldname      =  'WERKS'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'Plant'.     "Field Text
  APPEND lwa_fcat TO lt_fieldcat.
  CLEAR:lwa_fcat.

  lwa_fcat-col_pos        =  4.           "Column
  lwa_fcat-fieldname      =  'MATNR'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'Material'.  "Field Text
  APPEND lwa_fcat TO lt_fieldcat.
  CLEAR:lwa_fcat.

  lwa_fcat-col_pos        =  5.           "Column
  lwa_fcat-fieldname      =  'NTGEW'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'Quantity'.  "Field Text
  lwa_fcat-do_sum         = 'X'.          "Sum
  APPEND lwa_fcat TO lt_fieldcat.
  CLEAR:lwa_fcat.

  lwa_fcat-col_pos        =  6.           "Column
  lwa_fcat-fieldname      =  'GEWEI'.     "Field Name
  lwa_fcat-tabname        =  'LT_EKPO'.   "Internal Table Name
  lwa_fcat-seltext_l      =  'UOM'.       "Field Text
  APPEND lwa_fcat TO lt_fieldcat.
  CLEAR:lwa_fcat.
  ENDFORM.                    " sub_field_catalog

*&---------------------------------------------------------------------*
*&      Form  f_populate_layout
*&---------------------------------------------------------------------*
*       Populate ALV layout
*----------------------------------------------------------------------*
*       No Parameter
*----------------------------------------------------------------------*
FORM f_build_layout.

  CLEAR wa_layout.
  wa_layout-colwidth_optimize = 'X'.         " Optimization of Col width
  wa_layout-coltab_fieldname = 'CELLCOLOR'.  " Cell color Column Name

ENDFORM.                    " f_populate_layout
*&---------------------------------------------------------------------*
*&      Form  f_alv_report_display
*&---------------------------------------------------------------------*
FORM f_alv_report_display .

* ALV report
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = sy-repid
      is_layout                = wa_layout
      it_fieldcat              = lt_fieldcat
    TABLES
      t_outtab                 = lt_ekpo
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc <> 0.
  ENDIF.

ENDFORM.                    " f_alv_report_display

 Output

[adsenseyu1]

Cell Color in ABAP ALV Grid