Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member184578
Active Contributor

Introduction

This document helps how to use multi copy/paste from excel to Web Dynpro ABAP. In earlier versions of Netweaver ( before Netweaver 7.0 Ehp3 ), there is no provision to paste the clip board data to Web Dynpro ABAP. When you copy data from excel and paste in Web Dynpro ABAP Table, the data will be pasted only in the pasted cell.

From Netweaver 7.0 Ehp3, we have an event onTablePaste for input field, where we can handle the Pasted clipboard data.

Prerequisites

SAP NetWeaver 700 Ehp3.


Step by Step Process

Step 1: Create a Web Dynpro Component.

Go to the SE80 transaction and create a Web Dynpro Component.

Enter Description and click on OK.

Save and Activate the Component.

Step 2: Data Binding

Go to the Context tab of Main View and create a node STUDENT.

Enter Node name, select cardinality 0..n and click on OK.

Now right click on Context node STUDENT and create an attribute ID of type NUMC10.

Similarly create attributes: FNAME of type STRING, LNAME of type STRING and DOB of type DATS.

Step 3: Layout Design.

   Now Go to Layout tab, and click on Web Dynpro Code Wizard( magic symbol button).

Now double click on Table to create and Bind Table UI.

Click on context and select the STUDENT node.

Change the Standard cell editor to 'Input Filed' .and click on OK.

Now we can see the Table UI in the layout.

Now select the ID input filed table cell editor, under properties click on create event for onTablePaste.


Enter Action name and description and click on OK.

Now go to methods Tab of MAIN view, and enter the below code in WDDOINIT method

WDDOINIT
METHOD WDDOINIT .

   DATA lo_nd_student TYPE REF TO if_wd_context_node.
   DATA lt_student    TYPE wd_this->elements_student.

   lo_nd_student = wd_context->get_child_node( name = wd_this->wdctx_student ).
*  Create empty rows to insert data
   DO 10 TIMES.
     APPEND INITIAL
LINE TO lt_student.
   ENDDO.

   lo_nd_student->bind_table( new_items = lt_student set_initial_elements = abap_true ).

ENDMETHOD.

Now double click on ONACTIONPASTEDATA method and create the parameters:

1. CONTEXT_ELEMENT   Type REF TO IF_WD_CONTEXT_ELEMENT

2. ID                                       Type                 STRING

3. TABLE                               Type                 WDUI_TABLE_PASTE_DATA

Enter the below code in ONACTIONPASTEDATA method

ONACTIONPASTEDATA

METHOD ONACTIONPASTEDATA .

     DATA lo_nd_student TYPE REF TO if_wd_context_node.
     DATA lt_student    TYPE wd_this->elements_student.
     DATA ls_cell       LIKE LINE OF table.
     DATA lv_row_number TYPE i VALUE 1.
     DATA lv_paste_row  TYPE i.
     DATA lv_cur_column TYPE i.
     DATA lr_rtti       TYPE REF TO cl_abap_typedescr.
     DATA lcx_dyn_check TYPE REF TO cx_dynamic_check.
     DATA lt_attributes TYPE string_table.
     DATA lv_col_name   TYPE string.

     FIELD-SYMBOLS <table_row> LIKE LINE OF lt_student.
     FIELD-SYMBOLS <table_col> TYPE data.

     lo_nd_student = wd_context->get_child_node( name = wd_this->wdctx_student ).
     lo_nd_student->get_static_attributes_table( IMPORTING table = lt_student ).
*   Get row number at which the data is pasted
     lv_paste_row  = context_element->get_index( ).
*   Get attributes of the Context Node
     lt_attributes = lo_nd_student->get_node_info( )->get_attribute_names( ).

