google-apps-script - Google Sheet 在单元格具有特定值时发送电子邮件
问题描述
我有一个供学生输入数据的 Google 表单,如果特定问题有特定答案,我希望将电子邮件发送给我自己或其他人。在电子邮件中,我希望将提交表格的学生的姓名包含在电子邮件中。我遇到的问题是电子邮件仅从表格的第 2 行发送信息。(我有第 1 行 Frozen 作为标题)。我需要它从谷歌表单输入的最后一行数据中提取。
谷歌表格 - https://docs.google.com/forms/d/1r1Tsyfl71zfzgzhs3-_qFB57FcSSEX1zS5BkTgz-ESA/prefill
谷歌表格数据 - 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);
}
}
解决方案
如果您已经在表单提交的表单中注册了电子邮件,那么就像创建可安装触发器一样简单。
您可以使用事件对象来做到这一点。
如果您不熟悉触发器,那么我建议您阅读其中的官方文档,您可以看到一些如何创建触发器的示例:
var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger("CheckStudent") // You will need to adapt your function
// to recieve the event parameter
.forSpreadsheet(sheet)
.onFormSubmit()
.create();
一旦你执行了前面的代码,你的函数就会在每次有人提交表单时触发。
编辑
现在我想你可以直接在表单对象中执行触发器,而不是通过工作表。
稍微更改事件对象代码:
var form = FormApp.getActiveForm();
ScriptApp.newTrigger('CheckStudent')
.forForm(form)
.onOpen()
.create();
推荐阅读
- php - 项目列表数组上的 PHP/Swift JSON 序列化导致零元素
- r - 从 data.table 或 head() 中选择“前 n”列时,R 从底层向量返回级别
- macos - 从 jenkins 调用 docker 容器(套接字上的权限被拒绝)
- angular5 - Angular5中的自定义工具栏
- python - python2.7 绘制没有自相交的简单多边形
- php - 带有 Migadu 的 PHPMailer 无法正常工作
- dc.js - 在交叉过滤器中数组维度的情况下维度.top的行为
- html - 由于输入字段过多,HTML 表单无法打开客户端?
- javascript - 在js帮助中用'for'捕获ID
- css - 将不同大小的文本对齐到底部