Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 665
Esir Kings Avatar asked Jan 25 '26 12:01

Esir Kings


1 Answers

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:

  • batch_update in class gspread.models.Worksheet(spreadsheet, properties)
  • batch_update in class gspread.models.Spreadsheet(client, properties)
like image 114
Tanaike Avatar answered Jan 27 '26 00:01

Tanaike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!