问题描述
我有一个Google表格供学生输入数据,如果特定问题有特定答案,我希望向自己或其他人发送电子邮件。在电子邮件中,我希望将提交表格的学生的姓名包含在电子邮件中。我遇到的问题是电子邮件仅从表格的第2行发送信息。 (我将第1行冻结为标题)。我需要它从Google表单输入的最后一行数据中提取。
Google表单-https://docs.google.com/forms/d/1r1Tsyfl71zfzgzhs3-_qFB57FcSSEX1zS5BkTgz-ESA/prefill
Google表格数据-https://docs.google.com/spreadsheets/d/1P1RD7My91jLSHS9hgEOBPv5oYnfCJQviN6Nzy_cH7Cc/edit?usp=sharing
这是我的脚本代码:
function CheckStudent(e) {
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange("B2");
var emailAddress = emailRange.getValue();
// Send Alert Email.
{
var studentData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1").getRange("C2");
var studentData = studentData.getValue();
var message = studentData + ' is not doing well today!' // Second column
var subject = 'Student Daily Check Alert';
MailApp.sendEmail(emailAddress,subject,message);
}
}
解决方法
如果您已经在表单提交的表单中注册了电子邮件,那么就像创建一个installable trigger一样简单。
您可以使用event object来做到这一点。
如果您不熟悉触发器,那么我建议您阅读the official documentation那里的触发器,您可以看到一些有关如何创建触发器的示例:
var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger("CheckStudent") // You will need to adapt your function
// to recieve the event parameter
.forSpreadsheet(sheet)
.onFormSubmit()
.create();
执行完之前的代码后,每当有人提交表单时,函数就会触发。
编辑
现在我想您可以直接在表单对象中执行触发器,而无需通过表格。
更改代码event object:
var form = FormApp.getActiveForm();
ScriptApp.newTrigger('CheckStudent')
.forForm(form)
.onOpen()
.create();