Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index error occur in worksheet.get_values(combine_merged_cells = True) #1298

Closed
lovelyunsh opened this issue Sep 12, 2023 · 2 comments · Fixed by #1299
Closed

Index error occur in worksheet.get_values(combine_merged_cells = True) #1298

lovelyunsh opened this issue Sep 12, 2023 · 2 comments · Fixed by #1299
Assignees
Labels

Comments

@lovelyunsh
Copy link

Describe the bug
If the last row or column in the worksheet is merged_cell,
IndexError occurs when performing worksheet.get_values(combine_merged_cells = True).
image
or
image

To Reproduce
Steps to reproduce the behavior:

  1. Create worksheet with merged cell
  2. Call up worksheet to get_worksheet()
  3. performing worksheet.get_values(combine_merged_cells = True)

Expected behavior
worksheet.get_values(combine_merged_cells = True)
return
[['merge-test1'], ['merge-test1'], ['merge-test2'], ['merge-test2']]

Code example*
If applicable, provide a code example to help explain your problem.

ws = spreadsheet.get_worksheet(0)
ws.get_values(combine_merged_cells = True)

Environment info:

  • Operating System [e.g. Linux, Windows, macOS]: Windows
  • Python version : 3.9.7
  • gspread version : 5.10.0

Stack trace or other output that would be helpful

Traceback (most recent call last):
  File "C:\Users\lovel\Desktop\test\gspreadtest.py", line 17, in <module>
    ws.get_values(combine_merged_cells = True)
  File "C:\Users\lovel\AppData\Local\Programs\Python\Python39\lib\site-packages\gspread\utils.py", line 705, in wrapper
    return f(*args, **kwargs)
  File "C:\Users\lovel\AppData\Local\Programs\Python\Python39\lib\site-packages\gspread\worksheet.py", line 456, in get_values
    return combined_merge_values(worksheet_meta, vals)
  File "C:\Users\lovel\AppData\Local\Programs\Python\Python39\lib\site-packages\gspread\utils.py", line 742, in combined_merge_values
    new_values[row_index][col_index] = top_left_value
IndexError: list assignment index out of range

Additional context
The list brought by worksheet.get() has only the size of the merged cell up to the left-top with actual data, and the "merges" of the meta data has an index that exceeds that size, resulting in an error.

@lovelyunsh lovelyunsh changed the title Index error occur in worksheet.get_values(combine_merged_cells = True). Index error occur in worksheet.get_values(combine_merged_cells = True) Sep 12, 2023
@alifeee
Copy link
Collaborator

alifeee commented Sep 12, 2023

Hi, this is a good spot. This is an edge case I had not considered when implementing combine_merge_cells (in #1215). It is actually more than just being on the edge, but if you call any range (e.g., "A1:B2") and there are merged cells outside this range, then the error will happen. oops!

The problem is related to the Google API only returning rows/cols with values, so the range is smaller than the one asked for. (see #1289)

We will look into fixing this issue.

It may take a while for us to look into, so for now, I suggest this "quick fix":

import gspread

gc = gspread.service_account(filename="google_credentials.json")
spreadsheet = gc.open_by_key("<spreadsheet_id>")
worksheet = spreadsheet.worksheet("<worksheet_title>")

ROWS = worksheet.row_count
COLUMNS = worksheet.column_count

spreadsheet_meta = spreadsheet.fetch_sheet_metadata()
worksheet_meta = next(
    x for x in spreadsheet_meta["sheets"] if x["properties"]["title"] == worksheet.title
)

values = worksheet.get_values()
values_padded = gspread.utils.fill_gaps(values, rows=ROWS, cols=COLUMNS)
values_combined = gspread.utils.combined_merge_values(worksheet_meta, values_padded)

print(values_combined[:10])  # first 10 rows only

@alifeee
Copy link
Collaborator

alifeee commented Sep 18, 2023

This should be fixed by the newest release, v5.11.2.

Please try reinstalling gspread and let me know if there is still an issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants