【发布时间】: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