1- """This module provides static functions to peform common tasks with SAP GuiGridView COM objects."""
1+ """This module provides static functions to perform common tasks with SAP GuiGridView COM objects."""
22
33def scroll_entire_table (grid_view , return_to_top = False ) -> None :
44 """This function scrolls through the entire table to load all cells.
@@ -9,13 +9,13 @@ def scroll_entire_table(grid_view, return_to_top=False) -> None:
99
1010 Returns:
1111 _type_: _description_
12- """
12+ """
1313 if grid_view .RowCount == 0 or grid_view .VisibleRowCount == 0 :
1414 return
15-
15+
1616 for i in range (0 , grid_view .RowCount , grid_view .VisibleRowCount ):
1717 grid_view .FirstVisibleRow = i
18-
18+
1919 if return_to_top :
2020 grid_view .FirstVisibleRow = 0
2121
@@ -30,7 +30,7 @@ def get_all_rows(grid_view, pre_load=True) -> tuple[tuple[str]]:
3030
3131 Returns:
3232 tuple[tuple[str]]: A 2D tuple of all cell values in the gridview.
33- """
33+ """
3434
3535 if pre_load :
3636 scroll_entire_table (grid_view , True )
@@ -45,9 +45,9 @@ def get_all_rows(grid_view, pre_load=True) -> tuple[tuple[str]]:
4545 for c in columns :
4646 v = grid_view .GetCellValue (r , c )
4747 row_data .append (v )
48-
48+
4949 output .append (tuple (row_data ))
50-
50+
5151 return tuple (output )
5252
5353
@@ -62,7 +62,7 @@ def get_row(grid_view, row:int, scroll_to_row=False) -> tuple[str]:
6262
6363 Returns:
6464 tuple[str]: A tuple of the row's data.
65- """
65+ """
6666
6767 if scroll_to_row :
6868 grid_view .FirstVisibleRow = row
@@ -85,7 +85,7 @@ def iterate_rows(grid_view) -> tuple[str]:
8585
8686 Yields:
8787 tuple[str]: A tuple of the next row's data.
88- """
88+ """
8989
9090 row = 0
9191 while row < grid_view .RowCount :
@@ -106,7 +106,7 @@ def get_column_titles(grid_view) -> tuple[str]:
106106
107107 Returns:
108108 tuple[str]: A tuple of the gridview's column titles.
109- """
109+ """
110110
111111 return tuple (grid_view .GetColumnTitles (c )[0 ] for c in grid_view .ColumnOrder )
112112
@@ -125,7 +125,7 @@ def find_row_index_by_value(grid_view, column:str, value:str) -> int:
125125
126126 Returns:
127127 int: The index of the first row which column value matches the given value.
128- """
128+ """
129129
130130 if column not in grid_view .ColumnOrder :
131131 raise ValueError (f"Column '{ column } ' not in grid_view" )
@@ -134,14 +134,14 @@ def find_row_index_by_value(grid_view, column:str, value:str) -> int:
134134 # Only scroll when row isn't visible
135135 if not grid_view .FirstVisibleRow <= row <= grid_view .FirstVisibleRow + grid_view .VisibleRowCount - 1 :
136136 grid_view .FirstVisibleRow = row
137-
137+
138138 if grid_view .GetCellValue (row , column ) == value :
139139 return row
140-
140+
141141 return - 1
142142
143- def find_all_row_indecies_by_value (grid_view , column :str , value :str ) -> list [int ]:
144- """Find all indecies of the rows where the given column's value
143+ def find_all_row_indices_by_value (grid_view , column :str , value :str ) -> list [int ]:
144+ """Find all indices of the rows where the given column's value
145145 match the given value. Returns an empty list if no row is found.
146146
147147 Args:
@@ -153,8 +153,8 @@ def find_all_row_indecies_by_value(grid_view, column:str, value:str) -> list[int
153153 ValueError: If the column name doesn't exist in the grid view.
154154
155155 Returns:
156- list[int]: A list of row indecies where the value matches.
157- """
156+ list[int]: A list of row indices where the value matches.
157+ """
158158 if column not in grid_view .ColumnOrder :
159159 raise ValueError (f"Column '{ column } ' not in grid_view" )
160160
@@ -164,39 +164,8 @@ def find_all_row_indecies_by_value(grid_view, column:str, value:str) -> list[int
164164 # Only scroll when row isn't visible
165165 if not grid_view .FirstVisibleRow <= row <= grid_view .FirstVisibleRow + grid_view .VisibleRowCount - 1 :
166166 grid_view .FirstVisibleRow = row
167-
167+
168168 if grid_view .GetCellValue (row , column ) == value :
169169 rows .append (row )
170-
171- return rows
172-
173-
174-
175-
176- if __name__ == '__main__' :
177- import win32com .client
178-
179- SAP = win32com .client .GetObject ("SAPGUI" )
180- app = SAP .GetScriptingEngine
181- connection = app .Connections (0 )
182- session = connection .Sessions (0 )
183-
184- table = session .findById ("wnd[0]/usr/cntlGRID1/shellcont/shell" )
185-
186- rows = find_all_row_indecies_by_value (table , "ZZ_PARTNER" , '15879880' )
187- print (rows )
188- table .setCurrentCell (rows [0 ], "ZZ_PARTNER" )
189-
190- # print(get_row(table, 1, True))
191-
192- # scroll_entire_table(table)
193-
194- # data = get_all_rows(table)
195- # print(len(data), len(data[0]))
196-
197- # for r in iterate_rows(table):
198- # print(r)
199-
200- # print(get_column_titles(table))
201-
202170
171+ return rows
0 commit comments