3

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?

E_K
  • 2,159
  • 23
  • 39
  • About [your this comment](https://stackoverflow.com/questions/60015321/how-to-reset-all-rows-and-column-data-uisng-python-gspread-sheets/60022818?noredirect=1#comment110843082_60022818), I found your question here. So I would like to propose the modified script as an answer. Could you please confirm it? – Tanaike Jul 02 '20 at 01:10

1 Answers1

5

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:

halfer
  • 19,824
  • 17
  • 99
  • 186
Tanaike
  • 181,128
  • 11
  • 97
  • 165