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

只谈情不闲聊 提交于 2019-12-10 21:49:09

问题


Code used

  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 shown

    {
      "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."
              }
            ]
          }
        ]
      }
    }

I've changed access settings of the google spreadsheet to allow anyone with it's link to view and edit it. This error is arising only when I'm trying to write into the spreadsheet, I'm able to read a spreadsheet using other functions.


回答1:


No one knows what domains you white-listed so no one can reproduce your code. And your spreadsheet doesn't have the Class Data sheet and you tried wrongly to write at A1:C1 not A1:A4.

So I added a sheet and changed values and tried with the API explorer. The result was 200 OK. Maybe you can try 'RAW' instead of "RAW" or/and values: values instead of resource: body.




回答2:


I had specified the authorization scope required by the API incorrectly(before the update function was called). Error got fixed after using https://www.googleapis.com/auth/spreadsheets instead of https://www.googleapis.com/auth/spreadsheets.readonly



来源:https://stackoverflow.com/questions/49382301/error-while-writing-into-a-google-spreadsheet-using-google-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!