未更改时(隔夜)执行Google Script onChange触发器

时间:2019-07-17 17:47:30

标签: google-apps-script triggers

我有一个运行sendEmails()函数和可安装onChange触发器的Google脚本。效果很好,但是我看到它也可以在一夜之间执行,而无需对电子表格进行任何更改。

任何人都知道可能会发生什么/如何预防这种情况?

以下是脚本编辑器中正在运行的代码:

/**
 * Sends emails with data from the current spreadsheet.
 */

function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = 1; // Number of rows to process
  // Fetch the range of cells A2:C2
  var dataRange = sheet.getRange(startRow, 1, numRows, 5);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[4]; // Fifth column
    var message = row[0]; // First column
    var subject = row[3]; // Fourth Column
    MailApp.sendEmail(emailAddress, subject, message);
  }
}

在执行/触发日志中,我看到昨晚在凌晨5:07和前一天的凌晨1:16执行了触发器,而电子表格没有任何更改

screenshot of trigger log

2 个答案:

答案 0 :(得分:0)

尝试此操作,看看它是否可以消除隔夜的电子邮件。

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;
  var numRows = 1;
  var dataRange = sheet.getRange(startRow, 1, numRows, 5);
  var data = dataRange.getValues();
  for (var i=0;i<data.length;i++) {
    var row = data[i];
    var emailAddress = row[4]; // Fifth column
    var message = row[0]; // First column
    var subject = row[3]; // Fourth Column
    if(emailAddress && message && subject) {
      MailApp.sendEmail(emailAddress, subject, message);
    }
  }
}

答案 1 :(得分:0)

无法找出潜在的问题,但是创建了代码来阻止脚本为先前发送过电子邮件的任何行发送电子邮件(在这种情况下,向Asana创建任务)。

var TASK_SENT= "TASK_SENT";

/**
 * Sends non-duplicate emails with data from the current spreadsheet.
 */

function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = sheet.getLastRow();
  // Fetch the range of cells UPDATE
  var dataRange = sheet.getRange(startRow, 1, numRows, 48);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var message = row[45] //Next Steps Column
    var staff = row[1] // Your Name Column
    var internal_part = row[7] //Who was the Check-in with? Column
    var external_part = row[13] //What Site was your Check-in With? Column
    var check_dt = row[2] //Date of Contact Column
    var check_dt = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yy")
    var site_disc = row[8] //What site was discussed?
    var emailAddress = row[46]; // Email address column
    var taskSent = row[47]; // Task_Sent column
    if (taskSent != TASK_SENT) { // Prevents sending duplicates
      var subject = staff + " " + 'check-in w/' + " " + internal_part + external_part + " " + 're:' + " " + site_disc + " " + check_dt + " " + 'Next Steps';
      if (emailAddress == "") {
        continue;}
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 48).setValue(TASK_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}