[英]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, ie 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.
依此类推,触发器将继续创建,直到满足条件为止。
Here is a code I wrote: 这是我写的代码:
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();
}
}
}
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.
当我在Test1值为15时运行该函数,然后将该值更改为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. 如果它们的各个部分是分开的,则它们可以工作:1.运行该函数时,如果该值为5,则发送电子邮件。 2. The email is sent after a minute after I run the function of creating that trigger while value is 5.
2.在运行值为5时创建触发器的功能后,过一分钟后发送了电子邮件。
Maybe there is a simpler way to do this? 也许有一种更简单的方法可以做到这一点? I'm a newbie in JavaScript.
我是JavaScript的新手。 Thanks in advance for any suggestions.
在此先感谢您的任何建议。
When your trigger runs after 60 seconds, Test1
is no longer defined, so you cannot check if undefined
is <= 10
. 当触发器在60秒后运行时,将不再定义
Test1
,因此您无法检查undefined
是否<= 10
。 The reason for this is because only the code in SendEmailOnce
is executed, not the code outside of it. 其原因是因为仅执行
SendEmailOnce
中的代码,而不执行其外部的代码。 The same also goes for the emailAddress
variable. emailAddress
变量也是如此。
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. 您需要在可以调用的函数中包含前四行代码,该函数将返回
Test1
和emailAddress
的值,或者将其包含在SendEmailOnce
函数中。
Modified Code: 修改后的代码:
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.