Gspread append_row appending data to different column
Asked Answered
I

1

6

see the output here

I am using append_row to add data to a google sheet... I don't understand why this is happening
#the code
def write_to_dangersheet(flow):
            credentials_file = "file.json"
            scope = ['https://spreadsheets.google.com/feeds',
                     'https://www.googleapis.com/auth/drive']
            credentials = ServiceAccountCredentials.from_json_keyfile_name(credentials_file, scope)
            gc=gspread.authorize(credentials)

            test_wb=gc.open_by_key('yyyyyyyyyyyyyy').worksheet('betaflow')

            f=[phonenumber,date," ",flow,]

            if flow=='webhook-test':
                test_wb.append_row(f)
        write_to_dangersheet(flow)
Innuendo answered 21/3, 2020 at 20:53 Comment(2)
Unfortunately, from your script, your issue cannot be replicated. So can I ask you about the detail method for replicating your issue?Obstetric
See my response in the gspread issue: github.com/burnash/gspread/issues/735Urien
U
10

Use table_range parameter of append_row() to explicitly specify the table range:

worksheet.append_row(['Test1', '', 'Test2'], table_range='A1')

Background:

Worksheet.append_row() method corresponds to Sheets API spreadsheets.values.append.

When spreadsheets.values.append is called, it searches for logical "table" to append a row of values. Values will be appended to the next row of the table, starting with the first column of the table.

Depending on the data in your spreadsheet there could be several potential "tables" (usually separated by empty columns or rows). By default append_row() does not specify which "table" to use and lets Sheets API detect the table implicitly. In cases where there are multiple tables, Sheets API could select the "table" you didn't intend to append to. In such cases, you need to explicitly tell the API which logical "table" you would like to use.

Appending values section of Google Sheets API v4 docs has a good example of multiple tables in a sheet.

Urien answered 22/3, 2020 at 18:30 Comment(1)
gspread append_row documentationLarissa

© 2022 - 2025 — McMap. All rights reserved.