【问题标题】:Error while 'writing' into a google spreadsheet using Google API使用 Google API“写入”到 Google 电子表格时出错
【发布时间】:2018-03-20 10:53:12
【问题描述】:

使用的代码

  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 电子表格的访问设置,以允许知道其链接的任何人查看和编辑它。仅当我尝试写入电子表格时才会出现此错误,我可以使用其他功能读取电子表格。

【问题讨论】:

    标签: javascript google-spreadsheet-api


    【解决方案1】:

    没有人知道您将哪些域列入白名单,因此没有人可以复制您的代码。而且您的电子表格没有 Class Data 工作表,并且您错误地尝试写在 A1:C1 而不是 A1:A4

    所以我添加了一张表并更改了values 并尝试使用API explorer。结果是200 OK。也许你可以试试'RAW' 代替"RAW" 或/和values: values 代替resource: body

    【讨论】:

    • 我已将 'Class Data' 更改为 'Sheet1'(即使它在 'get' 函数中没有造成任何问题)。我尝试使用 'RAW' 而不是 "RAW “没有任何错误变化。我还将'A1:C1'更改为'A1:A4'(但这不是错误的原因,它可能使剩余的单元格为空)。除此之外,我们应该为“资源”分配一个包含我们要插入的值的对象,对吧?那么,我到底应该使用什么来代替“资源:正文”?
    • 正如我所说,只需将您当前代码中的resource: body 替换为values: values。你试过吗?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 2011-05-11
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      • 2019-04-18
      相关资源
      最近更新 更多