dimanche 7 mai 2017

Google Apps Script error when accessing nested array by index

Google Apps Script is raising an error when I try to access the nested array with double indexing, it says: TypeError: Cannot read property "3" from undefined. (line 27, file "Code")

Here is the code:

    var ss = SpreadsheetApp.openById("1R-A0HcURMH_Rl7Sw9CDH8M6zMzOoqTtOVLh9r8W29-E");
var sheetMAT = ss.getSheetByName("Sheet3");
var data = sheetMAT.getRange(3, 2, sheetMAT.getLastRow() - 1, 4).getValues();
var temporaryData = [];
var dataReadyLine = [];

function getReadyLine() {
  var rawData = sheetMAT.getRange(3, 2, sheetMAT.getLastRow() - 1, 4).getValues();
  Logger.log(rawData[0][3]);
  for (var i=0; i<=rawData.length; i++) {
    if (rawData[i][3] === "A Ready Line") {
      temporaryData.push(data[i][1], data[i][0]);
      dataReadyLine.push(temporaryData);
      temporaryData = [];
    }
  }
  return dataReadyLine;
};

The line 'Logger.log(rawData[0][3]);' successfully prints the value of the nested array item but when it comes to IF conditional it gives the error of undefined. Why is it giving this error? How can i make the FOR loop work?

Here is the print screen with the error when I try to run the code: Print Screen

Aucun commentaire:

Enregistrer un commentaire