电子表格 API 将两列相乘并将结果写入另一列 python

spreadsheet API multiply two columns and write the results as another column with python

我正在尝试将 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()

您的代码中的问题是您使用的是 spreadsheets.values.batchUpdate which doesn't contain a repeatCell request in its Request body

您需要使用spreadsheets.batchUpdate if you want to use a RepeatCellRequest

此外,正如@TheMaster所述,您使用了错误的id作为您的sheetId,它应该是您查看目标时在URL中找到的gid sheet tab.Please 还要记住 GridRangezero-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 公式。