8

I've followed this tutorial and I am able to successfully get it working to "get" data from the spreadsheet, but now I would like to update a single cell using just the JavaScript API. I'm trying to use this JS method:

    gapi.client.sheets.spreadsheets.values.update({
      spreadsheetId: 'something',
      range: 'Sheet1!B2',
    })

But I'm not quite sure how to pass in the new cell value into the Request body. I've tried my best to follow this as a guide but I'm not quite sure how to use this. I can't find any documentation around "gapi.client.sheets.spreadsheets.values.update" JavaScript API. I get 400 errors back from the API with anything I've tried.

1
  • 1
    A good nights rest did me some good; I was able to get it working. Commented Aug 2, 2016 at 11:58

3 Answers 3

13
gapi.client.sheets.spreadsheets.values.update({
    spreadsheetId: 'something',
    range: 'Sheet1!B2',
    valueInputOption: 'USER_ENTERED',
    values: [ ["123"] ]
}).then(function(response) {
    console.log(response);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I am getting error code 403, permission denied with the message: The request cannot be identified with a client project. Please pass a valid API key with the request. Can you please help??
@Tim this should be updated to replace "values" with "resource" as Curtis Chong has below.
7

I think google updated their API because Tim's answer didn't work for me. Here is my structure:

let spreadsheetId = "idOfMySheet";
let range = "nameOfSheet!A1";
let valueInputOption = "RAW";
let myValue = 5;

let values = [[myValue]];
let resource = {
    values,
};
sheets.spreadsheets.values.update({
    spreadsheetId,
    range,
    valueInputOption,
    resource
}, (err, result) => {
    if (err) {
        console.log(err);
    } else {
        console.log('%d cells updated.', result.updatedCells);
    }
});

Comments

0

Check This link

For updating sheet values, authorization is required with non-read-only OAuth scope like https://www.googleapis.com/auth/spreadsheets

and then

 return gapi.client.sheets.spreadsheets.values.update({
        "spreadsheetId": sid,
        "range": sheetName + "!A2:D",
        "includeValuesInResponse": "true",
        "responseDateTimeRenderOption": "FORMATTED_STRING",
        "responseValueRenderOption": "FORMATTED_VALUE",
        "valueInputOption": "USER_ENTERED",
        "resource": {
          "majorDimension": "ROWS",
          "range": sheetName + "!A2:D",
          "values": [['data1', 'data2', 'data3', 'data4']]
        }
      }).then(function(response) {
       console.log(response);
      
      }, function(err) { console.error("Execute error", err); });

1 Comment

for single cell used '"range": sheetName+"!B1",'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.