日期搜索自动报告 - Google文档

时间:2015-03-10 18:53:30

标签: google-apps-script google-sheets

好的,我在询问这个问题之前已经经过了这个网站,但由于某种原因,我无法获得我自动实现的代码。我只需要运行脚本(我已经设置了一个触发器)来检查表5第3列(称为Reports)中的电子邮件。如果今天的日期与给定的日期相符,我需要它向我发送包含第1列数据和第2列数据的电子邮件。

有人可以查看代码并给我一些帮助吗?

function sendEmails() {
  var spreadsheet = SpreadsheetApp.openById('Unique ID for spreadsheet');           

  var sheet = spreadsheet.getSheets()[4];

  var startRow = 2;  // First row of data to process
  var numRows = 250;   // Number of rows to process
  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, numRows, 4)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();   

  var emailAddress = 'email@email.com';
  var subject = "Report Today";

  for (i in data) {
    var row = data[i];
    if( row[3] == true) {
      var message = row[0]+row[1];
      try {
          MailApp.sendEmail(emailAddress, subject, message);
      } catch(errorDetails) {
        MailApp.sendEmail("Email@email.com", "sendEmail script error", errorDetails.message);
      }

    }
  }
}

1 个答案:

答案 0 :(得分:0)

通过将时间(小时,分钟,秒)设置为等,确保日期匹配,以便能够比较它们。

function sendEmails() {

     var spreadsheet =     SpreadsheetApp.openById('[Spreadsheet_Id]');           

      var sheet = spreadsheet.getSheets()[4];

      var startRow = 2;  // First row of data to process
      var numRows = 10;   // Number of rows to process
  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, numRows, 4);

  // Fetch values for each row in the Range.
  var data = dataRange.getValues();   

  var emailAddress = 'email@email.com';
  var subject = "Report Today";

  var today = new Date();
  today.setHours(0,0,0,0);

  for (i in data) {
    var row = data[i];
    row[3].setHours(0,0,0,0);
    if( row[3] == today ) { 
        Logger.log("Send email with: " + row);
        var message = row[0]+row[1];
        try {
          MailApp.sendEmail(emailAddress, subject, message);
        } catch(errorDetails) {
          MailApp.sendEmail(emailAddress, "sendEmail script error", errorDetails.message);
        }
      }
  }
}