如果单元格日期等于今天如何发送电子邮件

时间:2019-05-28 20:04:52

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

我正在尝试创建一个与电子表格一起使用的Google Apps脚本,以便在满足某些条件时发送电子邮件。专门用于在C列等于今天且A列等于false的情况下发送电子邮件。

链接到电子表格: https://docs.google.com/spreadsheets/d/1kULWOMtZaay6PcgF5XTwVoJnKPo7JSA_AK50J8RNYzk/edit?usp=sharing

我能够将其设置为电子表格可以处理大部分工作。 D列检查日期,然后检查A列,然后当D列读取TRUE时脚本将发送。我想知道是否可以让Google Apps脚本检查今天的日期,而不是电子表格。

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 50;   // Number of rows to process
  var numOfColumns = sheet.getLastColumn();

  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, numRows, numOfColumns);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues(); 

  var sendTrigger = "";

  var i = 0;
  for (i=0;i<data.length;i++) {
    var row = data[i];
    var emailAddress = row[4];  // fifth column
    var message = row[5];       // sixth column

    sendTrigger = row[3];

    if (sendTrigger == 1) {
      var subject = ("This is a test of the send email function");
      MailApp.sendEmail(emailAddress, subject, message);
    };
  };
};

我希望脚本检查A列和C列,并在A列等于FALSE且C列等于TODAY的情况下发送电子邮件

1 个答案:

答案 0 :(得分:2)

尝试一下:

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 50;   // Number of rows to process
  var numOfColumns = sheet.getLastColumn();
  var dataRange = sheet.getRange(startRow, 1, numRows, numOfColumns);
  var data = dataRange.getValues(); 
  var sendTrigger = "";
  var dt=new Date();
  var dv=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
  for (var i=0;i<data.length;i++) {
    var row = data[i];
    var emailAddress = row[4];  // fifth column
    var message = row[5];       // sixth column
    var d=new Date(row[2]);
    if (!row[0] && new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf()==dv) {
      var subject = ("This is a test of the send email function");
      MailApp.sendEmail(emailAddress, subject, message);
    }
  }
}