ABAP – ALV custom toolbar button + Import data from clip board

SAPIn this code example I’ll show how to add custom button to your ALV toolbar. This custom button will import data from clipboard into the ALV grid. This can solve issue with classic Ctrl+C and Ctrl+V where there’s a problem that new rows are NOT created in the ALV grid automatically – this will be solved by the extra toolbar buttonI have created a Z-program and screen 0100 to demonstrate the functionality.

Here comes the definition of a simple data structure used in our ALV grid:

TYPES:
  BEGIN OF gty_data,
    matnr TYPE matnr,
    plant TYPE werks_d,
  END OF gty_data.

Definition of the LCL_DEMO class :

CLASS lcl_demo DEFINITION.
  PUBLIC SECTION.
    METHODS show_grid.

  PRIVATE SECTION.
    DATA:
      mt_fieldcat TYPE lvc_t_fcat,
      mo_data_grid TYPE REF TO cl_gui_alv_grid,
      mt_data TYPE STANDARD TABLE OF gty_data WITH KEY matnr.

    CONSTANTS:
*     Custom user functions encapsulated in private class constant
      BEGIN OF mc_functions,
        import_clipboard TYPE ui_func VALUE 'IMP_CLIPBOARD',
      END OF mc_functions.

    METHODS my_toolbar_handler      " TOOLBAR
       FOR EVENT toolbar OF cl_gui_alv_grid
         IMPORTING
           e_object
           e_interactive.

    METHODS my_user_command_handler    " USER_COMMAND
       FOR EVENT user_command OF cl_gui_alv_grid
         IMPORTING
           e_ucomm.

    METHODS import_clipboard.
    METHODS build_fieldcat.
ENDCLASS.

Here follows implementation of the class:

CLASS lcl_demo IMPLEMENTATION.

* Create field catalog for our two fields
  METHOD build_fieldcat.
    FIELD-SYMBOLS:
      <fs_fcat> TYPE lvc_s_fcat.

    APPEND INITIAL LINE TO mt_fieldcat ASSIGNING <fs_fcat>.
    <fs_fcat>-fieldname = 'MATNR'.
    <fs_fcat>-scrtext_s = 'Material'.

    APPEND INITIAL LINE TO mt_fieldcat ASSIGNING <fs_fcat>.
    <fs_fcat>-fieldname = 'PLANT'.
    <fs_fcat>-scrtext_s = 'Plant'.
  ENDMETHOD.                    "build_fieldcat

There are actually several types of objects that can be added to the toolbar:

Value Constant Meaning
0 cntb_btype_button Button (normal)
1 cntb_btype_dropdown Pushbutton with menu
2 cntb_btype_menu Menu
3 cntb_btype_sep Seperator
4 cntb_btype_group Pushbutton group
5 cntb_btype_check Checkbox
6 Menu entry

Method which adds the custom button to ALV toolbar:

  METHOD my_toolbar_handler.
    data:
      ls_button type stb_button.

    ls_button-butn_type = 0. "Button
    ls_button-function = mc_functions-import_clipboard.
    ls_button-icon = '@48@'. "Import icon
    ls_button-text = 'Import Clipboard'.

    INSERT ls_button into e_object->mt_toolbar INDEX 1.
  ENDMETHOD.

Method which handles user-click on the custom toolbar button:

  METHOD my_user_command_handler.
    CASE e_ucomm.
      WHEN mc_functions-import_clipboard.
        me->import_clipboard( ).
    ENDCASE.

*   We need to refresh grid with updated data
    IF mo_data_grid IS NOT INITIAL.
      mo_data_grid->refresh_table_display( ).
    ENDIF.
  ENDMETHOD.

A method for clipboard import:

  METHOD import_clipboard.
    DATA:
      ls_data type gty_data,
      lt_clipdata TYPE STANDARD TABLE OF char255,
      ls_clipdata type char255,
      lt_record TYPE STANDARD TABLE OF char255,
      ls_record type char255,
      lv_clip_len TYPE i.

    CONSTANTS: c_tab  TYPE c VALUE cl_bcs_convert=>gc_tab.

    cl_gui_frontend_services=>clipboard_import(
      IMPORTING
         data                 = lt_clipdata
         length               = lv_clip_len
       EXCEPTIONS
         cntl_error           = 1
         error_no_gui         = 2
         not_supported_by_gui = 3
         OTHERS               = 4 ).
    IF sy-subrc NE 0.
      MESSAGE 'Error while importing data from clipboard' TYPE 'I'.
      RETURN.
    ENDIF.

    LOOP AT lt_clipdata INTO ls_clipdata.
*     Split data to respective fields
      SPLIT ls_clipdata AT c_tab
        INTO TABLE lt_record.

*     Populate data into the ls_data structure
      CLEAR ls_data.

      "reading MATNR
      READ TABLE lt_record INTO ls_record INDEX 1.
      IF sy-subrc = 0.
        ls_data-matnr = ls_record.
      ENDIF.

      "reading PLANT
      READ TABLE lt_record INTO ls_record INDEX 2.
      IF sy-subrc = 0.
        ls_data-plant = ls_record.
      ENDIF.

*     Populate the ALV data table
      IF ls_data IS NOT INITIAL.
        APPEND ls_data TO mt_data.
      ENDIF.
    ENDLOOP.
  ENDMETHOD.

And the final public method to display the ALV grid:

  METHOD show_grid.
    DATA:
      ls_layout   TYPE lvc_s_layo.

    IF mo_data_grid IS INITIAL.
      CREATE OBJECT mo_data_grid
        EXPORTING
          i_parent      = cl_gui_container=>screen0
          i_appl_events = abap_true.

      ls_layout-sel_mode = 'A'.
      ls_layout-no_rowmark = abap_false.

      build_fieldcat( ).

*     !!! Handlers registration !!!
      SET HANDLER my_toolbar_handler FOR mo_data_grid.
      SET HANDLER my_user_command_handler FOR mo_data_grid.

      mo_data_grid->set_table_for_first_display(
        EXPORTING
          is_layout             = ls_layout
        CHANGING
          it_fieldcatalog       = mt_fieldcat
          it_outtab             = mt_data ).
    ENDIF.
  ENDMETHOD.

The report main program is now quite simple:

DATA:
  gr_grid TYPE REF TO lcl_demo.

START-OF-SELECTION.
  CALL SCREEN 100.

MODULE pbo OUTPUT.
  CREATE OBJECT gr_grid.
  gr_grid->show_grid( ).
ENDMODULE.

To show you a real example I prepared few lines in Excel:

Excel data

After running the ABAP report (code above) I got this screen:

Custom toolbar button demo

When I pressed the Import Clipboard button I got the following:

Custom toolbar button demo - Imported data

Leave a Reply