Google Sheets API Python-清除工作表

Cou*_*er0 4 python google-api google-sheets-api

我想不断更新/重写为Google工作表。但是,我不能不更新旧表就更新它,因为有时更新的行少于表中的前一行和旧行。

因此,开发人员页面上列出的协议为:

{
  "requests": [
    {
      "updateCells": {
        "range": {
          "sheetId": sheetId
        },
        "fields": "userEnteredValue"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我认为翻译成python看起来像这样:

requests = [{ 'updateCells': { 'range': { 'sheetId': spreadsheet_id }, 'fields': 'userEnteredValue' } }]

body = { 'requests': requests }
spreadsheet_id='[uniqueIDhere]'

result = service.spreadsheets( ).values( ).batchUpdate(spreadsheetId=spreadsheet_id, body=body ).execute( )
Run Code Online (Sandbox Code Playgroud)

返回错误:

googleapiclient.errors.HttpError:https://sheets.googleapis.com/v4/spreadsheets/[uniqueIDhere]/values:batchUpdate?alt=json返回“接收到无效的JSON有效负载。未知名称“ requests”:找不到字段。“>

似乎很奇怪,“请求”无效,因为它在协议中列出了。无论如何,还有其他人可以使用它吗?谢谢。-杰森

Cou*_*er0 7

找到了另一种方法

rangeAll = '{0}!A1:Z'.format( sheetName )
body = {}
resultClear = service.spreadsheets( ).values( ).clear( spreadsheetId=spreadsheet_id, range=rangeAll,
                                                       body=body ).execute( )
Run Code Online (Sandbox Code Playgroud)

这很好。仍然想知道为什么requests-updateCells协议不起作用。