Write spool in color

Write spool in color

Create spool to display it later in SP01, SP02…

With code below, you can easily add lines in spool like that :


    ls_text-color = c_color_jaune.
    CONCATENATE 'Error on PN' lw_matnr INTO ls_text-text SEPARATED BY space.
    APPEND ls_text TO gt_text.

    CLEAR ls_text.
    ls_text-color = c_color_rouge.
    APPEND ls_text TO gt_text.

Use code below :

Types, constants and data used :

To be declared in TOP include.


CONSTANTS: c_color_jaune    TYPE c            VALUE 'J',
           c_color_rouge    TYPE c            VALUE 'R',
           c_color_vert     TYPE c            VALUE 'V'.

TYPES: BEGIN OF ty_text_spool,
          color TYPE c,
          text  TYPE char512,
       END OF ty_text_spool,
     t_ty_text_spool TYPE TABLE OF ty_text_spool.

DATA : gs_text                TYPE ty_text_spool,
       gt_text                TYPE t_ty_text_spool.

Create spool


FORM create_spool  USING    uw_titleline  TYPE  tsp01-rqtitle
                   CHANGING cw_spoolid    TYPE  tsp01-rqident
                            cw_handle     TYPE  sy-tabix.

  CONSTANTS : lc_destination TYPE tsp01-rqdest  VALUE 'LOCL'.

  "Ouverture du spool
  CALL FUNCTION 'RSPO_SR_OPEN'
   EXPORTING
     dest                  = lc_destination
   layout                 = 'X_65_80'
     titleline             = uw_titleline
*   LIFETIME               = '8'
   doctype                = 'LIST'
   IMPORTING
     handle                =  cw_handle
     spoolid               =  cw_spoolid
* TABLES
*   ATTRIBUTES             =
   EXCEPTIONS
     device_missing         = 1
     name_twice             = 2
     no_such_device         = 3
     operation_failed       = 4
     OTHERS                 = 5
            .
  IF sy-subrc <> 0.
    CLEAR cw_spoolid.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ENDIF.

ENDFORM.                    " CREATE_SPOOL

Add messages


FORM add_message_spool  USING    uw_handle  TYPE  sy-tabix
                                 ut_text    TYPE  t_ty_text_spool.

  DATA : ls_text TYPE ty_text_spool .

  "Mise en place du caractère spé
  DATA :  lraw_unicode(2886)  TYPE x,
          lraw_unicode_length TYPE int2,
          lw_caractere_spe    TYPE char255.


  "Classe de conversion x to char ou char to x
  DATA    abap_2_unicode      TYPE REF TO cl_abap_conv_out_ce.
  DATA :  unicode_2_abap      TYPE REF TO cl_abap_conv_in_ce,
          erreur              TYPE REF TO cx_root,

          lw_string           TYPE char255,
          lw_xstring          TYPE x255.

  FIELD-SYMBOLS <l_x> TYPE x.


**********************************************************************
* Mise en place des valeurs pour convertion x/char
**********************************************************************
  "Pour insérer de la couleur, il faut passer par l'hexadécimal
  "1ier Etape - Convertir le caractère spécial # pour montrer que c'est à traiter en couleur
  lraw_unicode = 'FFF8'.
  lraw_unicode_length = 2.
  ASSIGN lraw_unicode(lraw_unicode_length) TO <l_x>.

  "Création de la classe pour x to string avec le codepage à utiliser
  TRY.
      unicode_2_abap = cl_abap_conv_in_ce=>create(
              encoding = '4103' "Unicode 16BE, see table TCP00
              ).
    CATCH cx_root INTO erreur.
  ENDTRY.
  "Déterminer les caractères correspondant à xstring FFF8
  TRY.
      unicode_2_abap->convert(
            EXPORTING input =  
            IMPORTING data  = lw_caractere_spe ).
    CATCH cx_root INTO erreur.
  ENDTRY.

  "Création de la classe pour conv string to x avec code page à utiliser
  TRY.
      abap_2_unicode = cl_abap_conv_out_ce=>create(
               encoding = '4103' "Unicode 16BE, see table TCP00
               ).
    CATCH cx_root INTO erreur.
  ENDTRY.

**********************************************************************
* Ecriture dans le SPOOL
**********************************************************************
  CLEAR ls_text.
  LOOP AT ut_text INTO ls_text.

    "Couleur à mettre sur la ligne
    "Pour insérer de la couleur, il faut passer par l'hexadécimal
    "Couleur
    "3 pour jaune
    "6 pour rouge
    "5 pour vert
    "7 pour orange

    "Paramètre
    "N pour intensified OFF inverse OFF
    "H pour intensified ON inverse OFF
    "V pour intensified ON inverse ON

    CASE ls_text-color.
        "J pour Jaune
      WHEN c_color_jaune.
        CONCATENATE lw_caractere_spe 'COL3H' ls_text-text INTO lw_string.
        "R pour Rouge
      WHEN c_color_rouge.
        CONCATENATE lw_caractere_spe 'COL6H' ls_text-text INTO lw_string.
        "V pour Vert
      WHEN c_color_vert.
        CONCATENATE lw_caractere_spe 'COL5H' ls_text-text INTO lw_string.

      WHEN OTHERS. "Pas de couleurs
        CONCATENATE lw_caractere_spe 'COL0N' ls_text-text INTO lw_string.
    ENDCASE.

    "Détermination des caractères en xstring UTF16 pour notre chaine de caractères
    TRY.
        abap_2_unicode->convert(
              EXPORTING data = lw_string
              IMPORTING buffer  = lw_xstring ).
      CATCH cx_root INTO erreur.
    ENDTRY.

    "Appel de la fonction pour écrire dans le spool en héxa

    CALL FUNCTION 'RSPO_SR_WRITE_BINARY'
      EXPORTING
        handle                 = uw_handle
        data                   = lw_xstring
*   LENGTH                 =
*   CODEPAGE               =
     EXCEPTIONS
        handle_not_valid       = 1
        operation_failed       = 2
        OTHERS                 = 3.
    IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    CLEAR : lw_xstring , lw_string , ls_text.


  ENDLOOP.

ENDFORM.                    " ADD_MESSAGE_SPOOL

Close spool


**********************************************************************
***Fermeture du SPOOL
**********************************************************************
  CALL FUNCTION 'RSPO_SR_CLOSE'
    EXPORTING
      handle                 = gw_handle
*   PAGES                  =
*   FINAL                  =
   EXCEPTIONS
     handle_not_valid       = 1
     operation_failed       = 2
     OTHERS                 = 3
            .
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

Thanks to Melanie ! 😉

About the author

fjourneau administrator

Hi, I'm Florian Journeau, SAP ABAP R3 Freelance, based in Toulouse, France. You want to know more about me, have a look on my CV : cv.fjourneau.net.

Leave a Reply