TypeError: Cannot read property of undefined google sheets app scripts

I have code where I’m trying to index and pull data from a previous sheet. This code used to work, but now i’m getting a typeError when running the code.

function updateLocations(market) {   
   

  var activeSheet = SpreadsheetApp.getActiveSpreadsheet();
  var lastPeriod = activeSheet.getSheetByName("Update Info").getRange("C7").getValue();
  var sourceSheet = SpreadsheetApp.openByUrl(lastPeriod).getSheetByName("Assembly Redwood");
  var targetSheet = activeSheet.getSheetByName("Assembly Redwood");


  var targetArr = targetSheet.getRange(4,1,targetSheet.getLastRow(),10).getValues();
  var sourceArr = sourceSheet.getRange(4,1,sourceSheet.getLastRow(),10).getValues();

  var POlistTarget = targetArr.map(function(r){return [r[0],r[1]]});
  var POlistSource = sourceArr.map(function(r){return [r[0],r[1]]});

  var skuList = POlistTarget.map(function(r){return [r[1]]});



  var arrSource = [];
  for(var i = 0; i < POlistSource.length; i++){
    var POSKU = POlistSource[i][0]+POlistSource[i][1];
    arrSource.push(POSKU);
  }

  var arrTarget = [];
  for(var i = 0; i < POlistTarget.length; i++){
    var POSKU = POlistTarget[i][0]+POlistTarget[i][1];
    arrTarget.push(POSKU);
  }



  var units = [];
  for(var i = 0; i < arrTarget.length; i++){
    var row = arrSource.indexOf(arrTarget[i]);
    var unit = sourceArr[row][8]; //***************type error flags this line
    units.push([unit]);
  }
Logger.log(units);


}

All of the variables seem to be logging correctly, but I’m still getting the following error:

TypeError: Cannot read property ‘8’ of undefined (line 333, file
"Code")

98 thoughts on “TypeError: Cannot read property of undefined google sheets app scripts”

Leave a Comment