【问题标题】:spreadsheet API multiply two columns and write the results as another column with python电子表格 API 将两列相乘并将结果写入 python 的另一列
【发布时间】:2021-10-05 14:40:48
【问题描述】:

我正在尝试将 C 列和 E 列相乘并将结果写入 G 列,但我无法弄清楚。你知道我该怎么做吗?

request_body ={
  "requests": [
    {
      "repeatCell": {
        "range": {
          "sheetId": spreadsheetId,
          "startRowIndex": 2,
          "endRowIndex": 15,
          "startColumnIndex": 7,
          "endColumnIndex": 8
        },
        "cell": {
          "userEnteredValue": {
              "formulaValue": "=FLOOR(E2*C2)"
          }
        },
        "fields": "userEnteredValue"
      }
    }
  ]
}


response = serviceSheets.spreadsheets().values().update(
spreadsheetId=spreadsheetId,
body=request_body
).execute()

【问题讨论】:

  • 你当前的脚本有什么问题?
  • 我收到此错误并且无法解决 HttpError:
  • 当我将方法更改为 batchUpdate 时,我收到此错误:'requests[0].repeat_cell.range.sheet_id' (TYPE_INT32), "1Iknox10drdCsOXETvLyyahPFjs2s24UIci40ilbpzZA""。详细信息:"[{'@type' : 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'requests[0].repeat_cell.range.sheet_id', 'description': 'requests[ 中的值无效0].repeat_cell.range.sheet_id\' (TYPE_INT32), "1Iknox10drdCsOXETvLyyahPFjs2s24UIci40ilbpzZA"'}]}]">
  • 电子表格和工作表是不同的东西。表意味着一个选项卡。 id 将出现在#gridid= 之后的 URL 中。还有edit你的错误问题

标签: python google-sheets google-sheets-api


【解决方案1】:

您的代码中的问题是您使用的spreadsheets.values.batchUpdate 在其Request body 中不包含repeatCell 请求

如果你想使用RepeatCellRequest,你需要使用spreadsheets.batchUpdate

此外,正如@TheMaster 所提到的,您使用的sheetId 的ID 不正确,它应该是在您查看目标工作表选项卡时在URL 中找到的gid。还请记住@ 987654325@ 从zero-index 开始。并且结束索引被排除在外。

示例代码:

request_body ={
  "requests": [
    {
      "repeatCell": {
        "range": {
          "sheetId": spreadsheetId,
          "startRowIndex": 0,
          "endRowIndex": 15,
          "startColumnIndex": 7,
          "endColumnIndex": 8
        },
        "cell": {
          "userEnteredValue": {
              "formulaValue": "=FLOOR(E2*C2)"
          }
        },
        "fields": "userEnteredValue"
      }
    }
  ]
}


response = serviceSheets.spreadsheets().batchUpdate(
spreadsheetId=spreadsheetId,
body=request_body
).execute()

输出:

  • 由于我使用 endRowIndex = 15,因此未设置单元格 H16 公式。

【讨论】:

  • 如果您在实施过程中遇到一些问题,请告诉我。我只使用 API Explorer 来测试示例请求体。
  • 天哪,你和大师真的救了我的命!非常感谢你。问题是 spreed_id。我使用 credentials.json 文件授权了该应用程序,所以我认为电子表格 ID 就足够了。当我更改 sheetId 时,它起作用了!谢谢你们 。万事如意!
猜你喜欢
  • 2022-01-01
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多