From c5fa638d09f14906b7ad0d98b7bd78cd252e427f Mon Sep 17 00:00:00 2001 From: Alexandre Lavigne Date: Wed, 31 Jul 2024 20:14:50 +0200 Subject: [PATCH] fixup! update `find_table` to return correct behaviour --- gspread/utils.py | 27 +++++++++++++-------------- tests/utils_test.py | 10 +++++----- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/gspread/utils.py b/gspread/utils.py index 1443bead0..5e389ea58 100644 --- a/gspread/utils.py +++ b/gspread/utils.py @@ -986,30 +986,26 @@ def to_records( def _expand_right(values: List[List[str]], start: int, end: int, row: int) -> int: - """This is a private function, returning the column index of the first empty cell + """This is a private function, returning the column index of the last non empty cell on the given row. Search starts from ``start`` index column. Search ends on ``end`` index column. Searches only in the row pointed by ``row``. - - If no empty value is found, it will return the given ``end`` index. """ try: - return values[row].index('""', start, end) + return values[row].index("", start, end) - 1 except ValueError: return end def _expand_bottom(values: List[List[str]], start: int, end: int, col: int) -> int: - """This is a private function, returning the row index of the first empty cell + """This is a private function, returning the row index of the last non empty cell on the given column. Search starts from ``start`` index row. Search ends on ``end`` index row. Searches only in the column pointed by ``col``. - - If no empty value is found, it will return the given ``end`` index. """ for rows in range(start, end): # in case we try to look further than last row @@ -1018,9 +1014,9 @@ def _expand_bottom(values: List[List[str]], start: int, end: int, col: int) -> i # check if cell is empty (or the row => empty cell) if col >= len(values[rows]) or values[rows][col] == "": - return rows + return rows - 1 - return end + return end - 1 def find_table( @@ -1076,12 +1072,15 @@ def find_table( col -= 1 if direction == TableDirection.down: - rightMost = col + 1 + rightMost = col bottomMost = _expand_bottom(values, row, len(values), col) if direction == TableDirection.right: - rightMost = _expand_right(values, col, len(values[row]), row) - bottomMost = row + 1 + if row >= len(values): + rightMost = len(values) - 1 + else: + rightMost = _expand_right(values, col, len(values[row]), row) + bottomMost = row if direction == TableDirection.table: bottomMost = _expand_bottom(values, row, len(values), col) @@ -1090,8 +1089,8 @@ def find_table( result = [] # build resulting array - for rows in values[row:bottomMost]: - result.append(rows[col:rightMost]) + for rows in values[row : bottomMost + 1]: + result.append(rows[col : rightMost + 1]) return result diff --git a/tests/utils_test.py b/tests/utils_test.py index 19ab245ae..f986d9bd5 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -532,8 +532,8 @@ def test_find_table_simple(self): no_values = utils.find_table(values, "A2", utils.TableDirection.table) table_values = [ - ["B2", "C2"], - ["B3", "C3"], + ["B2", "C2", "", "E2"], + ["B3", "C3", "D3", "E3"], ] for rowindex, row in enumerate(table): self.assertListEqual(row, table_values[rowindex]) @@ -563,9 +563,9 @@ def test_find_table_header_gap(self): ["", "", "", ""], ] expected_table = [ - ["A1"], - ["A2"], - ["A3"], + ["A1", "", "C1"], + ["A2", "B2", "C2"], + ["A3", "B3", "C3"], ] table = utils.find_table(