0

I am trying to pass the array named 'sheetsArray' out of the code below so that I may utilize it for some tasks. I cannot for the life of me figure out how to do this, despite trying many things and googling for hours. I'm sure it's easy but I'm not even sure what I should be searching that I'm not.

var sheetsArrayOut = sheets.spreadsheets.get({
  auth: googleauth,
  spreadsheetId: outputDOCID,
}, function(err,response) {
  if (err) {
    console.log('ERROR:' + err);
    return
  }
  var sheets = response.sheets;

  if (sheets.length == 0) {
    console.log('No data found.');
  } else {
      var sheetsArray = [];
      for (i = 0; i < sheets.length; i++) {
        sheetsArray.push(sheets[i].properties.title);
      }
  }
  console.log(sheetsArray[4]);  // this returns the sheet name
  return sheetsArray;
});

console.log(sheetsArrayOut[4]); // this returns undefined

3 Answers3

1

The first = in var = sheetsArrayOut = sheets.spreadsheets.get(...) is invalid syntax.

Use var sheetsArrayOut = sheets.spreadsheets.get(...) instead.

Jan Mund
  • 151
  • 6
  • I'm not sure how that happened but that was not the source of my issue. – Chris Trevorrow Dec 22 '17 at 23:08
  • The solution @Acika00Mk proposed doesn't work because the `spreadsheet.get(..)` result function is called via promise. Directly logging to console after the call prints the (not yet) assigned var `sheetsArrayOut' as still undefined. – Jan Mund Dec 22 '17 at 23:16
  • to check if this is the issue, instead of console.log(..) give js some time and use setTimeout(function() { console.log(..); }, 1000); – Jan Mund Dec 22 '17 at 23:19
1

It returns undefined because I suspect the function is asynchronous. In this instance you can probably use a callback to good effect:

function getData(function (sheetsArray) {
  // do things with sheetsArray
});

function getData(callback) {
  sheets.spreadsheets.get({
    auth: googleauth,
    spreadsheetId: outputDOCID,
  }, function(err,response) {
    // do a bunch of things
    callback(sheetsArray);
  });
}
Andy
  • 61,948
  • 13
  • 68
  • 95
  • Thank you so much for your help! This worked perfectly. I'm fairly new to using javascript for more than event handlers on web pages, and I was not yet familiar with the concept of asynchronous code. I really appreciate you taking the time to give me some direction. – Chris Trevorrow Dec 23 '17 at 02:11
  • https://stackoverflow.com/q/14220321/1377002 – Andy Dec 23 '17 at 11:26
0

Why don't you just reassign the value at the end of the request

var sheetsArrayOut;
sheets.spreadsheets.get({
    auth: googleauth,
    spreadsheetId: outputDOCID,
    }, function(err,response) {
        //your functional code 
        sheetsArrayOut = sheetsArray;
});
Acika00mk
  • 71
  • 1
  • 4
  • I tried this but it is still outputting undefined. – Chris Trevorrow Dec 22 '17 at 23:07
  • Yes because the get event is not finished. One perspective is to call a function at the end of the request and change your data. The second one is to use async/await modules in node – Acika00mk Dec 22 '17 at 23:13