Sie sind auf Seite 1von 5

ABAP ALV custom toolbar button +

Import data from clip board


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

1. TYPES:
2. BEGIN OF gty_data,
3. matnr TYPE matnr,
4. plant TYPE werks_d,
5. END OF gty_data.

Definition of the LCL_DEMO class :

1. CLASS lcl_demo DEFINITION.


2. PUBLIC SECTION.
3. METHODS show_grid.
4.
5. PRIVATE SECTION.
6. DATA:
7. mt_fieldcat TYPE lvc_t_fcat,
8. mo_data_grid TYPE REF TO cl_gui_alv_grid,
9. mt_data TYPE STANDARD TABLE OF gty_data WITH KEY matnr.
10.
11. CONSTANTS:
12. * Custom user functions encapsulated in private class constant
13. BEGIN OF mc_functions,
14. import_clipboard TYPE ui_func VALUE 'IMP_CLIPBOARD',
15. END OF mc_functions.
16.
17. METHODS my_toolbar_handler " TOOLBAR
18. FOR EVENT toolbar OF cl_gui_alv_grid
19. IMPORTING
20. e_object
21. e_interactive.
22.
23. METHODS my_user_command_handler " USER_COMMAND
24. FOR EVENT user_command OF cl_gui_alv_grid
25. IMPORTING
26. e_ucomm.
27.
28. METHODS import_clipboard.
29. METHODS build_fieldcat.
30. ENDCLASS.
Here follows implementation of the class:

1. CLASS lcl_demo IMPLEMENTATION.


2.
3. * Create field catalog for our two fields
4. METHOD build_fieldcat.
5. FIELD-SYMBOLS:
6. <fs_fcat> TYPE lvc_s_fcat.
7.
8. APPEND INITIAL LINE TO mt_fieldcat ASSIGNING <fs_fcat>.
9. <fs_fcat>-fieldname = 'MATNR'.
10. <fs_fcat>-scrtext_s = 'Material'.
11.
12. APPEND INITIAL LINE TO mt_fieldcat ASSIGNING <fs_fcat>.
13. <fs_fcat>-fieldname = 'PLANT'.
14. <fs_fcat>-scrtext_s = 'Plant'.
15. 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:

1. METHOD my_toolbar_handler.
2. data:
3. ls_button type stb_button.
4.
5. ls_button-butn_type = 0. "Button
6. ls_button-function = mc_functions-import_clipboard.
7. ls_button-icon = '@48@'. "Import icon
8. ls_button-text = 'Import Clipboard'.
9.
10. INSERT ls_button into e_object->mt_toolbar INDEX 1.
11. ENDMETHOD.
Method which handles user-click on the custom toolbar button:

1. METHOD my_user_command_handler.
2. CASE e_ucomm.
3. WHEN mc_functions-import_clipboard.
4. me->import_clipboard( ).
5. ENDCASE.
6.
7. * We need to refresh grid with updated data
8. IF mo_data_grid IS NOT INITIAL.
9. mo_data_grid->refresh_table_display( ).
10. ENDIF.
11. ENDMETHOD.

A method for clipboard import:

1. METHOD import_clipboard.
2. DATA:
3. ls_data type gty_data,
4. lt_clipdata TYPE STANDARD TABLE OF char255,
5. ls_clipdata type char255,
6. lt_record TYPE STANDARD TABLE OF char255,
7. ls_record type char255,
8. lv_clip_len TYPE i.
9.
10. CONSTANTS: c_tab TYPE c VALUE cl_bcs_convert=>gc_tab.
11.
12. cl_gui_frontend_services=>clipboard_import(
13. IMPORTING
14. data = lt_clipdata
15. length = lv_clip_len
16. EXCEPTIONS
17. cntl_error = 1
18. error_no_gui = 2
19. not_supported_by_gui = 3
20. OTHERS = 4 ).
21. IF sy-subrc NE 0.
22. MESSAGE 'Error while importing data from clipboard' TYPE 'I'.
23. RETURN.
24. ENDIF.
25.
26. LOOP AT lt_clipdata INTO ls_clipdata.
27. * Split data to respective fields
28. SPLIT ls_clipdata AT c_tab
29. INTO TABLE lt_record.
30.
31. * Populate data into the ls_data structure
32. CLEAR ls_data.
33.
34. "reading MATNR
35. READ TABLE lt_record INTO ls_record INDEX 1.
36. IF sy-subrc = 0.
37. ls_data-matnr = ls_record.
38. ENDIF.
39.
40. "reading PLANT
41. READ TABLE lt_record INTO ls_record INDEX 2.
42. IF sy-subrc = 0.
43. ls_data-plant = ls_record.
44. ENDIF.
45.
46. * Populate the ALV data table
47. IF ls_data IS NOT INITIAL.
48. APPEND ls_data TO mt_data.
49. ENDIF.
50. ENDLOOP.
51. ENDMETHOD.

And the final public method to display the ALV grid:

1. METHOD show_grid.
2. DATA:
3. ls_layout TYPE lvc_s_layo.
4.
5. IF mo_data_grid IS INITIAL.
6. CREATE OBJECT mo_data_grid
7. EXPORTING
8. i_parent = cl_gui_container=>screen0
9. i_appl_events = abap_true.
10.
11. ls_layout-sel_mode = 'A'.
12. ls_layout-no_rowmark = abap_false.
13.
14. build_fieldcat( ).
15.
16. * !!! Handlers registration !!!
17. SET HANDLER my_toolbar_handler FOR mo_data_grid.
18. SET HANDLER my_user_command_handler FOR mo_data_grid.
19.
20. mo_data_grid->set_table_for_first_display(
21. EXPORTING
22. is_layout = ls_layout
23. CHANGING
24. it_fieldcatalog = mt_fieldcat
25. it_outtab = mt_data ).
26. ENDIF.
27. ENDMETHOD.

The report main program is now quite simple:

1. DATA:
2. gr_grid TYPE REF TO lcl_demo.
3.
4. START-OF-SELECTION.
5. CALL SCREEN 100.
6.
7. MODULE pbo OUTPUT.
8. CREATE OBJECT gr_grid.
9. gr_grid->show_grid( ).
10. ENDMODULE.
To show you a real example I prepared few lines in Excel:

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

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

Das könnte Ihnen auch gefallen