Ir al contenido principal

ABAP - Celda de color en ALV OO

Está tan bueno este tuto, que literalmente me lo copio (abajo va la fuente)

Dear SAPLearners, in this tutorial we will learn how to color a particular cell in ALV using the factory class CL_SALV_TABLE.
In earlier tutorial we have added colors to row in ABAP ALV, click here to know how.
This is similar to coloring row. Lets see how we can do it.
To apply cell color,
  • Create a new column of Type LVC_T_SCOL in your output internal table.
  • Set the above column as Color Column in ALV by using the method SET_COLUMN_COLOR( ).
  • Add the color data to the color column based on your requirement.
Create a program in SE38 and copy the below code.
*&---------------------------------------------------------------------*
*&         Add color to cell in ALV using factory class                *
*&---------------------------------------------------------------------*
*&                  www.saplearners.com                                *
*&---------------------------------------------------------------------*
REPORT  zsl_oop_alv_row_color.

*----------------------------------------------------------------------*
*       CLASS lcl_sflight DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_sflight DEFINITION.

  PUBLIC SECTION.
    TYPES: BEGIN OF lty_sflight,
           carrid          TYPE s_carr_id,
           connid          TYPE s_conn_id,
           fldate          TYPE s_date,
           price           TYPE s_price,
           currency        TYPE s_currcode,
           planetype       TYPE s_planetye,
           seatsmax        TYPE s_seatsmax,
           seatsocc        TYPE s_seatsocc,
           color           TYPE lvc_t_scol,            "<< Color Column
           END OF lty_sflight.

    METHODS: get_sflight_data,
             get_alv_instance,
             set_color,
             display.

    DATA: lo_alv      TYPE REF TO cl_salv_table,
          gt_sflight  TYPE STANDARD TABLE OF lty_sflight.

ENDCLASS.                    "lcl_sflight DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_sflight IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_sflight IMPLEMENTATION.

* Get SFLIGHT data
  METHOD get_sflight_data.
    SELECT carrid connid fldate price currency planetype seatsmax
           seatsocc INTO CORRESPONDING FIELDS OF TABLE me->gt_sflight
                    FROM sflight
                    WHERE carrid = 'AA'.
  ENDMETHOD.                    "get_sflight_data

* Get ALV instance
  METHOD get_alv_instance.
    TRY.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table = lo_alv
          CHANGING
            t_table      = gt_sflight.
      CATCH cx_salv_msg.
    ENDTRY.

  ENDMETHOD.                    "get_alv_instance

* Display ALV
  METHOD display.
    CALL METHOD lo_alv->display.
  ENDMETHOD.                    "display
  METHOD set_color.
* Set color to a particular row based on your condition.
    FIELD-SYMBOLS: <lwa_sflight> TYPE lty_sflight.
    DATA: ls_color               TYPE lvc_s_scol.

    LOOP AT gt_sflight ASSIGNING <lwa_sflight>.
      IF <lwa_sflight>-seatsocc > 370.
        ls_color-fname     = 'SEATSOCC'.          "<<Specify the column name
        ls_color-color-col = 5.
        ls_color-color-int = 0.
        ls_color-color-inv = 0.
        APPEND ls_color TO <lwa_sflight>-color.
      ENDIF.
    ENDLOOP.
  ENDMETHOD.                    "set_color
ENDCLASS.                    "lcl_sflight IMPLEMENTATION


START-OF-SELECTION.
  DATA: lo_cl_sflight     TYPE REF TO lcl_sflight,
        lo_columns        TYPE REF TO cl_salv_columns_table.


  CREATE OBJECT lo_cl_sflight.

* Get the Data for ALV report
  lo_cl_sflight->get_sflight_data( ).

* Get ALV instance
  lo_cl_sflight->get_alv_instance( ).


*------------- To apply COLOR to a ROW in ALV ---------------------*
*// 1. Get List of columns
  CALL METHOD lo_cl_sflight->lo_alv->get_columns
    RECEIVING
      value = lo_columns.

*// 2. Set the Color Column to the ALV
  TRY.
      CALL METHOD lo_columns->set_color_column
        EXPORTING
          value = 'COLOR'.
    CATCH cx_salv_data_error.
  ENDTRY.

*// 3. Add color to particular row
  lo_cl_sflight->set_color( ).

* Display ALV report
  lo_cl_sflight->display( ).
https://gist.github.com/prakash13t/7e4f8b9b8d1ba2d6f178#file-cellcolor_salv-abap

ALV Output Before:

ALV Output after adding color to cells:

Comentarios

Entradas más populares de este blog

A to Z of OLE Excel in ABAP 7.4

  SAP users, both business and end users always need to download the output of a report to spreadsheet and do their analytics.   The standard excel output from a report is very simple process but it is old fashioned and the spreadsheet looks quite boring.  There is no default formatting and the users have to do all the hard work of changing the fonts, coloring the texts, marking the borders etc. Updated 16th Aug 2019  –  If you are working in non-ABAP 7.4 (below), there is complete reference program for you too. Go to the end of this article.  Thank you  Legxis  ( LeonievonK ) for the share. I acknowledge, whatever I mentioned above can be achieved in many ways programmatically. We can do it in the old traditional ABAP way but  providing multiple tabs in the spreadsheet and formatting is quite tricky with non  OLE  method. OLE = Object Linking and Embedding The high level agenda of this article is to be the  G.O.A.T.  (please google it if you do not know the full form)  of OLE Excel Gu
How to add custom field in Additional B Tab for SAP Sales Order In this article we want to explain step by step how to add custom field in SAP Sales Order transaction VA01/VA02/VA03. This time Mr ABAPGurus will give the tutorial of enhancement to add custom field in SAP Sales order. In this sample we will add new custom field for comments and customer satisfaction, this data will save into separate table from SAP Standard table ( VBAK / VBAP ). SAP provided us with ADDITIONAL TAB in the sales order transaction ( VA01/VA02/VA03 ) which allow customer to add custom fields.  T he different between ADDITIONAL A and ADDITIONAL B is the ADDITIONAL A for field which already predefine in Sales Order Header ( VBAK ) fields and ADDITIONAL B is for field that freely define. This sample we will use ADDITIONAL B because we will store data from customer satisfaction into Z database table. 1. Create one Z table using SE11 SAP Transaction code. 2.Using SE38 Transaction code and ope

VOFM

Creating VOFM custom routine, functionality same as SAP standard routine 103 with Additional Functionality 1.    Introduction 1.1   Document Purpose 1.2    This is the document helps you to create custom routine for sales order for below specific requirement: Functional description: In case of sale order with the SD document category ‘K’ or ‘L’ or ‘H’ related to an invoice, we need to fill the fixed value date (VBKD_VALDT) with the billing reference date (VBRK_FKDAT). For example: Invoice 90001111, the billing date is 22.07.2013 If we create a sale order related to this invoice, we want the fixed value date equal to the billing reference date. As per the requirement: Sap standard routine ‘103’ triggers whenever sales order is being created with reference. Sap standard routine is configured in VOFM transaction as: Sap standard routine ‘103’ has the functionality of copying the billing document header data and billing document line item data into the