Skip to Content

Original blog post comes from oprsteny.com

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:

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

To report this post you need to login first.

11 Comments

You must be Logged on to comment or reply to a post.

    1. Miroslav Oprsteny Post author

      Hi,

      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.

      (0) 
      1. Shai Sinai

        Hi,

        I don’t see any problem with it.

        Please check the ‘Insert in New Row’ function:

        /wp-content/uploads/2014/03/alv_insert_407275.jpg

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

        (0) 
      2. Miroslav Oprsteny 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.

        (0) 
        1. Shai Sinai

          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?

          (0) 
          1. Miroslav Oprsteny 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 🙂

            (0) 
            1. Shai Sinai

              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… 🙂

              (0) 
            2. Jozef Vaclavek

              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.

              (0) 
              1. Miroslav Oprsteny 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.

                (0) 

Leave a Reply