简体   繁体   English

为什么我收到 TypeError:在 Google 表格上运行应用程序脚本时无法读取未定义的属性“1”?

[英]Why I'm getting TypeError: Cannot read property '1' of undefined while running an apps script on Google Sheets?

I've been trying to find where the cause of it here, but I just can't.我一直试图在这里找到它的原因,但我不能。

Basically, it should look for the row(s) win Form Responses 1 containing testNo equal to the one and set "Yes" to them... It does that, but it keeps giving me this error in the Logs.基本上,它应该寻找包含 testNo 等于 1 的行赢得表单响应 1,并将它们设置为“是”......它确实这样做了,但它一直在日志中给我这个错误。

I think that because the formRepValues may come with an array containing an empty element, it may be causing this error...?我认为因为 formRepValues 可能带有一个包含空元素的数组,它可能会导致这个错误......?

function sendEmail() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();
  const testSheet = ss.getSheetByName("Todays Tests v2");
  const row = sheet.getActiveCell().getRow();
  const col = sheet.getActiveCell().getColumn();
  const emailSent = testSheet.getRange(row, 13, 1, 1).getValue();

  const formRespSheet = ss.getSheetByName('Form Responses 1');
  const formRespRng = formRespSheet.getRange(2, 13, formRespSheet.getLastRow() - 1, 2);
  const formRespValues = formRespRng.getValues();

  if (ss.getActiveSheet().getSheetName() == testSheet.getSheetName() &&
    row > 5 &&
    col == 14 &&
    emailSent != 'Yes') {
    const email = testSheet.getRange(row, 4).getValue();
    const name = testSheet.getRange(row, 2).getValue();
    const testNo = testSheet.getRange(row, 1).getValue();

    for (var n = 0; formRespValues.length; n++) {
      if (formRespValues[n][1] == testNo) {
        formRespSheet.getRange('M' + (2 + n)).setValue('Yes');
      }
    }
  }
}

Thank you!谢谢!

I believe you forgot to set a condition to stop your for loop: the expression formRespValues.length will cause the for loop to run indefinitely.我相信你忘了设置一个条件来停止你的 for 循环:表达式 formRespValues.length 将导致 for 循环无限期地运行。

Try the following:尝试以下操作:

for (var n = 0; n < formRespValues.length; n++) {
     if (formRespValues[n][1] == testNo) {
       formRespSheet.getRange('M' + (2 + n)).setValue('Yes');
     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到 TypeError: Cannot read property '0' of undefined error in google apps script? - Why am I getting TypeError: Cannot read property '0' of undefined error in google apps script? TypeError:无法读取未定义 onEdit 的属性“源” - Google Apps 脚本 - TypeError: Cannot read property 'source' of undefined onEdit - Google Apps Script Google Apps脚本-TypeError:无法从未定义中读取属性“ 0” - Google Apps Script - TypeError: Cannot read property “0” from undefined 获取TypeError:无法在谷歌应用脚本中读取未定义的属性'getAs' - getting TypeError: Cannot read property 'getAs' of undefined , in google app script 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined Google Sheets JavaScript - TypeError:无法读取未定义的属性“getSheetName” - Google Sheets JavaScript - TypeError: Cannot read property 'getSheetName' of undefined TypeError:无法读取未定义的谷歌表格应用程序脚本的属性 - TypeError: Cannot read property of undefined google sheets app scripts Google Apps 脚本:TypeError:无法读取未定义的属性“值”(第 3 行,文件“代码”) - Google Apps Script: TypeError: Cannot read property 'values' of undefined (line 3, file "Code") 获取 TypeError: Cannot read property 'localeCompare' of undefined for my sortBy function 在我的 React 组件中,我不知道为什么 - Getting TypeError: Cannot read property 'localeCompare' of undefined for my sortBy function in my React component and I'm not sure why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM