Error while 'writing' into a google spread

2019-07-04 21:04发布

问题:

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