1

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()
2
  • 1
    chr(65+next_col) would only give upto Z. It won't give AA etc. See stackoverflow.com/questions/21229180. Also provide minimal reproducible example: What's up with this block if last_col < 25:? Commented Mar 30 at 3:16
  • 1
    Maybe first use print() (and print(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. Commented Mar 30 at 11:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.