|
With SAP GUI 6.20, SAP introduced a “scripting API”: an object model that represents a screen with its controls at runtime. In Windows environment the API can be used from any tool that supports the COM object interface, especially from the VBScript and JavaScript languages. The object model allows one to automate all user actions.
Unlike GuiXT, the SAP GUI scripting approach is not intended to change the screen layout. It completes the GuiXT functionality, allowing us to manipulate special controls (like the grid control) that GuiXT cannot handle directly, but is restricted to actions that a user is able to do as well. With GuiXT 2004 Q2 3 and above, you can combine the two approaches. Use the new keyword “ApplyGuiScript” in a GuiXT script or an InputScript in order to perform further actions on the screen via a VBScript. The general procedure, clarified with a few examples, is set out below. | |
1 Requirements
2 SAP documentation Please see the following SAP notes concerning SAP GUI scripting:
The following links provide copies of the original SAP documents, for easy access. Please refer to the original SAP documentation in SAP Service Marketplace (see links below) in order to get the most recent versions of the documents.
3 GuiXT scripts and SAP GUI scripting When you execute an ApplyGuiScript command in a GuiXT script the internal procedure is as follows:
4 InputScripts and SAP GUI scripting When you execute an ApplyGuiScript command in an InputScript please observe the following:
The internal procedure is as follows:
5 Writing and testing the VBScript SAP GUI provides a recording mode that can generate a VBScript directly from your actions. We suggest that you start with such a recorded script. It is a good idea to test the VBScript independently of the GuiXT script before combining both. To test the VBScript display the SAP screen where you want to apply the script. You can either:
We recommend the 2nd possibility (drag&drop), since in this case you use the same SAP GUI interface that GuiXT uses internally. Some VBScript variables, especially the “session” variable, are already defined and preset by SAP GUI when you use drag&drop.
6 Example: Selecting a fixed row in a grid control In IW38 you want to select a certain row, say the third row. First record the action in SAP GUI:
Save the generated VBScript in a suitable directory, e.g. “C:\guixt\sapgui scripts”. The script looks as follows: If Not IsObject(application) Then In your GuiXT script use “ApplyGuiScript” and specify the name of the VBScript file: ApplyGuiScript “C:\guiXT\sapgui scripts\select_grid_row_3.vbs” The screen will then show up like this:
You can simplify the VBScript if you always use the drag&drop method for testing, since the “session” variable is then defined automatically. The following script does the job as well: Set aw = session.activeWindow() Please observe that the row index starts with 0 in SAP GUI scripting, so the third row has index 2. 7 Example: Selecting variable rows in a grid control To select a variable number of rows we use a template VBScript file “select_grid_rows.vbs”: Set aw = session.activeWindow() In the GuiXT script we use “ApplyGu Set V[grid_rows] “1,3,5″
8 Example: Reading grid cells We want to offer a new entry field “Search for” where the user can specify a short text (or a part of the text) to search for. When the user presses a pushbutton “Select rows” we select all grid rows that contain the specified text:
The scripts look as follows: GuiXT script: pos X[GRID1] (2,0) InputScript “grid_select_rows.txt”: Screen saplslvc_fullscreen.0500 VBScript “select_grid_rows_via_short_text.vbs“: Set aw = session.activeWindow() Dim myselectedRows for i = 0 to GRID1.rowCount – 1 if myselectedRows = “” then end if GRID1.selectedRows = myselectedRows Please observe that, in contrast to the table control, the grid control does not require one to scroll through it page by page. All rows are already accessible without scrolling. You can also scroll the display so that the first selected line is shown: Set aw = session.activeWindow() Dim myselectedRows for i = 0 to GRID1.rowCount – 1 if myselectedRows = “” then if i < GRID1.firstVisibleRow or i > GRID1.firstVisibleRow + GRID1.visibleRowCount-1 then end if GRID1.selectedRows = myselectedRows 9 Example: Returning values from VBScript Returning values from VBScript is not yet fully supported. You have to use a temporary file to pass the value. In addition please bear in mind that the VBScript is not processed synchronously with the GuiXT script or InputScript. You have to wait for the next screen display before you can read the file generated in VBScript. As a demonstration of this technique we enhance the last example, returning the number of selected grid lines:
The scripts look as follows: GuiXT script: pos X[GRID1] (2,0) InputScript “grid_select_rows.txt”: Screen saplslvc_fullscreen.0500 Screen saplslvc_fullscreen.0500 VBScript “select_grid_rows_via_short_text.vbs“: Set aw = session.activeWindow() Dim myselectedRows for i = 0 to GRID1.rowCount – 1 if myselectedRows = “” then if i < GRID1.firstVisibleRow or i > GRID1.firstVisibleRow + GRID1.visibleRowCount-1 then else mycount = mycount + 1
end if GRID1.selectedRows = myselectedRows
10 Example: Collecting data from selected rows This example shows how we can collect data from all selected grid rows. We generate a simple file containing the order numbers of all selected grid rows. We include this example since the handling of intervals like “3,10,12-20,34″, the format that SAP GUI returns for the selected grid rows, has to be done in VBScript. The script still refers to the IW38 grid. VBScript Set aw = session.ActiveWindow Dim oFS sel = Split(GRID1.selectedRows, “,”) oTS.Close
11 Example: Activate object services
In the toolbar we want to add a new pushbutton that calls up a function from the “object services menu”. Since there are no normal menu entries for these special functions, a short VBScript is needed. In this case the VBScript leads to an application server request, so that we have to omit the “Enter” in the InputScript.
GuiXT script: Pushbutton (toolbar) “@FM@Send with note” process=”send_with_note.txt” InputScript “send_with_note.txt”: Screen sapmv45a.0100 VBScript “object_services_1.vbs”: session.findById(“wnd[0]/titl/shellcont/shell”).pressButton “%GOS_TOOLBOX” |