Skip to Content
Author's profile photo Former Member

ALV custom toolbar button to import data from clipboard

Original blog post comes from

In this code example I’d like to show how to add custom button to your ALV toolbar.

This custom button will import data from clipboard into the ALV grid.

It can solve the 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 button

I 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:

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

Definition of the LCL_DEMO class :

    METHODS show_grid.

      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.

*     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

    METHODS my_user_command_handler    " USER_COMMAND
       FOR EVENT user_command OF cl_gui_alv_grid

    METHODS import_clipboard.
    METHODS build_fieldcat.

Here follows implementation of the class:


* Create field catalog for our two fields
  METHOD build_fieldcat.
      <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.
      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.

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( ).

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

A method for clipboard import:

  METHOD import_clipboard.
      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.

         data                 = lt_clipdata
         length               = lv_clip_len
         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'.

    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.

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

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

And the final public method to display the ALV grid:

  METHOD show_grid.
      ls_layout   TYPE lvc_s_layo.

    IF mo_data_grid IS INITIAL.
      CREATE OBJECT mo_data_grid
          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.

          is_layout             = ls_layout
          it_fieldcatalog       = mt_fieldcat
          it_outtab             = mt_data ).

The report main program is now quite simple:

  gr_grid TYPE REF TO lcl_demo.


  CREATE OBJECT gr_grid.
  gr_grid->show_grid( ).

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

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Miroslav,

      Thanks for sharing.

      However, I'm little confused: Doesn't 'Insert in New Row' do exactly the same?

      Author's profile photo Former Member
      Former Member
      Blog Post Author


      you are correct about inserting in new row.

      But did you think about situation you don't have this empty row created?

      Or when you want to import ie 50 lines?

      Would you like to press "Add New Line" 50 times and then press Ctrl+V?

      The goal of this code was to create new lines automatically.

      Author's profile photo Former Member
      Former Member

      inserting (ctrl+v) would do the same job, even if you have more rows in your clipboard

      Author's profile photo Former Member
      Former Member


      I don't see any problem with it.

      Please check the 'Insert in New Row' function:


      As Jozef mentioned, CTRL+V should also do the work.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi both,

      thank you for your replies - I just tested and you' re right, it is possible to import clipboard with just Ctrl+V, so I apologize for misleading info. However this seems to work ONLY if you have all required neighbor columns enabled for editing.

      The functionality described in the article here was used in our project due to fact there was always data lines created with values each displayed in two columns (except key fields) where:

      - one column was editable (new value being put in by user)

      - one column read-only (current SAP value)

      3-11-2014 8-18-59 AM.png

      And this was the moment where standard Ctrl+V was not working as expected and we had to implement the clipboard data import function programatically.

      Author's profile photo Former Member
      Former Member

      Thanks for the clarification.

      So, if I understood correctly, This is useful when the imported data and ALV have different structure (field catalog). Is that right?

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      In general, for ALV - yes. Otherwise you can use the standard Ctrl+V functionality.

      But the clipboard data import function could be used in many more different scenarios, not only in the ALV 🙂

      Author's profile photo Former Member
      Former Member

      Of course.

      One example would be table controls, where "smart" paste (for more records than displayed in screen) isn't available.

      Just wanted to summarize the subject of this blog... 🙂

      Author's profile photo Former Member
      Former Member

      I believe you would not be able to clipboard-import a different structure than the one you have in your ALV. Of course you can, in your code, decide where to place which "column" from the clipboard. But this is probably impossible to do once your outer structure changes (and you do not have the header - columns names there). So the most (and probably only) added value of this shall be the possibility to import not-editable columns.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      As I wrote earlier - customer wanted to import data ONLY to editable columns (they were migrating from MS Excel so the data structure was given and was stable, so the column headers was not necessary in this case, but generally you're correct)

      The problem was with skipping the read-only columns which was achieved by this functionality.

      Author's profile photo Eitan Rosenberg
      Eitan Rosenberg


      Very nice idea.

      IMHO it will be more useful if you UL a full working program .