Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a SpreadSheet with the google API

I'm trying to update a given spreadsheet cell, using node.js googleapis v4 and authorizing with a JWT client.

Reading works fine, but I cannot understand how to write:

    new Promise((resolve, reject) => {

            sheets.spreadsheets.values.update({
                auth: this._auth,
                spreadsheetId: this._metaData.spreadSheetId,
                range: range,
                valueInputOption: 'USER_ENTERED'
            },(err, resp) => {

                if (err) {
                    console.log('Data Error :', err)
                    reject(err);
                }

                resolve(resp);

            });

        });

How do I specify the data, and how do I pass it to the call?

I understand I should use a ValueRange object, but how?

like image 994
Mascarpone Avatar asked Aug 24 '16 14:08

Mascarpone


People also ask

How do I update data in Google spreadsheet?

Visit Google Sheets and open the spreadsheet where you want to locate and update your data. Click Edit > Find and Replace from the menu. When the Find and Replace window displays, enter the data you want to locate in the Find box. Then, in the Replace With box, enter what you want to update it with.

Can Google Sheets pull data from an API?

Once you've set up your API to Google Sheets connection, click Save And Run to get data to your spreadsheet.


1 Answers

After better reviewing the (poor) documentation, I inferred that you had to pass a request object in the call:

return new Promise((resolve, reject) => {
  sheets.spreadsheets.values.update(
    {
      auth: this._auth,
      spreadsheetId: this._metaData.spreadSheetId,
      range: range,
      valueInputOption: "USER_ENTERED",
      resource: { range: "Sheet1!A1", majorDimension: "ROWS", values: [["b"]] },
    },
    (err, resp) => {
      if (err) {
        console.log("Data Error :", err);
        reject(err);
      }
      resolve(resp);
    }
  );
});
like image 73
Mascarpone Avatar answered Sep 19 '22 23:09

Mascarpone