Search code examples
pythonpython-3.xgoogle-sheetsgoogle-sheets-apigspread

Is there an issue on python 3.8 with gsheet batch_update?


Following the following question and solution, how to reset all rows and column data uisng python gspread sheets, I have the following exact code

requests = {"requests": [{"updateCells": {"range": {"sheetId": worksheet._properties['sheetId']}, "fields": "*"}}]}
res = spreadsheet.batch_update(requests)

but I am receiving the following error

File "/root/.local/lib/python3.8/site-packages/gspread/models.py", line 1171, in batch_update data = [ File "/root/.local/lib/python3.8/site-packages/gspread/models.py", line 1172, in <listcomp> dict(vr, range=absolute_range_name(self.title, vr['range'])) TypeError: string indices must be integers

Anyone who has experienced this? and how did you resolve it?


Solution

  • Modification points:

    • Although, unfortunately, I cannot see your whole script in your question, from your error message, I thought that your issue might be that you are using batch_update method in class gspread.models.Worksheet. Because in my environment, when I tested the following script, I confirmed the same error with you.

        worksheet = spreadsheet.worksheet(sheetName)
        requests = {"requests": [{"updateCells": {"range": {"sheetId": worksheet._properties['sheetId']}, "fields": "*"}}]}
        res = worksheet.batch_update(requests)
        print(res)
      
    • In this case, please use batch_update method in class gspread.models.Spreadsheet.

    In order to remove this issue, how about the following sample script?

    Sample script:

    client = gspread.authorize(credentials)
    spreadsheetId = "###"  # Please set Spreadsheet ID.
    sheetName = "###"  # Please set sheet name.
    spreadsheet = client.open_by_key(spreadsheetId)
    worksheet = spreadsheet.worksheet(sheetName)
    requests = {"requests": [{"updateCells": {"range": {"sheetId": worksheet._properties['sheetId']}, "fields": "*"}}]}
    res = spreadsheet.batch_update(requests)
    
    • Please set your authorization script.

    Note:

    • I tested above script with python 3.8.3 and gspread 3.6.0, and I could confirm that the script worked.

    References: