Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sheet Api get function returning undefined value in nodejs

function changeData(auth,sheetId) {
  var sheets = google.sheets('v4');
  sheets.spreadsheets.values.update({
    auth: auth,
    spreadsheetId: sheetId,
    range: 'Sheet1!D6', 
    valueInputOption: "USER_ENTERED",
    resource: {
      values: [ ["abc"] ]
    }
  }, (err, response) => {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    } else {
        console.log("Appended");
    }
  });
}

I can get the above function to work and it changes the value just fine but the below function doesnt return value and says 0 rows retrieved. what am i doing wrong?

function read(auth,sheet_id)
    {
      var sheets = google.sheets('v4');
        sheets.spreadsheets.values.get({
        auth: auth,
        spreadsheetId: sheet_id,
        range: 'Sheet1!D6'
      }, function(err, result) {
        if(err) {
          // Handle error
          console.log(err);
        } else {
          var numRows = result.values ? result.values.length : 0;
          console.log('%d rows retrieved.', numRows);
        }
      });
    }
like image 425
Mushfiq Avatar asked Oct 25 '25 04:10

Mushfiq


1 Answers

If you are using googleapis after the version of v26.0.1 (now it's 32.0.0.), in your script, you can retrieve the data by result.data. So how about the following modification?

From :

var numRows = result.values ? result.values.length : 0;

To :

var numRows = result.data.values ? result.data.values.length : 0;
like image 68
Tanaike Avatar answered Oct 26 '25 23:10

Tanaike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!