【发布时间】:2017-07-18 01:02:27
【问题描述】:
我是 Javascript 的新手,一直在编写这个脚本,以获取电子表格中的最新条目(从 Google 表单创建),将收集到的用户电子邮件地址与第二张表中的名册匹配,并向家长发送电子邮件。我是一名教师,我的想法是能够创建一个 google 表单,用于编译学生输入的信息,并在他们提交表单/更新表格后通过电子邮件将其发送给他们的父母。
我知道这很混乱......还有一些额外的变量和东西,但是当您“运行”脚本时,脚本可以完美/按预期工作。唯一的事情是,我试图在提交表单时让脚本在触发器上运行,但事实并非如此。我在使用触发器时遗漏了什么吗?
代码如下:
function createEmail() {
// Sets variables for both sheets
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheets()[0];
var sheet2 = ss.getSheets()[1];
// This gathers information from the most recent entry and write it to an array called newReflectionValues
var reflectionLastRow = sheet1.getLastRow();
var reflectionLastColumn = sheet1.getLastColumn();
var reflectionLastCell = sheet1.getRange(reflectionLastRow, reflectionLastColumn).getValue();
var reflectionRange = sheet1.getRange(reflectionLastRow, 1, 1, reflectionLastColumn);
var newReflectionValues = reflectionRange.getValues();
var studentEmail = newReflectionValues[0][3];
Logger.log("NEW REFLECTION VALUES")
Logger.log(newReflectionValues);
Logger.log("Email will send to student email:")
Logger.log(studentEmail)
// Makes an array of the parent email addresses
var rosterLastRow = sheet2.getLastRow();
var rosterLastColumn = sheet2.getLastColumn();
var rosterEmails = sheet2.getSheetValues(2, 1, rosterLastRow, rosterLastColumn);
Logger.log("PARENT EMAILS")
Logger.log(rosterEmails);
// Cross check emails - if a match, write emails to variable
var parentEntriesLength = rosterLastRow;
for (i = 0; i < parentEntriesLength; i++) {
var currentRange = rosterEmails[i];
if (currentRange[2] == studentEmail) {
var toParents = String(currentRange[3]) + ", " + String(currentRange[4]);
var studentName = String(currentRange[0]);
var countOfReflections = currentRange[6];
break;
} else {
var toParents = "NO PARENT EMAILS FOUND";
}
}
// FINISH EMAIL BELOW
MailApp.sendEmail({
to: toParents,
bcc: "rdoyle@rafos.org" + ", " + String(studentEmail),
subject: "Behavior Reflection Notification",
htmlBody: "<p>Hello,</p>" +
"<p>Today studentName received a behavior reflection for the following action:</p>" +
"<p>newReflectionValues</p>" +
"<p>They took a short break in class and completed the following reflection:</p>" +
"<p>reflectionInformation</p>" +
"<p>" + String(studentName) + " has recieved " + countOfReflections + " reflections this year." + "</p>" +
"<p>This email has been sent with information that the student completed directly on the reflection form and has been bcc'd to them as well as myself. If you have any questions regarding this behavior or incident, please feel free to ask.</p>"
});
}
【问题讨论】:
-
您是如何构建触发器的?触发器可以应用的操作有一些限制。我不确定这是否适用于您的问题。 developers.google.com/apps-script/guides/triggers
-
我想这就是我所迷失的。我已经看过那个文档,但我不确定我是否应该制作自己的触发器,或者只是能够通过转到编辑>当前项目触发器>然后以这种方式设置它来设置它.
-
在许多情况下,编写代码来设置触发器是没有意义的。如果您需要为用户安装触发器,而他们自己无法完成,那么您可以使用代码创建触发器。如果是自己用,只需要安装一次,然后无限期运行,最好手动安装。
-
好的,现在说得通了。我刚刚看了一个小时长的谷歌脚本介绍,来自谷歌开发者的直播视频。我相信手动设置触发器是我应该做的,但这让我回到了主要问题。如果我直接从脚本编辑页面运行脚本,一切正常,但如果我手动创建触发器......什么都没有。
标签: javascript google-apps-script google-sheets triggers