*  Convert Column Data to Row and set to the Context
     LOOP AT lt_student ASSIGNING <table_row> FROM lv_paste_row.
       lv_cur_column = 1.
       LOOP AT table INTO ls_cell WHERE row = lv_row_number.
         IF lv_cur_column > lines( lt_attributes ).
           EXIT.
         ENDIF.
         READ TABLE lt_attributes INTO lv_col_name INDEX lv_cur_column.
         ASSERT sy-subrc = 0.
         ASSIGN COMPONENT lv_col_name OF STRUCTURE <table_row> TO <table_col>.
         lr_rtti = cl_abap_typedescr=>describe_by_data( <table_col> ).
         CASE lr_rtti->type_kind.
           WHEN cl_abap_typedescr=>typekind_decfloat16.
             REPLACE ALL OCCURRENCES OF ',' IN ls_cell-data WITH '.'.
             TRY.
                 <table_col> = ls_cell-data.
               CATCH cx_sy_conversion_no_number cx_sy_move_cast_error INTO lcx_dyn_check.
                 " Paste not possible, report warning
                 wd_this->wd_get_api( )->get_message_manager( )->report_warning

                                                                                             lcx_dyn_check->get_longtext( ) ).
             ENDTRY.
           WHEN cl_abap_typedescr=>typekind_date.
             CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'  " Convert date to internal
               EXPORTING
                 date_external            = ls_cell-data
               IMPORTING
                 date_internal            = <table_col>
               EXCEPTIONS
                 date_external_is_invalid = 1
                 OTHERS                  = 2.
             IF sy-subrc <> 0.
               wd_this->wd_get_api( )->get_message_manager( )->report_warning( 'Invalid Date Format' ).
             ENDIF.
           WHEN OTHERS.
             TRY.
                 <table_col> = ls_cell-data.
               CATCH cx_sy_conversion_no_number cx_sy_move_cast_error INTO lcx_dyn_check.
                 " Paste not possible, report warning
                 wd_this->wd_get_api( )->get_message_manager( )->report_warning(

                                                                                              lcx_dyn_check->get_longtext( ) ).
             ENDTRY.
         ENDCASE.

         ADD 1 TO lv_cur_column. " Move to next Column
       ENDLOOP.
       ADD 1 TO lv_row_number. " Move to next Row
     ENDLOOP.

*  Bind the Pasted data to the table
   lo_nd_student->bind_table( lt_student ).

ENDMETHOD.

Now Save and Activate the Web Dynpro Component.

Create Application.

Create Web Dynpro Application and save it.

Enter description, click on OK and save the application.


Now right click on web dynpro application and click on Test.

Result:

Now you can see, the table with empty rows ready for input.

Now copy the data from Excel and paste it in the table

Paste it in the Table UI

Now you can see all the data got pasted in Table UI ( not only in the visible rows )

Explanation.

When you paste data in the Table UI (Press CTRL +V ), the onActionPasteData method will be called and the Pasted Data will be passed to the Parameter TABLE of the onActionPasteData event handler method in the form of columnar data.

here is the debugger snapshot of the TABLE ( pasted data )

So, we have to convert this column data to Row data. For this I have written the code in onActionPasteData method to convert the column data to row data and set to the table UI.


Here, We have created 10 empty rows in the table UI and pasted 10 records from excel. Now if we paste 15 records from excel, only first 10 records will be pasted to the table UI as we have created only 10 rows.

Copy 15 records from excel

Paste in the Table UI

Now we can see only 10 records got pasted in the Table UI. ( this is because we are looping the table UI and dynamically setting the pasted data)

Inorder to paste all the copied data, we will write a simple code in the onActionPasteData method.

write the below code in the onActionPasteData method to paste all the copied data.

ONACTIONPASTEDATA

METHOD ONACTIONPASTEDATA .

     DATA lo_nd_student      TYPE REF TO if_wd_context_node.
     DATA lt_student         TYPE wd_this->elements_student.
     DATA ls_cell            LIKE LINE OF table.
     DATA lv_row_number      TYPE i VALUE 1.
     DATA lv_paste_row       TYPE i.
     DATA lv_cur_column      TYPE i.
     DATA lr_rtti            TYPE REF TO cl_abap_typedescr.
     DATA lcx_dyn_check      TYPE REF TO cx_dynamic_check.
     DATA lt_attributes      TYPE string_table.
     DATA lv_col_name        TYPE string.
     DATA lv_copied_rows     TYPE i.
     DATA lv_total_tab_rows  TYPE i.
     DATA lv_diff_copy_paste TYPE i.
     DATA ls_table           TYPE wdui_table_line_paste_data.

     FIELD-SYMBOLS <table_row> LIKE LINE OF lt_student.
     FIELD-SYMBOLS <table_col> TYPE data.

     lo_nd_student = wd_context->get_child_node( name = wd_this->wdctx_student ).
     lo_nd_student->get_static_attributes_table( IMPORTING table = lt_student ).
