【发布时间】:2019-01-07 10:32:41
【问题描述】:
每次有人填写表格时,我都会尝试让我的谷歌表格通过电子邮件向我发送表格的内容。我正在使用下面的说明,并且完全按照它们进行操作。但是,由于某种原因,当我填写表格时,我收到一条错误消息“TypeError: Cannot call method "getRange" of null. (line 7, file "Code")"
我查看了它正上方的代码行,其中定义了“s”,我认为问题在于 getActiveSpreadsheet 由于某种原因无法正常工作。我认为也许脚本找不到活动表。根据我从谷歌收到的错误电子邮件,以下是导致问题的代码 sn-p。
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
我正在使用的整段代码;
function sendFormByEmail(e)
{
// Remember to replace this email address with your own email address
var email = "support@itjones.com";
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var message = "";
var subject = "Jones IT - 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[1]].toString() + " - starts " + e.namedValues[headers[2]].toString();
// Send the email
MailApp.sendEmail(email, subject, message);
}
【问题讨论】:
标签: google-apps-script google-sheets google-forms