Tuesday, December 20, 2022

initializing a 2D array in google apps script

Trying code like:
var resultasarray = [];  
  
  while (TableData.next()) {
    for (var j = 0; j < numberOfColumns; j++) {
       resultasarray[i][j] = TableData.getString(j+1));
  }
  i++;
       
    }
threw errors, TypeError: Cannot read properties of undefined (reading '0')

Using push() instead, worked.

var resultasarray = [];  
  
  while (TableData.next()) {
    var rowvals = [];

    for (var j = 0j < numberOfColumnsj++) {
        rowvals.push(TableData.getString(j+1));
        //Logger.log(TableData.getString(j+1))
    }
    resultasarray.push(rowvals)
    i++;
  }

No comments:

Post a Comment