我环顾四周,有一些零碎的东西,但不能把拼图放在一起。我正在尝试创建一个脚本,该脚本将在配置为每天运行的触发器上运行。触发器将在编辑器的资源选项下设置。
基本上,我正在寻找脚本来捕获一系列单元格,确定到期日期,该日期将填充在一列中,将其与当前日期匹配。如果匹配,则发送电子邮件。我从 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)
}
}
}