【问题标题】:TypeError: Cannot read property "values" from undefined. (line 4, file "Code")TypeError:无法从未定义中读取属性“值”。 (第 4 行,文件“代码”)
【发布时间】:2017-12-19 20:59:07
【问题描述】:

我正在使用此模板向填写表格的人发​​送电子邮件,问题是每次我尝试运行它时都会收到此错误

“TypeError:无法从未定义中读取属性“值”。(第 4 行,文件“代码”)”

谁能帮我找出问题所在?

谢谢!

function emailOnFormSubmit(e) {

    // Create as many variables as answers (columns in your spreadsheet) you require to send
    var Timestamp = e.values[0];
    var mail = e.values[1];
    var name = e.values[2];
    var Break = e.values[3];


    // The subject of the email
    var subject = "Break time! " + name;

    // emailBody is for those devices that can't render HTML, is plain text
    var emailBody = "Hola " + name + "tu hora de salida es " + Timestamp + "tu hora de regreso es " + Timestamp + time (0,20,0) +
                    "\nFrom " + city + 
                    "\nWith email " + mail + 
                    "\nRegister on " + timestamp +
                    "\n\nThank you for register!"; 

    // html is for those devices that can render HTML
    // nowadays almost all devices can render HTML
    var htmlBody =  "Thank you, your form was submitted on <i>" + timestamp + "</i>" + 
                    "<br/><br/>The details you entered were as follows: " +
                    "<br/>Your Name: <font color=\"red\"><strong>" + name + "</strong></font>" +
                    "<br/>From: " + city + 
                    "<br/>With email: " + mail;

    // More info for Advanced Options Parameters 
    // https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(String,String,String,Object)
    var advancedOpts = { name: "No reply", htmlBody: htmlBody };

    // This instruction sends the email
    MailApp.sendEmail(mail, subject, emailBody, advancedOpts);

}

【问题讨论】:

  • 把这个放在第 2 行:console.log(e); 把这个放在第 3 行:console.log(e.values[0]); 这两个的输出是什么?
  • 它给了我这个错误信息“TypeError: Cannot read property "values" from undefined. (line 3, file "Code")"
  • 先检查是否存在。 if (e.values[0]){ var Timestamp = e.values[0]; }
  • 你肯定对 e 有问题,所以你需要先弄清楚原因,但要消除错误,最好的办法是:let { values = [] } = e;让时间戳 = 值 [0] || "";您将返回 undefined 而不是错误。
  • 如果您通过单击“播放”按钮运行脚本,则没有事件(如果脚本在表单提交时触发,则不会提交表单)。因此,事件对象 (e) 将是未定义的。

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


【解决方案1】:

小背景:

你的错误是TypeError: Cannot read property "values" from undefined. (line 4, file "Code"),我认为第4行是var Timestamp = e.values[0];

在这一行中,您尝试在第 0 位获取对象 e 中传递的值,错误是说对象 e 未定义。这通常发生在我们尝试手动运行一个函数而不提供它需要的所有参数时。

在这里,根据您提供的信息,这意味着您希望每当有人填写表格时触发此功能,并且此功能将向该人发送电子邮件。要使此函数正常工作,您必须在调用此函数时将 Object e 提供给它。

为什么会出现这个错误:

当您尝试手动运行它(通过从下拉列表中选择它并单击执行按钮)时,您没有向它提供它需要的参数,即 Object e

建议的解决方案:

根据表单提交创建一个触发器并从那里调用此函数,该触发器将在调用时提供此参数。我的意思是,无论何时提交任何表单,该触发器都会使用该响应调用此函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多