Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a single cell of Google Sheets using JavaScript API

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.

like image 768
Tim Avatar asked Aug 02 '16 11:08

Tim


People also ask

How do I automatically replace text in Google Sheets?

On your computer, open a spreadsheet in Google Sheets. Find and replace. Next to "Find," type the word you want to find, If you want to replace the word, enter the new word next to "Replace with."


2 Answers

gapi.client.sheets.spreadsheets.values.update({
    spreadsheetId: 'something',
    range: 'Sheet1!B2',
    valueInputOption: 'USER_ENTERED',
    values: [ ["123"] ]
}).then(function(response) {
    console.log(response);
});
like image 134
Tim Avatar answered Sep 22 '22 11:09

Tim


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);
    }
});
like image 23
Curtis Chong Avatar answered Sep 20 '22 11:09

Curtis Chong