Purpose Upload Excel to an SAP grid control |
Solution Example
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
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 //
-------------------------------------------------- // select a file title="Select the Excel file to upload" _ filter="*.xlsx" _ directory="C:\Puchase Requisitions Excel" // any file selected? else no action return "S: No file selected" -statusline endif // upload Excel file via JavaScript Return
InputScript "me51n_excel_upload_continue.txt" // -------------------------------------------------- Enter
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 |