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:
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 tabs 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).
This is a very fast, probably not the most elegant solution, but does work.