使用 Google API 'writing' 到 google 电子表格时出错

Error while 'writing' into a google spreadsheet using Google API

使用的代码

  var values = [
    [
      'value1','value2','value3'
    ]
  ];
   var body = {
    values: values
  };
  gapi.client.sheets.spreadsheets.values.update({
    spreadsheetId: '1Lofhq9R7X5wzGvO7fMViN8D8q1W3fiNxO5jjP7XL_s0',
    range: 'Sheet1!A1:A4',
    valueInputOption:'RAW',
    resource: body
  }).then((response) => {
    var result = response.result;
    console.log(`${result.updatedCells} cells updated.`);
  });

显示错误

    {
      "error": {
        "code": 400,
        "message": "Invalid JSON payload received. Unknown name \"valueInputOption\": Cannot bind query parameter. Field 'valueInputOption' could not be found in request message.",
        "status": "INVALID_ARGUMENT",
        "details": [
          {
            "@type": "type.googleapis.com/google.rpc.BadRequest",
            "fieldViolations": [
              {
                "description": "Invalid JSON payload received. Unknown name \"valueInputOption\": Cannot bind query parameter. Field 'valueInputOption' could not be found in request message."
              }
            ]
          }
        ]
      }
    }

我已经更改了 google 电子表格的访问设置,以允许拥有它 link 的任何人查看和编辑它。仅当我尝试写入电子表格时才会出现此错误,我能够使用其他函数读取电子表格。

没有人知道您 white-listed 的域是什么,因此没有人可以复制您的代码。你的点差 sheet 没有 Class Data sheet 并且你尝试错误地写在 A1:C1 而不是 A1:A4.

所以我添加了 sheet 并更改了 values 并尝试使用 API explorer。结果是200 OK。也许你可以尝试 'RAW' 而不是 "RAW" or/and values: values 而不是 resource: body.

我错误地指定了 API 所需的授权范围(在调用更新功能之前)。 使用后错误得到修复 https://www.googleapis.com/auth/spreadsheets 代替 https://www.googleapis.com/auth/spreadsheets.readonly