Google Apps脚本 - 根据单元格中的日期发送电子邮件

时间:2012-06-14 01:16:23

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

我环顾四周,有点点零碎,但不能把拼图放在一起。我正在尝试创建一个脚本,该脚本将在配置为每天运行的触发器上运行。触发器将在编辑器的Resources选项下设置。

基本上我正在寻找捕获一系列单元格的脚本,确定到期日期,该日期将填充在一列中,并将其与当前日期相匹配。如果匹配则发送电子邮件。我已经开始发送来自Google电子表格教程的电子邮件。我在if语句中添加了检查日期,但是我在dataRange的比较中丢失了它。任何人都可以帮助纠正这些问题,或指导我指导研究。

脚本似乎运行但没有任何反应,我认为这是因为“if(currentTime == dataRange)”dataRange无法正确匹配?

以下是代码:

function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;  // First row of data to process
var numRows = 50;   // Number of rows to process
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();   
//Get todays date     
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();      
//Test column  for date due & match to current date
    if ( currentTime == dataRange) {
  for (i in data) {
var row = data[i];
var emailAddress = row[0];  // First column
var message = row[1];       // Second column
var subject = "Task Item Due";
MailApp.sendEmail(emailAddress, subject, message);

}
}
}

我正在更新Srik提供的建议,并在下面的代码中提供他的建议。我试图发布这一段时间,所以我不确定为什么它没有通过同行评审?

 function sendEmail() {

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

var dataRange = sheet.getRange(startRow, 1, numRows, 50);

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

for (i in data) {
var row = data[i];
var date = new Date();
var sheetDate = new Date(row[1]);

if (date.getDate() == sheetDate.getDate() && date.getMonth() == sheetDate.getMonth() && date.getFullYear() == sheetDate.getFullYear());
{
  var emailAddress = row[0];  // First column
  var message = row[2];       // Second column
  var subject = "Sending emails from a Spreadsheet";
  MailApp.sendEmail(emailAddress, subject, message);
 // Browser.msgBox(emailAddress)

}

}

}

代码似乎运行但我在脚本编辑器中遇到以下错误。 “无法发送电子邮件:没有收件人(第23行)”。但它仍然会发送电子邮件。它应该比较日期,只有在日期匹配时才发送电子邮件。它每行发送一封电子邮件。我已经分享了电子表格,以查看代码以及电子表格的设置方式。 Shared Spreadsheet

更新代码W / SERGE帮助记录器和Utilities.formatDate ..谢谢!!

function sendEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;  // First row of data to process
var numRows = sheet.getLastRow()-1;   // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues();
//Logger.log(data)

for (i in data) {
  var row = data[i];
  var date = new Date();
  date.setHours(0);
  date.setMinutes(0);
  date.setSeconds(0);
  //Logger.log(date);
  var sheetDate = new Date(row[2]);
 //Logger.log(sheetDate);
 var Sdate = Utilities.formatDate(date,'GMT+0200','yyyy:MM:dd')
 var SsheetDate = Utilities.formatDate(sheetDate,'GMT+0200', 'yyyy:MM:dd')
     Logger.log(Sdate+' =? '+SsheetDate)
        if (Sdate == SsheetDate){
          var emailAddress = row[0];  // First column
          var message = row[1];       // Second column
          var subject = "Your assigned task is due today." +message;
          MailApp.sendEmail(emailAddress, subject, message);
          //Logger.log('SENT :'+emailAddress+'  '+subject+'  '+message)
            }    
   }
  }

3 个答案:

答案 0 :(得分:6)

这是您的代码的工作版本,我使用Utilities.formatDate来创建日期的字符串,因此您可以选择您比较的内容(只有几天,几小时?分钟?)

我为我的测试评论了邮件调用,在需要时重新启用它

function sendEmail() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = sheet.getLastRow()-1;   // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  Logger.log(data)

  for (i in data) {
    var row = data[i];
    var date = new Date();
    var sheetDate = new Date(row[1]);
    Sdate=Utilities.formatDate(date,'GMT+0200','yyyy:MM:dd')
    SsheetDate=Utilities.formatDate(sheetDate,'GMT+0200', 'yyyy:MM:dd')
    Logger.log(Sdate+' =? '+SsheetDate)
    if (Sdate == SsheetDate){
      var emailAddress = row[0];  // First column
      var message = row[2];       // Second column
      var subject = "Sending emails from a Spreadsheet";
//      MailApp.sendEmail(emailAddress, subject, message);
     Logger.log('SENT :'+emailAddress+'  '+subject+'  '+message)
    }    
  }
}

不要忘记查看日志; - )

答案 1 :(得分:2)

我会这样做....

像这样设置电子表格: https://docs.google.com/spreadsheet/ccc?key=0AkGlO9jJLGO8dDJad3VNTkhJcHR3UXlJSVRNTFJreWc

将代码更改为:

function sendEmails() {
  var spreadsheet = SpreadsheetApp.openById('Type spreadsheet key here from spreadsheet URL');       
  /// e.g.  var spreadsheet = SpreadsheetApp.openById('0AkGlO9jJLGO8dDJad3VNTkhJcHR3UXlJSVRNTFJreWc');     

  var sheet = spreadsheet.getSheets()[0]; // gets the first sheet, i.e. sheet 0

  var range = sheet.getRange("B1"); 
  var dateString = new Date().toString();
  range.setValue(dateString);   // this makes all formulas recalculate

  var startRow = 4;  // First row of data to process
  var numRows = 50;   // 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();   

  for (i in data) {
    var row = data[i];
    if( row[3] == true) {
      var emailAddress = row[0];  // First column
      var message = row[1];       // Second column
      var subject = "Task Item Due";
      try {
          MailApp.sendEmail(emailAddress, subject, message);
      } catch(errorDetails) {
        // MailApp.sendEmail("eddyparkinson@someaddress.com", "sendEmail script error", errorDetails.message);
      }

    }
  }
}

触发器:

由于触发器,您需要使用openById打开电子表格。  为此,请在代码中替换“在电子表格网址中键入电子表格密钥”。  要找到密钥,请打开google docs电子表格,该链接有“key = A0a0df ...”粘贴代码。参见示例。

JavaScript的:  如果您想了解有关使用Java Script的更多信息,我建议http://www.w3schools.com/js/default.asp

答案 2 :(得分:1)

您在这里比较两个不同的对象

    if ( currentTime == dataRange) {

currentTime是一个Date对象,而dataRange是一个Range对象 - 它们永远不会相等,所以没有任何反应。 您可以做的是(DATE_COL是具有您日期的列)

for ( var i in data ){
  var row = data[i] ; 
  var today = new Date(); 
  var date = new Date(row[DATE_COL]) ; 

  if (today.getDate() == date.getDate() && 
      today.getMonth() == date.getMonth() && 
      today.getYear() == date.getYear() {
    /* Your email code here */ 
  }
}