I have data transferred to a Google spreadsheet in a row and after reaching column Z, my data is transferred below and starts with column A and continues below, but I need it to be written to the right
try:
result = sheets_service.spreadsheets().values().get(
spreadsheetId=spreadsheet_id,
range=f"{sheet_name}!A1:Z1000"
).execute()
values = result.get('values', [])
if not values:
next_row = 1
next_col = 0
else:
last_row = len(values) - 1
last_col = max([len(row) for row in values]) - 1 if values else 0
if last_col < 25:
next_row = 1
next_col = last_col + 1
else:
next_row = last_row + 3
next_col = 0
sheets_service.spreadsheets().values().update(
spreadsheetId=spreadsheet_id,
range=f"{sheet_name}!{chr(65 + next_col)}{next_row}:{chr(65 + next_col)}{next_row + len(data) - 1}",
valueInputOption="USER_ENTERED",
body=body
).execute()
chr(65+next_col)would only give uptoZ. It won't giveAAetc. See stackoverflow.com/questions/21229180. Also provide minimal reproducible example: What's up with this blockif last_col < 25:?print()(andprint(type(...)),print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called"print debugging"and it helps to see what code is really doing.