【问题标题】:Cannot do batch_update on Google Sheet with more than 999 rows无法在超过 999 行的 Google Sheet 上执行 batch_update
【发布时间】:2020-10-21 20:52:28
【问题描述】:

尝试将 batch_update 发布到 Google 工作表时出现以下错误。我要发布到的工作表中有 5600 行

('/home/xx/xxx/xx.csv', <Spreadsheet u'spreadsheet name' id:id#>, 'A5600')
Traceback (most recent call last):
  File "xx.py", line 50, in <module>
    pasteCsv(csvFile, sheet, cell)
  File "xx.py", line 38, in pasteCsv
    return sheet.batch_update(body)
  File "/home/xx/.local/lib/python2.7/site-packages/gspread/models.py", line 146, in batch_update
    'post', SPREADSHEET_BATCH_UPDATE_URL % self.id, json=body
  File "/home/xx/.local/lib/python2.7/site-packages/gspread/client.py", line 73, in request
    raise APIError(response)
gspread.exceptions.APIError: {u'status': u'INVALID_ARGUMENT', u'message': u'Invalid requests[0].pasteData: GridCoordinate.rowIndex[5599] is after last row in grid[999]', u'code': 400}

有没有办法将网格从 [999] 更改为更高的数字,以便我能够发布 csv 文件内容?

【问题讨论】:

  • 我认为当你提供你当前的脚本时,它会帮助用户思考解决方案。

标签: python google-sheets-api gspread


【解决方案1】:

答案:

您可以在插入 CSV 内容之前发出批处理请求以增加工作表中的行数。

使用批处理请求的示例:

spreadsheetId = "your-spreadsheet-id"
sheetId = "sheet-id"

sh = client.open_by_key(spreadsheetId)

request = {
    "requests": [
        {
            "insertDimension": {
                "range": {
                  "sheetId": sheetId,
                  "dimension": "ROWS",
                  "startIndex": 999,
                  "endIndex": 5599
                },
                "inheritFromBefore": false
            }
        }
    ]
}

result = sh.batch_update(request)

您需要将 sheetId 更改为您正在更新的电子表格中的工作表的 gid。

记住:行和列是 0 索引的,因此在第 1000 行下方插入行将意味着 startIndex 为 999。

使用 gspread 方法的示例:

或者在 gspread 中你可以直接使用gspread.models.Worksheet.add_rows() 方法:

sh = client.open_by_key(spreadsheetId)
ws = sh.get_worksheet(index)
ws.add_rows(4600)

参考资料:

【讨论】:

  • 添加后得到这个: raise APIError(response) gspread.exceptions.APIError: {u'status': u'INVALID_ARGUMENT', u'message': u"'requests[0] 处的值无效]' (oneof), oneof 字段 'kind' 已设置。无法设置 'insertDimension'", u'code': 400, u'details': [{u'fieldViolations': [{u'field': u' requests[0]', u'description': u"'requests[0]' (oneof) 的值无效,oneof 字段 'kind' 已设置。无法设置 'insertDimension'"}], u'@type': u'type.googleapis.com/google.rpc.BadRequest'}]}
  • 我使用 gspread 方法更新了我的答案。 @MykolaZayats。请让我知道这是否有效。
  • 在发布修复之前通过 gspread 方法添加一行。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
相关资源
最近更新 更多