【问题标题】:Is there an issue on python 3.8 with gsheet batch_update?使用 gsheet batch_update 在 python 3.8 上是否存在问题?
【发布时间】:2020-07-01 14:35:02
【问题描述】:

以下问题和解决方案,how to reset all rows and column data uisng python gspread sheets, 我有以下确切代码

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

但我收到以下错误

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

谁有过这种经历?你是怎么解决的?

【问题讨论】:

  • 关于your this comment,我在这里找到了你的问题。所以我想提出修改后的脚本作为答案。你能确认一下吗?

标签: python python-3.x google-sheets google-sheets-api gspread


【解决方案1】:

修改点:

  • 虽然很遗憾,我在您的问题中看不到您的整个脚本,但从您的错误消息中,我认为您的问题可能是您在 gspread.models.Worksheet 类中使用batch_update 方法。因为在我的环境中,当我测试下面的脚本时,我和你确认了同样的错误。

      worksheet = spreadsheet.worksheet(sheetName)
      requests = {"requests": [{"updateCells": {"range": {"sheetId": worksheet._properties['sheetId']}, "fields": "*"}}]}
      res = worksheet.batch_update(requests)
      print(res)
    
  • 在这种情况下,请使用 gspread.models.Spreadsheet 类中的batch_update 方法。

为了解决这个问题,下面的示例脚本怎么样?

示例脚本:

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)
  • 请设置您的授权脚本。

注意:

  • 我使用 python 3.8.3 和 gspread 3.6.0 测试了上述脚本,我可以确认该脚本有效。

参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2018-03-27
    • 2021-03-28
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2015-02-23
    相关资源
    最近更新 更多