1

This is pretty self explanatory, I don't know why I'm getting the error since process() should be executing, but it's not.

function process () {
  var List = new Array("a","b","c");
}

function print () {
  if (typeof List == undefined) {
    process();
  }
  var doc = SpreadsheetApp.getActive();
  for (var i = 0; i < List.length; i++) {
    var c = i+1
    doc.getRange("A"+c.toString()).setValue(List[i]);
  }
}

print();

ERROR: Line 10, List is not defined

user3334776
  • 113
  • 1
  • 10

2 Answers2

3

You are defining the variable List as a local variable, thus it will only exist in process(), define it as a global variable removing the private word var, here it is:

function process () {
    List = new Array("a","b","c");
}
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Better, make it `return` the value. And you're not defining any variable there, you just assign to it - always *declare* it explicitly even if global. – Bergi Aug 09 '14 at 15:03
0

you can move the List var to outside of the function.

 var List =  [];
function process () {
 List = new Array("a","b","c");
}

function print () {
  if (typeof List == undefined) {
    process();
  }
  var doc = SpreadsheetApp.getActive();
  for (var i = 0; i < List.length; i++) {
    var c = i+1
    doc.getRange("A"+c.toString()).setValue(List[i]);
  }
}

print();
Idan Magled
  • 2,186
  • 1
  • 23
  • 33