Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法在超过999行的Google工作表上执行批处理更新_Python_Google Sheets Api_Gspread - Fatal编程技术网

Python 无法在超过999行的Google工作表上执行批处理更新

Python 无法在超过999行的Google工作表上执行批处理更新,python,google-sheets-api,gspread,Python,Google Sheets Api,Gspread,尝试向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)

尝试向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}
('/home/xx/xxx/xx.csv',A5600')
回溯(最近一次呼叫最后一次):
文件“xx.py”,第50行,在
粘贴CSV(CSV文件、图纸、单元格)
文件“xx.py”,第38行,以pasteCsv格式
退货单。批次更新(正文)
文件“/home/xx/.local/lib/python2.7/site packages/gspread/models.py”,第146行,在批更新中
“post”,电子表格\u批处理\u更新\u URL%self.id,json=body
文件“/home/xx/.local/lib/python2.7/site packages/gspread/client.py”,第73行,在请求中
引发错误(响应)
gspread.exceptions.APIRROR:{u'status':u'INVALID_ARGUMENT',u'message':u'INVALID requests[0]。pasteData:GridCoordinate.rowIndex[5599]位于网格[999]的最后一行之后,u'code':400}
是否有办法将网格从[999]更改为更高的数字,以便我能够发布csv文件内容?

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

使用批处理请求的示例:
spreadsheetId=“您的电子表格id”
sheetId=“图纸id”
sh=客户端。按密钥打开(电子表格ID)
请求={
“请求”:[
{
“insertDimension”:{
“范围”:{
“sheetId”:sheetId,
“维度”:“行”,
“startIndex”:999,
“endIndex”:5599
},
“inheritFromBefore”:false
}
}
]
}
结果=sh.batch\u更新(请求)
您需要将
sheetId
更改为要更新的电子表格中工作表的gid

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

使用gspread方法的示例: 或者,在gspread中,您可以直接使用
gspread.models.Worksheet.add_rows()
方法:

sh=client.open\u by\u key(电子表格ID)
ws=sh.get_工作表(索引)
ws.add_行(4600)
我希望这对你有帮助

参考资料:

我认为,当您提供当前脚本时,它将帮助用户考虑解决方案。添加该脚本后得到:raise APIRROR(response)gspread.exceptions.APIRROR:{u'status':u'INVALID_ARGUMENT',u'message':u“requests[0]处的值无效”(oneof),字段'kind'中的一个已经设置。无法设置'insertDimension'”,u'code':400,u'details':[{u'fieldinvalices':[{u'fieldinvalices':u'requests[0]”,u'description':u“requests[0]'处的无效值(其中一个),字段'kind'中的一个已经设置。无法设置'insertDimension'}],u'@type':u'type.googleapis.com/google.rpc.BadRequest'}}我用gspread方法更新了我的答案@MykolaZayats。请让我知道这是否有效。在发布前通过gspread方法添加行修复了它。谢谢