Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

javascript - 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")

question from:https://stackoverflow.com/questions/65934295/typeerror-cannot-read-property-of-undefined-google-sheets-app-scripts

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Issue:

Array.prototype.indexOf() returns the first index at which a given element can be found in the array, or -1 if it is not present.

Based on the error you are getting I assume that this returns -1:

var row = arrSource.indexOf(arrTarget[i]) // value of row is -1

and therefore sourceArr[-1] undefined and these is why you can't use:

var unit = sourceArr[row][8]

because you are asking for the 8th element of something undefined sourceArr[-1].

Solution:

Check first if you get a non-negative value from the indexOf method:

  var units = [];
  for(var i = 0; i < arrTarget.length; i++){
    var row = arrSource.indexOf(arrTarget[i]);
    console.log(row) // console row to see its value
    if (row>-1){
      var unit = sourceArr[row][8]; 
      units.push([unit]);
    }
  }

or if (row!=-1) works as well.

Minimal Reproducible Example:

  const sourceArr = [[3,1,4,30,32,12,1,4,5,3],
                     [3,1,4,30,32,12,1,4,5,3],
                     [3,1,4,30,32,12,1,4,5,3],
                     [3,1,4,30,32,12,1,4,5,3]]
  
console.log(sourceArr[0][8]); // this works
console.log(sourceArr[-1][8]); // TypeError: Cannot read property '8' of undefined 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...