在Google Apps脚本中,如果满足条件,如何仅发送一封电子邮件 [英] In Google Apps Script, how to send an email only one time if condition met
问题描述
我正在尝试在某个单元格具有所需值时自动发送电子邮件.我想出了如何连续执行此操作,即将触发器设置为每天在每天的特定时间触发,以检查条件并在满足条件时发送电子邮件.但是我希望触发器在发送一次电子邮件后停止工作.我以为我可以这样:
I'm trying to make automatic email send when a certain cell has required value. I figured out how to do this continually, i.e. a trigger is set to fire each day at a certain time of day to check the condition and send email if it's met. But I'd like the trigger to stop working after email was sent once. I thought I could do it this way:
我有一个功能,可以在满足条件的情况下发送电子邮件.如果不满足条件,则会创建一个触发器,该触发器将再次运行该函数一次.依此类推,触发器将继续创建,直到满足条件为止.
I have a function which sends an email if condition is met. If condition is not met, a trigger is created, which will run the function again, once. And so on, trigger will continue to be created until once a condition is met.
这是我写的代码:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssName = ss.getName();
var emailAddress = ss.getRange("Tab1!B4").getValue();
var Test1 = ss.getRange("Tab1!B6").getValue();
function SendEmailOnce(){
if (Test1 <= 10) {
MailApp.sendEmail({
to: emailAddress,
subject: "Sample subject: " + ssName,
htmlBody: "Sample message<br/> Regards, robot",
});
}
else {
function createTriggerCheckAgain() {
ScriptApp.newTrigger('SendEmailOnce')
.timeBased()
.after(60 * 1000)
.create();
}
}
}
由于某种原因,它不起作用.当我在Test1值为15时运行该函数,然后将该值更改为5时,不会发送电子邮件.
For some reason it doesn't work. When I run the function while Test1 value being 15, and then change the value to 5, no email is sent.
如果它们的各个部分分开,则它们可以工作:1.运行该函数时,如果该值为5,则发送电子邮件.2.在运行值为5时创建该触发器的功能后的一分钟后,将发送电子邮件.
The parts of it work if they are separate: 1. The email is sent if the value is 5 when I run the function. 2. The email is sent after a minute after I run the function of creating that trigger while value is 5.
也许有更简单的方法可以做到这一点?我是JavaScript的新手.预先感谢您的任何建议.
Maybe there is a simpler way to do this? I'm a newbie in JavaScript. Thanks in advance for any suggestions.
推荐答案
当触发器运行60秒后,不再定义 Test1
,因此您无法检查 undefined
是< = 10
.这样做的原因是因为仅执行 SendEmailOnce
中的代码,而不执行其外部的代码. emailAddress
变量也是如此.
When your trigger runs after 60 seconds, Test1
is no longer defined, so you cannot check if undefined
is <= 10
. The reason for this is because only the code in SendEmailOnce
is executed, not the code outside of it. The same also goes for the emailAddress
variable.
您需要在您可以调用的函数中包含前四行代码,该函数会返回 Test1
和 emailAddress
的值,或将其包含在您的 SendEmailOnce
函数.
You either need to include your first four lines of code in a function that you can call, which returns the value of Test1
and emailAddress
or include it in your SendEmailOnce
function.
修改后的代码:
function SendEmailOnce(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssName = ss.getName();
var emailAddress = ss.getRange("Tab1!B4").getValue();
var Test1 = ss.getRange("Tab1!B6").getValue();
if (Test1 <= 10) {
MailApp.sendEmail({
to: emailAddress,
subject: "Sample subject: " + ssName,
htmlBody: "Sample message<br/> Regards, robot",
});
}
else {
function createTriggerCheckAgain() {
ScriptApp.newTrigger('SendEmailOnce')
.timeBased()
.after(60 * 1000)
.create();
}
}
}
这篇关于在Google Apps脚本中,如果满足条件,如何仅发送一封电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!