Solution
We call up a JavaScript function that reads the Excel file via Excel
automation and then inserts the values into the grid cells via SAP GUI
Scripting.
Depending on the SAP transaction it may be necessary
to press "Enter" in our script from time to time after having
filled all grid rows that are available on the screen. The "Enter" then
provides further empty rows at the end of the grid so that we can
continue with the next set of values. This "packeting" considerably
increases the complexity of our solution, but it is necessary unless we
limit ourselves to a relatively small number of rows in the Excel file.
Example
We want to upload purchase requisition data in transaction ME51N:
Our Excel file contains the following sample data:
The following video shows the uploading process; see the full
screen mode to watch it in detail:
if X[GRIDCONTROL]
// enlarge grid control so that the user sees all columns
pos X[GRIDCONTROL] X[GRIDCONTROL] width=220
// add an upload button right hand side of the grid
Pushbutton X[GRIDCONTROL]+(0,222) "@J2@Excel upload" _
process="me51n_excel_upload.txt" size=(2,16)
// handle further data packets
if V[continue_excel_upload]
clear V[continue_excel_upload]
title "Please wait.... uploading Excel values"
CallJSAsync excel_me51n_continue_upload
endif
endif
InputScript "me51n_excel_upload.txt"
GuiXT
// --------------------------------------------------
// Upload purchase requisition items from Excel
// --------------------------------------------------
// select a file
SelectFile name="xfile" _
title="Select the Excel file to upload" _
filter="*.xlsx" _
directory="C:\Puchase Requisitions Excel"
// any file selected? else no action
if not Q[ok]
return "S: No file selected" -statusline
endif
// upload Excel file via JavaScript
CallJSAsync excel_me51n_upload "&V[xfile]"
Return
InputScript
"me51n_excel_upload_continue.txt"
GuiXT
// --------------------------------------------------
// Continue uploading purchase requisition items from Excel
// --------------------------------------------------
Set V[continue_excel_upload] "X"
Enter