11

Copy Data from Screen to Excel via the Clipboard

 2 years ago
source link: https://blogs.sap.com/2021/11/21/copy-data-from-screen-to-excel-via-the-clipboard/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
November 21, 2021 1 minute read

Copy Data from Screen to Excel via the Clipboard

In September 2021 during one of the practitioner forums someone mentioned needing to copy data from a screen onto Excel which sounded like a good challenge.

My example below is just taking a selection screen with a flavour on top to show the possibilities, but I’d guess any screen with different fields would do.  There is, of course, no need to do this with ALVs.

I created a simple favour with one additional script button:

selection_screen.png

To get the clipboard functionality I wrote a new function module which was placed in the Personas Whitelist.

FUNCTION zca_string_to_clipboard.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(P_EXCEL_STRING) TYPE  STRING
*"----------------------------------------------------------------------

  DATA: gt_text TYPE STANDARD TABLE OF char255.


  SPLIT p_excel_string AT '\n' INTO TABLE gt_text.

  CALL METHOD cl_gui_frontend_services=>clipboard_export
    IMPORTING
      data = gt_text
    CHANGING
      rc   = gv_rc.


ENDFUNCTION.

To use this function module, I attached the script which really only builds a long string of all the field values including tables and linefeeds.

var excelStr = session.findById("wnd[0]/usr/ctxtSO_PERNR-LOW").text;
excelStr += '\t';  //  Tab
excelStr += session.findById("wnd[0]/usr/ctxtSO_PERNR-HIGH").text;

excelStr += '\n';  // New Line

excelStr += session.findById("wnd[0]/usr/ctxtSO_UNAME-LOW").text;
excelStr += '\t';  //  Tab
excelStr += session.findById("wnd[0]/usr/ctxtSO_UNAME-HIGH").text;

excelStr += '\n';  // New Line

excelStr += session.findById("wnd[0]/usr/ctxtPA_DATAM").text;
excelStr += '\t';  //  Tab
excelStr += '';    // Empty cell for non-existing field

excelStr += '\n';  // New Line

session.utils.alert(excelStr);

var oRFC = session.createRFC("ZCA_STRING_TO_CLIPBOARD");
oRFC.setParameter("P_EXCEL_STRING", excelStr );
oRFC.send();

I tested this both on the browser as well as in the SAPGUI (we’re still on ECC 6.0, but guess would work on elsewhere).

excel_paste.png

This is a very fast, probably not the most elegant solution, but does work.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK