Purpose
Upload Excel to an SAP grid control

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:


Video

 

Show video in full screen


.zip file with InputScript, JavaScript and sample Excel file

GuiXTScript

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"

// --------------------------------------------------
// 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"

// --------------------------------------------------
//  Continue uploading purchase requisition items from Excel
// --------------------------------------------------

Set V[continue_excel_upload] "X"

Enter

 


JavaScript function

var XL = null;
var wBook = null;
var next_index = 0;

// open Excel file 
function excel_me51n_upload(filename)
{
     
   // try to attach to a running Excel instance
   XL = guixt.GetObject("Excel.Application");

   // create a new instance if not found or visible
   if (XL == null || XL.Visible)
   {
       XL = guixt.CreateObject("Excel.Application");

       if (XL == null) {
           alert("Excel not installed")
       };

       // Do not display Excel window
       XL.Visible = false;
       
       // Do not display any alerts
       XL.DisplayAlerts = false;

   };

    
     
    // Open File
   wBook = null;

    try {
        wBook = XL.Workbooks.Open(filename,0,true)
    }
    catch (e) {
        return "";
    }

    // skip headings
    next_index = 2;

    // no further action if upload not yet complete
    if (!upload_values()) return;
     
    wBook.CLose();

    XL.Quit();
    XL = null;

    // press enter
    guixt.input("OK:/0")
};



function excel_me51n_continue_upload()
{

    // continue uploading
    if (!upload_values()) return;

    wBook.CLose();

    XL.Quit();
    XL = null;
    
      // press enter
    guixt.input("OK:/0")

};


function upload_values()
{
    var grid = SapGrid;
    if (!grid) return;

    var cells = wBook.Worksheets(1).Cells;

    var k = next_index;

    while (cells(k, 1).Value) {

        // read next Excel row
        var matnr = cells(k, 1).Value;
        var menge = cells(k, 2).Value;
        var eeind = cells(k, 3).Value;
        var name1 = cells(k, 4).Value;
        var ekgrp = cells(k, 5).Value;
        var ekorg = cells(k, 6).Value;

        
        // try to put these values into SAP grid
        try {

            // set values
            grid.ModifyCell(k - 2, "MATNR", matnr);
            grid.ModifyCell(k - 2, "MENGE", menge);
            grid.ModifyCell(k - 2, "EEIND", eeind);
            grid.ModifyCell(k - 2, "NAME1", name1);
            grid.ModifyCell(k - 2, "EKGRP", ekgrp);
            grid.ModifyCell(k - 2, "EKORG", ekorg);

            grid.TriggerModified();

        }
        catch (e) {

            // no grid row vailable, we need to hit "Enter" first 
            next_index = k;

            //hit Enter and continue upload
            guixt.input("OK:/0,process=me51n_excel_upload_continue.txt")

            return false;
        }

        k++;

    };


    return true;
    
};

Components
InputAssistant + Controls