gpt4 book ai didi

javascript - Google 脚本项目触发器未运行?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:59:16 26 4
gpt4 key购买 nike

我是 Javascript 的新手,一直在研究这个脚本以获取电子表格(从 Google 表单创建)中的最新条目,将收集到的用户电子邮件地址与第二张表中的名册相匹配,并向家长发送电子邮件。我是一名教师,我的想法是能够创建一个 google 表单来编译学生输入的信息,并在他们提交表单/更新表格后通过电子邮件将其发送给他们的 parent 。

我知道它很乱……有一些额外的变量和东西,但是当您“运行”脚本时,脚本运行得很好/符合预期。唯一的问题是,我曾尝试在提交表单时让脚本在触发器上运行,但事实并非如此。我是否遗漏了使用触发器的内容?

代码如下:

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>"

});

}

最佳答案

您知道有两种类型的触发器 simpleinstallable但我认为您对它们的实际含义/作用有些困惑。我将在此处尝试解释文档中的要点。

通过简单地用触发器名称命名函数来使用简单触发器。例如,对于 Sheets/Forms,触发器 onFormSubmit(e) 在用户提交表单时被触发。参数 e 包含与提交相关的所有信息,您应该查看它,因为它比您当前获取提交信息的方法可靠得多。 See here: 'e' parameter

简单触发器的功能有限,因为脚本无需授权即可触发触发器。简单触发器无法访问其他文件、发送电子邮件或执行任何需要授权的操作。 See here

已安装的触发器是由用户或脚本手动设置的触发器。安装的触发器有更多的功能,但它们仍然有一些限制。 See here

已安装的触发器可以调用任何命名函数,e 参数的工作方式与简单触发器相同。

从您上面的代码来看,您安装的触发器应该如下所示。 enter image description here

当您点击保存时,您应该被要求授权,如果没有被要求,请点击调试/运行按钮授权脚本。

如果还是不行在view->execution transcript中检查execution transcript,最后一行会提示错误。

关于javascript - Google 脚本项目触发器未运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45155847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com