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.
55
66 Args:
77 grid_view: A SAP GuiGridView object.
88 return_to_top: Whether to return the table to the first row after scrolling. Defaults to False.
9-
10- Returns:
11- _type_: _description_
12- """
9+ """
10+ if grid_view .RowCount == 0 or grid_view .VisibleRowCount == 0 :
11+ return
1312
1413 for i in range (0 , grid_view .RowCount , grid_view .VisibleRowCount ):
1514 grid_view .FirstVisibleRow = i
16-
15+
1716 if return_to_top :
1817 grid_view .FirstVisibleRow = 0
1918
@@ -28,7 +27,7 @@ def get_all_rows(grid_view, pre_load=True) -> tuple[tuple[str]]:
2827
2928 Returns:
3029 tuple[tuple[str]]: A 2D tuple of all cell values in the gridview.
31- """
30+ """
3231
3332 if pre_load :
3433 scroll_entire_table (grid_view , True )
@@ -43,9 +42,9 @@ def get_all_rows(grid_view, pre_load=True) -> tuple[tuple[str]]:
4342 for c in columns :
4443 v = grid_view .GetCellValue (r , c )
4544 row_data .append (v )
46-
45+
4746 output .append (tuple (row_data ))
48-
47+
4948 return tuple (output )
5049
5150
@@ -60,7 +59,7 @@ def get_row(grid_view, row:int, scroll_to_row=False) -> tuple[str]:
6059
6160 Returns:
6261 tuple[str]: A tuple of the row's data.
63- """
62+ """
6463
6564 if scroll_to_row :
6665 grid_view .FirstVisibleRow = row
@@ -83,7 +82,7 @@ def iterate_rows(grid_view) -> tuple[str]:
8382
8483 Yields:
8584 tuple[str]: A tuple of the next row's data.
86- """
85+ """
8786
8887 row = 0
8988 while row < grid_view .RowCount :
@@ -104,7 +103,7 @@ def get_column_titles(grid_view) -> tuple[str]:
104103
105104 Returns:
106105 tuple[str]: A tuple of the gridview's column titles.
107- """
106+ """
108107
109108 return tuple (grid_view .GetColumnTitles (c )[0 ] for c in grid_view .ColumnOrder )
110109
@@ -123,7 +122,7 @@ def find_row_index_by_value(grid_view, column:str, value:str) -> int:
123122
124123 Returns:
125124 int: The index of the first row which column value matches the given value.
126- """
125+ """
127126
128127 if column not in grid_view .ColumnOrder :
129128 raise ValueError (f"Column '{ column } ' not in grid_view" )
@@ -132,14 +131,14 @@ def find_row_index_by_value(grid_view, column:str, value:str) -> int:
132131 # Only scroll when row isn't visible
133132 if not grid_view .FirstVisibleRow <= row <= grid_view .FirstVisibleRow + grid_view .VisibleRowCount - 1 :
134133 grid_view .FirstVisibleRow = row
135-
134+
136135 if grid_view .GetCellValue (row , column ) == value :
137136 return row
138-
137+
139138 return - 1
140139
141- def find_all_row_indecies_by_value (grid_view , column :str , value :str ) -> list [int ]:
142- """Find all indecies of the rows where the given column's value
140+ def find_all_row_indices_by_value (grid_view , column :str , value :str ) -> list [int ]:
141+ """Find all indices of the rows where the given column's value
143142 match the given value. Returns an empty list if no row is found.
144143
145144 Args:
@@ -151,8 +150,8 @@ def find_all_row_indecies_by_value(grid_view, column:str, value:str) -> list[int
151150 ValueError: If the column name doesn't exist in the grid view.
152151
153152 Returns:
154- list[int]: A list of row indecies where the value matches.
155- """
153+ list[int]: A list of row indices where the value matches.
154+ """
156155 if column not in grid_view .ColumnOrder :
157156 raise ValueError (f"Column '{ column } ' not in grid_view" )
158157
@@ -162,39 +161,8 @@ def find_all_row_indecies_by_value(grid_view, column:str, value:str) -> list[int
162161 # Only scroll when row isn't visible
163162 if not grid_view .FirstVisibleRow <= row <= grid_view .FirstVisibleRow + grid_view .VisibleRowCount - 1 :
164163 grid_view .FirstVisibleRow = row
165-
164+
166165 if grid_view .GetCellValue (row , column ) == value :
167166 rows .append (row )
168-
169- return rows
170-
171-
172-
173-
174- if __name__ == '__main__' :
175- import win32com .client
176-
177- SAP = win32com .client .GetObject ("SAPGUI" )
178- app = SAP .GetScriptingEngine
179- connection = app .Connections (0 )
180- session = connection .Sessions (0 )
181-
182- table = session .findById ("wnd[0]/usr/cntlGRID1/shellcont/shell" )
183-
184- rows = find_all_row_indecies_by_value (table , "ZZ_PARTNER" , '15879880' )
185- print (rows )
186- table .setCurrentCell (rows [0 ], "ZZ_PARTNER" )
187-
188- # print(get_row(table, 1, True))
189-
190- # scroll_entire_table(table)
191-
192- # data = get_all_rows(table)
193- # print(len(data), len(data[0]))
194-
195- # for r in iterate_rows(table):
196- # print(r)
197-
198- # print(get_column_titles(table))
199-
200167
168+ return rows
0 commit comments