【问题标题】:Google Apps Scripts Send automated email with trigger (not repetitively)Google Apps 脚本发送带触发器的自动电子邮件(不重复)
【发布时间】:2018-12-17 11:14:01
【问题描述】:

我正在尝试使用脚本和触发器通过 google 表格设置自动电子邮件。

如何定义只有新添加到电子表格才能触发电子邮件?电子表格会不断添加。

function sendloggeremails() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lr = ss.getLastRow()

  for (var i = 72; i <= lr; i++) {
    var currentEmail = ss.getRange(i, 7).getValue();
    var currentClassTitle = ss.getRange(i, 3).getValue();

    MailApp.sendEmail(currentEmail, "complaint: customer number " + currentClassTitle ,  "Please check through the log as you have a new assigned to you");
  }
}

var i = 72 显然是因为这是最后一行,我不想经常手动更改它。添加了触发器,但目前我仍然需要进入代码更改var i

任何人都可以提供帮助吗?

【问题讨论】:

  • 澄清一下,当您说“新增内容”时,您的意思是填满了另一行吗?行是否总是添加到末尾?如果您总是只得到最后一行,则循环是不必要的。如果单元格总是从左到右填充,您可以让代码仅在最右边的单元格填充后执行。

标签: google-apps-script google-sheets triggers


【解决方案1】:

使用循环仅发送一次电子邮件

你可以使用这样的东西。首先,您需要在已发送列中添加一些内容,这样旧行就不会重新发送他们的电子邮件,并且您仍会保留过去电子邮件的记录。我建议放置字符串“SENT”。但是测试只是问这个问题是该列是空的,所以任何事情都会起作用。

显然,我没有看到您的电子表格,所以我不知道将 sentColumn 放在哪里,因此您可以将 var sentColumn = 75 更改为您想要的任何内容,并且它使用的所有其他位置都会相应更改。

function sendloggeremails() {
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sh=ss.getActiveSheet();//You should probably change this to getSheetByName() and then put in the correct name of the sheet
  var sentColumn=75;//If this column is not empty then don't send emails.  If you send any email then also put "SENT" into this column so you wont resend it next time you run the loop.
  sh.getRange(1,sentColumn).setValue("Sent");//I'm assuming a header row and I'm putting a Header Title there you can pick any open column you want
  var rg=sh.getDataRange();//This gets all of the sheets data into one one range
  var vA=rg.getValues();//This gets all of the values in above range in a two dimension object we often refer to as a two dimensional array.
  //If you have header row you can start at i=1
  for(var i=1;i<vA.length; i++) {//This will loop over all of the rows on the sheet.
    if(!vA[i][sentColumn-1]){//Heres the test to see if theres something in sentColumn.  If sentColumn is empty meaning truthy is false then it send the email
      var currentEmail=vA[i][6];//this replaces the getValue from column7 *1
      var currentClassTitle=vA[i][2];//this replaces the getValue from column3 *1
      MailApp.sendEmail(currentEmail, "complaint: customer number " + currentClassTitle ,  "Please check through the log as you have a new assigned to you");
      sh.getRange(i+1,sentColumn).setValue("SENT");//After sending email we put something.  In this case "SENT" into the sentColumn so that next through the loop we won send another email because its truthy will be true.
    }
  }
}
//*1 Array indices are 1 less that column numbers because arrays start counting from zero.

如果您希望我们也可以在发送时删除这些行。您所要做的就是跟踪每次运行循环时删除了多少行,例如以 var n=0 之类的变量开头,然后删除的行号将是 i-n+1。并且在删除后立即将 n 加一。

您可以使用此功能设置触发器。

function setupEmailTrigger() {  
  if(ScriptApp.getProjectTriggers().indexOf('sendloggeremails')==-1){
    ScriptApp.newTrigger('sendloggeremails').timeBased().everyDays(1).atHour(17).create();
  }
}

它检查触发器是否已经存在,如果触发器已经存在,它什么也不做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 2013-08-15
    相关资源
    最近更新 更多