【问题标题】:TypeError: Cannot set property '2' of undefined (line 24, file "Code")TypeError:无法设置未定义的属性“2”(第 24 行,文件“代码”)
【发布时间】:2020-11-16 04:54:08
【问题描述】:

我一直在试图弄清楚这一点,但时间最长,我什么也没有。怎么解决这个错误,请帮忙

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)");
}
}
}

【问题讨论】:

  • 这意味着“数据”对象是未定义的。检查工作表的名称是否正确(“名称”)或您获得的值的范围(例如 var “last”)。您能否在 var sheet 行之后添加 Logger.log(sheet)?

标签: javascript google-apps-script google-sheets undefined typeerror


【解决方案1】:

这意味着i 超过data.length=> data[i] 将是未定义的=>undefined[2]:嗯,未定义没有属性[2]。

TypeError:无法设置未定义的属性“2”

for 循环的“条件”似乎有问题。 comma operator 只返回最后一个值。试试

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

改为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多