0

I've been trying to figure out this but for the longest time and I've got nothing. How do I fix this error, please help

function myFunction() {
  var form = FormApp.openByUrl('form');
  var allItems = form.getItems();
  var doc = SpreadsheetApp.getActiveSpreadsheet()
  var sheet = doc.getSheetByName("name");
  var last = doc.getLastRow();
  var data = sheet.getRange(1,1,last,8).getValues();

for(i=0,h=1; i<data.length, h<allItems.length;++i,++h){
        
    
    if(data[i][2] == 0 && h==4){ //this where the error messages comes up for the "2"
      
      var newText = allItems[h].asMultipleChoiceItem();
      var title = newText.getTitle();
      var newTitle = newText.setTitle(title+" (Sold out)");
}
}
}
1
  • That means that the "data" object is undefined. Check whether the name of the sheet is correct ("name") or the range of the values you are getting ( e.g. var "last"). Could you please add Logger.log(sheet) after the var sheet line? Commented Jul 26, 2020 at 20:51

1 Answer 1

2

It means i exceeds data.length=> data[i] will be undefined =>undefined[2]: Well,undefined doesn't have property [2].

TypeError: Cannot set property '2' of undefined

The "condition" of the for-loop seems amiss. The comma operator only returns the last value. Try

 i<data.length && h<allItems.length

instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.