{"id":920,"date":"2014-01-31T15:19:02","date_gmt":"2014-01-31T14:19:02","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=920"},"modified":"2014-01-31T15:24:52","modified_gmt":"2014-01-31T14:24:52","slug":"abap-alv-custom-toolbar-button-import-data-from-clip-board","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=920","title":{"rendered":"ABAP &#8211; ALV custom toolbar button + Import data from clip board"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\"alignleft size-full wp-image-358\" alt=\"SAP\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" width=\"44\" height=\"50\" \/>In this code example I&#8217;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&#8217;s a problem that new rows are NOT created in the ALV grid automatically &#8211; this will be solved by the extra toolbar button<!--more-->I have created a Z-program and screen 0100 to demonstrate the functionality.<\/p>\n<p>Here comes the definition of a simple data structure used in our ALV grid:<\/p>\n<pre lang=\"abap\">TYPES:\r\n  BEGIN OF gty_data,\r\n    matnr TYPE matnr,\r\n    plant TYPE werks_d,\r\n  END OF gty_data.<\/pre>\n<p>Definition of the <em>LCL_DEMO\u00a0<\/em>class :<\/p>\n<pre lang=\"abap\">CLASS lcl_demo DEFINITION.\r\n  PUBLIC SECTION.\r\n    METHODS show_grid.\r\n\r\n  PRIVATE SECTION.\r\n    DATA:\r\n      mt_fieldcat TYPE lvc_t_fcat,\r\n      mo_data_grid TYPE REF TO cl_gui_alv_grid,\r\n      mt_data TYPE STANDARD TABLE OF gty_data WITH KEY matnr.\r\n\r\n    CONSTANTS:\r\n*     Custom user functions encapsulated in private class constant\r\n      BEGIN OF mc_functions,\r\n        import_clipboard TYPE ui_func VALUE 'IMP_CLIPBOARD',\r\n      END OF mc_functions.\r\n\r\n    METHODS my_toolbar_handler      \" TOOLBAR\r\n       FOR EVENT toolbar OF cl_gui_alv_grid\r\n         IMPORTING\r\n           e_object\r\n           e_interactive.\r\n\r\n    METHODS my_user_command_handler    \" USER_COMMAND\r\n       FOR EVENT user_command OF cl_gui_alv_grid\r\n         IMPORTING\r\n           e_ucomm.\r\n\r\n    METHODS import_clipboard.\r\n    METHODS build_fieldcat.\r\nENDCLASS.<\/pre>\n<p>Here follows implementation of the class:<\/p>\n<pre lang=\"abap\">CLASS lcl_demo IMPLEMENTATION.\r\n\r\n* Create field catalog for our two fields\r\n  METHOD build_fieldcat.\r\n    FIELD-SYMBOLS:\r\n      &lt;fs_fcat&gt; TYPE lvc_s_fcat.\r\n\r\n    APPEND INITIAL LINE TO mt_fieldcat ASSIGNING &lt;fs_fcat&gt;.\r\n    &lt;fs_fcat&gt;-fieldname = 'MATNR'.\r\n    &lt;fs_fcat&gt;-scrtext_s = 'Material'.\r\n\r\n    APPEND INITIAL LINE TO mt_fieldcat ASSIGNING &lt;fs_fcat&gt;.\r\n    &lt;fs_fcat&gt;-fieldname = 'PLANT'.\r\n    &lt;fs_fcat&gt;-scrtext_s = 'Plant'.\r\n  ENDMETHOD.                    \"build_fieldcat<\/pre>\n<p>There are actually several types of objects that can be added to the toolbar:<\/p>\n<table border=\"1\" cellspacing=\"1\">\n<tbody>\n<tr>\n<td><strong>Value<\/strong><\/td>\n<td><strong>Constant<\/strong><\/td>\n<td><strong>Meaning<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>0<\/strong><\/td>\n<td>cntb_btype_button<\/td>\n<td>Button (normal)<\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td>cntb_btype_dropdown<\/td>\n<td>Pushbutton with menu<\/td>\n<\/tr>\n<tr>\n<td><strong>2<\/strong><\/td>\n<td>cntb_btype_menu<\/td>\n<td>Menu<\/td>\n<\/tr>\n<tr>\n<td><strong>3<\/strong><\/td>\n<td>cntb_btype_sep<\/td>\n<td>Seperator<\/td>\n<\/tr>\n<tr>\n<td><strong>4<\/strong><\/td>\n<td>cntb_btype_group<\/td>\n<td>Pushbutton group<\/td>\n<\/tr>\n<tr>\n<td><strong>5<\/strong><\/td>\n<td>cntb_btype_check<\/td>\n<td>Checkbox<\/td>\n<\/tr>\n<tr>\n<td><strong>6<\/strong><\/td>\n<td><\/td>\n<td>Menu entry<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Method which adds the custom button to ALV toolbar:<\/p>\n<pre lang=\"abap\">  METHOD my_toolbar_handler.\r\n    data:\r\n      ls_button type stb_button.\r\n\r\n    ls_button-butn_type = 0. \"Button\r\n    ls_button-function = mc_functions-import_clipboard.\r\n    ls_button-icon = '@48@'. \"Import icon\r\n    ls_button-text = 'Import Clipboard'.\r\n\r\n    INSERT ls_button into e_object-&gt;mt_toolbar INDEX 1.\r\n  ENDMETHOD.<\/pre>\n<p>Method which handles user-click on the custom toolbar button:<\/p>\n<pre lang=\"abap\">  METHOD my_user_command_handler.\r\n    CASE e_ucomm.\r\n      WHEN mc_functions-import_clipboard.\r\n        me-&gt;import_clipboard( ).\r\n    ENDCASE.\r\n\r\n*   We need to refresh grid with updated data\r\n    IF mo_data_grid IS NOT INITIAL.\r\n      mo_data_grid-&gt;refresh_table_display( ).\r\n    ENDIF.\r\n  ENDMETHOD.<\/pre>\n<p>A method for clipboard import:<\/p>\n<pre lang=\"abap\">  METHOD import_clipboard.\r\n    DATA:\r\n      ls_data type gty_data,\r\n      lt_clipdata TYPE STANDARD TABLE OF char255,\r\n      ls_clipdata type char255,\r\n      lt_record TYPE STANDARD TABLE OF char255,\r\n      ls_record type char255,\r\n      lv_clip_len TYPE i.\r\n\r\n    CONSTANTS: c_tab  TYPE c VALUE cl_bcs_convert=&gt;gc_tab.\r\n\r\n    cl_gui_frontend_services=&gt;clipboard_import(\r\n      IMPORTING\r\n         data                 = lt_clipdata\r\n         length               = lv_clip_len\r\n       EXCEPTIONS\r\n         cntl_error           = 1\r\n         error_no_gui         = 2\r\n         not_supported_by_gui = 3\r\n         OTHERS               = 4 ).\r\n    IF sy-subrc NE 0.\r\n      MESSAGE 'Error while importing data from clipboard' TYPE 'I'.\r\n      RETURN.\r\n    ENDIF.\r\n\r\n    LOOP AT lt_clipdata INTO ls_clipdata.\r\n*     Split data to respective fields\r\n      SPLIT ls_clipdata AT c_tab\r\n        INTO TABLE lt_record.\r\n\r\n*     Populate data into the ls_data structure\r\n      CLEAR ls_data.\r\n\r\n      \"reading MATNR\r\n      READ TABLE lt_record INTO ls_record INDEX 1.\r\n      IF sy-subrc = 0.\r\n        ls_data-matnr = ls_record.\r\n      ENDIF.\r\n\r\n      \"reading PLANT\r\n      READ TABLE lt_record INTO ls_record INDEX 2.\r\n      IF sy-subrc = 0.\r\n        ls_data-plant = ls_record.\r\n      ENDIF.\r\n\r\n*     Populate the ALV data table\r\n      IF ls_data IS NOT INITIAL.\r\n        APPEND ls_data TO mt_data.\r\n      ENDIF.\r\n    ENDLOOP.\r\n  ENDMETHOD.<\/pre>\n<p>And the final public method to display the ALV grid:<\/p>\n<pre lang=\"abap\">  METHOD show_grid.\r\n    DATA:\r\n      ls_layout   TYPE lvc_s_layo.\r\n\r\n    IF mo_data_grid IS INITIAL.\r\n      CREATE OBJECT mo_data_grid\r\n        EXPORTING\r\n          i_parent      = cl_gui_container=&gt;screen0\r\n          i_appl_events = abap_true.\r\n\r\n      ls_layout-sel_mode = 'A'.\r\n      ls_layout-no_rowmark = abap_false.\r\n\r\n      build_fieldcat( ).\r\n\r\n*     !!! Handlers registration !!!\r\n      SET HANDLER my_toolbar_handler FOR mo_data_grid.\r\n      SET HANDLER my_user_command_handler FOR mo_data_grid.\r\n\r\n      mo_data_grid-&gt;set_table_for_first_display(\r\n        EXPORTING\r\n          is_layout             = ls_layout\r\n        CHANGING\r\n          it_fieldcatalog       = mt_fieldcat\r\n          it_outtab             = mt_data ).\r\n    ENDIF.\r\n  ENDMETHOD.<\/pre>\n<p>The report main program is now quite simple:<\/p>\n<pre lang=\"abap\">DATA:\r\n  gr_grid TYPE REF TO lcl_demo.\r\n\r\nSTART-OF-SELECTION.\r\n  CALL SCREEN 100.\r\n\r\nMODULE pbo OUTPUT.\r\n  CREATE OBJECT gr_grid.\r\n  gr_grid-&gt;show_grid( ).\r\nENDMODULE.<\/pre>\n<p>To show you a real example I prepared few lines in Excel:<\/p>\n<p><a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_01.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"921\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=921\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_01.png\" data-orig-size=\"339,151\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"Excel data\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_01.png\" class=\"size-medium wp-image-921 alignnone\" alt=\"Excel data\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_01-300x133.png\" width=\"300\" height=\"133\" srcset=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_01-300x133.png 300w, https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_01.png 339w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>After running the ABAP report (code above) I got this screen:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"922\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=922\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_02.png\" data-orig-size=\"319,212\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"ALV_TOOLBAR_IMPORT_02\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_02.png\" class=\"size-medium wp-image-922 alignnone\" title=\"Custom toolbar button demo\" alt=\"Custom toolbar button demo\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_02-300x199.png\" width=\"300\" height=\"199\" srcset=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_02-300x199.png 300w, https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_02.png 319w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>When I pressed the <em>Import Clipboard<\/em> button I got the following:<\/p>\n<p><a href=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_03.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"923\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=923\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_03.png\" data-orig-size=\"323,218\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"Excel data\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_03.png\" class=\"size-medium wp-image-923 alignleft\" title=\"Custom toolbar button demo - Imported data\" alt=\"Custom toolbar button demo - Imported data\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_03-300x202.png\" width=\"300\" height=\"202\" srcset=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_03-300x202.png 300w, https:\/\/oprsteny.cz\/wp-content\/uploads\/ALV_TOOLBAR_IMPORT_03.png 323w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this code example I&#8217;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&#8217;s a problem &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=920\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":358,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"ABAP - ALV custom toolbar button + Import data from clip board","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[16,86,9],"tags":[447,266,196,265,264],"class_list":["post-920","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-abap","category-alv","category-development","tag-alv","tag-clipboard","tag-import","tag-toolbar","tag-user_command"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-eQ","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=920"}],"version-history":[{"count":3,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/920\/revisions"}],"predecessor-version":[{"id":925,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/920\/revisions\/925"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/media\/358"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}