【发布时间】:2015-03-11 21:28:05
【问题描述】:
项目密钥:MyvPlY2KvwGODjsi4szfo389owhmw9jII
我正在尝试运行一个脚本,该脚本在每次提交表单时通过电子邮件发送我的表单内容。我完全按照下面此链接中的说明进行操作,直到我开始遇到错误并收到建议更改我的脚本以按 id 打开电子表格:
http://www.snipe.net/2013/04/email-contents-google-form/
当我完成表格时,它应该将内容通过电子邮件发送到我的电子邮件。
我现在遇到的问题是通过表单值的函数不起作用。它返回错误“TypeError:无法从未定义中读取属性“namedValues”。(第 15 行,文件“代码”)”关于以下代码:
for(var i in headers)
message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "\n\n";
我对谷歌脚本不太熟悉,所以我也不确定如何解决这个问题。你有什么推荐的吗?我在下面包含了整个脚本。
function sendFormByEmail(e)
{
// Remember to replace this email address with your own email address
var email = "sample@email.com";
var s = SpreadsheetApp.openById("1hOqnK0IVa2WT6-c-MY0jyGGSpIJIV2yzTXdQYX4UQQA").getSheets()[0];
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var message = "";
var subject = "New User Form";
// The variable e holds all the form values in an array.
// Loop through the array and append values to the body.
for(var i in headers)
message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "\n\n";
// Insert variables from the spreadsheet into the subject.
// In this case, I wanted the new hire's name and start date as part of the
// email subject. These are the 3rd and 16th columns in my form.
// This creates an email subject like "New Hire: Jane Doe - starts 4/23/2013"
subject += e.namedValues[headers[2]].toString() + " - starts " + e.namedValues[headers[15]].toString();
// Send the email
MailApp.sendEmail(email, subject, message);
}
【问题讨论】:
-
将
Logger.log('value of e is: ' + e);语句作为sendFormByEmail函数的第一行代码。提交表单,然后在代码编辑器中,使用 VIEW 菜单和 LOGS 菜单项。应该有一些东西打印到日志中。如果您在代码编辑器中运行此代码,则e将未定义。e接收值的唯一方法是使用已安装的 On Submit 触发器,并实际提交表单。
标签: javascript google-apps-script triggers google-forms google-apps