简体   繁体   English

在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, 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. 您需要在可以调用的函数中包含前四行代码,该函数将返回Test1emailAddress的值,或者将其包含在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.

相关问题 Google Apps 脚本 - 满足条件时发送非重复 email - Google Apps Script - Send non repetitive email when condition met Google Apps 脚本表:如何创建仅在满足条件时才显示的 Ui 提示 - Google Apps Script Sheet: how to create a Ui Prompt that only shows if the condition is met 如果条件满足,则 Google Apps 脚本复制范围 - Google Apps Script Copy Range If Condition Met Google Apps脚本[电子邮件发送] - Google Apps Script [Email Send] 如何在一个 email 中发送数据数组而不在 google apps 脚本中循环? - How to send a data array in one email without looping in google apps script? 当谷歌应用程序脚本满足条件时,循环会中断数组 - Loop breaks over an array when a condition is met with google apps script Google Apps脚本-如何发送包含数组内容的电子邮件? - Google Apps Script - how to send an email with the contents of an array? Google Apps脚本中用于从Google表格发送电子邮件的if条件不起作用 - The if condition in Google Apps Script to send an email from Google sheets doesn't work 如何循环将月份添加到每个日期直到满足条件? 谷歌应用程序脚本 - How do I loop adding the month to each date until the condition is met? google apps script Google Apps 脚本:发送 docx email 附件 - Google Apps Script: Send docx email attachment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM