Purpose
Read an SAP grid control with ApplyGuiScript

Solution
We start a VBScript with the ApplyGuiScript statement. In the VBScript we use SAP GUI scripting to read the grid content. For returning values to GuiXT we build up an intermediate file.

Remark
In many cases it is easier to use the "Native Control Interface" of GuiXT Control instead of using SAP GUI scripting.  Especially when dealing with large grid controls, the "lazy loading" strategy of SAP GUI is difficult to handle in the scripting (see example below).
See also the following tips for alternatives.

Example
In transaction IW38 (plant maintenance orders) we 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. In addition, we display the number of matching entries in the message line: 

 

GuiXT Script

if Q[transaction=IW38]
 
 
  
// determine height of grid conrol
   GetFieldAttribute  X[GRID1]  height=h

   // shift grid control 2 rows and decrease its height
   Set V[h] &V[h] - 2
   pos X[GRID1] (2,0) height=&V[h]

  InputField (0,0) "Search for" (0,12) size=20 name="pm_short_text"
  Pushbutton (0,34) "Select rows" process="grid_select_rows.txt"
endif


InputScript "grid_select_rows.txt"

Screen saplslvc_fullscreen.0500

  // set a temporary filename to return the row count from VBScript
  Set V[return_filename] "&%[TEMP]\guixtscriptreturn.txt"

  // since VBScript works with ANSI literals, we need to convert the searchstring
  // otherwise e.g. Umlauts .,... are not found in the grid
 
set text[searchstring] "&V[pm_short_text]"
  ConvertText "searchstring" fromCodepage="UTF-8" toCodepage="ANSI"
  Set V[searchstring] "&text[searchstring]"

  // start VBScript
  // we use template= to have the GUiXT variables replaced in the VBScript file
  ApplyGuiScript template="select_grid_rows_via_short_text.vbs"

  Enter

label repeat_during_scrolling
Screen
saplslvc_fullscreen.0500

  // read the number of rows found and display a message
  OpenFile "&V[return_filename]"

  if Q[ok]
    ReadFile "&V[return_filename]" pm_selcount
    CloseFile "&V[return_filename]"
    Message "I: &V[pm_selcount] rows with short text ""&V[pm_short_text]"""  _                 -statusline

    RemoveFile "&V[return_filename]"
    Enter

  else

    // for large grids, SAP GUI uses "lazy loading" when scrolling and this
    // processes a "Screen" block in tthe InputScript!

   
goto
repeat_during_scrolling

   endif

 

VBScript "select_grid_rows_via_short_text.vbs"

Set
aw=session.activeWindow()
Set
GRID1=aw.FindById("usr/cntlGRID1/shellcont/shell")

Dim
myselectedRows
myselectedRows
=""
Dim
mycount
mycount
=0

Dim
firstLineFound
firstLineFound
=0

For
i=0ToGRID1.rowCount-1
    
    'weneedtosetthe
visiblerow,otherwisetherowcontentmightbe      emptyforlargegrids!
    Ifi<GRID1.firstVisibleRowOri>GRID1.firstVisibleRow                  +   GRID1.visibleRowCount-1Then
        GRID1.firstVisibleRow=i
    EndIf

    

    IfInStr(1,GRID1.getCellValue(i,"KTEXT"),"&V[searchstring]",1)>0 Then
        
        IfmyselectedRows=""Then
            myselectedRows=CStr(i)
        Else
            myselectedRows=myselectedRows+","+CStr(i)
        EndIf
        
        IffirstLineFound=0Then
            firstLineFound=i
        EndIf  
        
        mycount=mycount+1
        
    EndIf
Next

GRID1.selectedRows
=myselectedRows

If
firstLineFound>0Then
    GRID1.firstVisibleRow=firstLineFound
End
If  

'
Generatereturnfile
Dim
oFS
Set
oFS=CreateObject("Scripting.FileSystemObject")
Dim
oTS
Set
oTS=oFS.CreateTextFile("&V[return_filename]")
oTS.writeLine(CStr(mycount))
oTS.Close

 

 

Components
InputAssistant