TypeError:无法读取属性"值"来自undefined。 (第4行,文件"代码")

时间:2017-12-19 20:59:07

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

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

" TypeError:无法读取属性"值"来自undefined。 (第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);

}

1 个答案:

答案 0 :(得分:0)

小背景:

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

在这一行中,您尝试获取在第0个位置的对象e中传递的值,并且错误表示未定义对象e。当我们尝试手动运行函数而不提供所需的所有参数时,通常会发生这种情况。

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

为什么会出现此错误:

当您尝试手动运行它时(通过从下拉列表中选择它并单击执行按钮),那么您不会为它提供所需的参数e

建议的解决方案:

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