*   Get row number at which the data is pasted
     lv_paste_row  = context_element->get_index( ).
*   Get attributes of the Context Node
     lt_attributes = lo_nd_student->get_node_info( )->get_attribute_names( ).

*   Get total copied rows
     lv_copied_rows = lines( table ).
     READ TABLE table INTO ls_table INDEX lv_copied_rows.
     CLEAR lv_copied_rows.
     lv_copied_rows = ls_table-row.

*   Get total rows in table UI
     lv_total_tab_rows = lines( lt_student ).

*   Create extra rows in Table UI if pasted records are more that the table records
     IF lv_copied_rows > lv_total_tab_rows.
       lv_diff_copy_paste = lv_copied_rows - lv_total_tab_rows.
       DO lv_diff_copy_paste TIMES.
         APPEND INITIAL LINE TO lt_student.
       ENDDO.
     ENDIF.

*  Convert Column Data to Row and set to the Context
     LOOP AT lt_student ASSIGNING <table_row> FROM lv_paste_row.
       lv_cur_column = 1.
       LOOP AT table INTO ls_cell WHERE row = lv_row_number.
         IF lv_cur_column > lines( lt_attributes ).
           EXIT.
         ENDIF.
         READ TABLE lt_attributes INTO lv_col_name INDEX lv_cur_column.
         ASSERT sy-subrc = 0.
         ASSIGN COMPONENT lv_col_name OF STRUCTURE <table_row> TO <table_col>.
         lr_rtti = cl_abap_typedescr=>describe_by_data( <table_col> ).
         CASE lr_rtti->type_kind.
           WHEN cl_abap_typedescr=>typekind_decfloat16.
             REPLACE ALL OCCURRENCES OF ',' IN ls_cell-data WITH '.'.
             TRY.
                 <table_col> = ls_cell-data.
               CATCH cx_sy_conversion_no_number cx_sy_move_cast_error INTO lcx_dyn_check.
                 " Paste not possible, report warning
                 wd_this->wd_get_api( )->get_message_manager( )->report_warning(

                                                                                                lcx_dyn_check->get_longtext( ) ).
             ENDTRY.
           WHEN cl_abap_typedescr=>typekind_date.
             CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'  " Convert date to internal
               EXPORTING
                 date_external            = ls_cell-data
               IMPORTING
                 date_internal            = <table_col>
               EXCEPTIONS
                 date_external_is_invalid = 1
                 OTHERS                  = 2.
             IF sy-subrc <> 0.
               wd_this->wd_get_api( )->get_message_manager( )->report_warning( 'Invalid Date Format' ).
             ENDIF.
           WHEN OTHERS.
             TRY.
                 <table_col> = ls_cell-data.
               CATCH cx_sy_conversion_no_number cx_sy_move_cast_error INTO lcx_dyn_check.
                 " Paste not possible, report warning
                 wd_this->wd_get_api( )->get_message_manager( )->report_warning(

                                                                                                lcx_dyn_check->get_longtext( ) ).
             ENDTRY.
         ENDCASE.

         ADD 1 TO lv_cur_column. " Move to next Column
       ENDLOOP.
       ADD 1 TO lv_row_number. " Move to next Row
     ENDLOOP.

*  Bind the Pasted data to the table
   lo_nd_student->bind_table( lt_student ).

ENDMETHOD.

Now Paste the copied records from excel ( 15 records ).

Paste the copied data to Table UI. Now we can see all the Pasted data(15 records) got paste in Table UI.

Conclusion

Here I demonstrated a simple example on how to use onTablePaste event. In this demo, I created an action for 'ID' input field table cell editor. We can create action for all the columns and in which ever column user paste the data, we can set the pasted data to the Table UI. Also this code will paste the data from which row you pasted it. lets say, if you paste the data at 3rd row, from that row the data will be pasted in the Table UI.

We can use this for Forms and Lists also. This makes our life easier.!! no.. no.. SAP makes user's life easier..

Follow me onGoogle+

34 Comments
Labels in this area