5

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

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

该脚本似乎运行但没有任何反应,我认为这是因为“如果(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 行)”。但它仍然发送电子邮件。它应该比较日期并仅在日期匹配时发送电子邮件。它为每一行发送一封电子邮件。我已经分享了电子表格以查看代码以及电子表格的设置方式。 共享电子表格

在记录器和 Utilities.formatDate 上更新代码 W/SERGE HELP .. 谢谢!!

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)
            }    
   }
  }
4

3 回答 3

7

这是您的代码的工作版本,我使用 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)
    }    
  }
}

不要忘记查看日志;-)

于 2012-06-16T22:23:15.937 回答
3

我会这样做....

像这样设置电子表格: 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 打开电子表格。为此,请在代码中替换“在此处从电子表格 URL 键入电子表格密钥”。要找到密钥,请打开 google 文档电子表格,链接有“key=A0a0df...”粘贴代码。请参阅示例。

JavaScript:如果你想了解更多关于使用 Java Script 的知识,我推荐http://www.w3schools.com/js/default.asp

于 2012-06-14T02:43:16.470 回答
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 */ 
  }
}
于 2012-06-14T07:06:59.580 回答