[英]Comparing Dates in Google Sheets
I am new to Java and to Google Script Editor. 我是Java和Google脚本编辑器的新手。 I have a custom CRM spreadsheet in google sheets, and would like to set up reminder emails based on regularly scheduled follow-up dates. 我在Google表格中有一个自定义的CRM电子表格,并希望根据定期安排的后续日期设置提醒电子邮件。 I'm having trouble with the code. 我在使用代码时遇到麻烦。 I think the trouble may be due to the fact that I'm trying to compare a date to a string, but I can't figure out how to get it to work. 我认为麻烦可能是由于我试图将日期与字符串进行比较,但我无法弄清楚如何使其有效。
The goal is to send off an email when the date for follow-up matches today's date. 目的是在后续日期与今天的日期匹配时发送电子邮件。 The date for follow-up is calculated based on a formula. 随访日期是根据一个公式计算的。
Even when the log reads: 即使日志显示为:
[16-07-28 13:38:06:549 PDT] Date is Thu Jul 28 2016 00:00:00 GMT-0700 (PDT) [PDT 16-16-28 13:38:06:549]日期为2016年7月28日星期四00:00:00 GMT-0700(PDT)
[16-07-28 13:38:06:549 PDT] Today is Thu Jul 28 2016 00:00:00 GMT-0700 (PDT) [PDT 16-16-28 13:38:06:549]今天是2016年7月28日,星期四00:00:00 GMT-0700(PDT)
My If statement if (date == todayFull) doesn't work. 我的If语句if(date == todayFull)不起作用。 Here's the code: 这是代码:
function sendEmails() {
var ss = SpreadsheetApp.openById("number");
var sheet = ss.getSheetByName("Script");
var startRow = 2; // First row of data to process
var lastRow = sheet.getLastRow();
var lastCol = sheet.getLastColumn();
// Fetch the range of cells
var dataRange = sheet.getRange(2, 1, lastRow, lastCol);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var date = row[32];
var todayFull = new Date();
todayFull.setHours(0);
todayFull.setMinutes(0);
todayFull.setSeconds(0);
Logger.log("Date is "+date);
Logger.log("Today is "+todayFull);
if (date == todayFull) {
Logger.log("This is a test. The Date is Today");
// var emailAddress = row[28]; // Email column
// var groupName = row[3]; // Group Name column
// var subject = "Follow up with this group right now!";
// MailApp.sendEmail(emailAddress, subject, groupName);
};
};
}
Thanks for the help. 谢谢您的帮助。 The first answer ended up working most of the way. 第一个答案在大多数情况下都有效。 Using .getDate() helped, but I also had to add arguments for month and year. 使用.getDate()有帮助,但我还必须为月份和年份添加参数。 Here's the code I ended up with: 这是我最终得到的代码:
function sendEmails() {
var ss = SpreadsheetApp.openById("");
var sheet = ss.getSheetByName("");
var startRow = 4; // First row of data to process
var lastRow = sheet.getLastRow(); //Get the last row of data to be processed
var lastCol = sheet.getLastColumn();
var dataRange = sheet.getRange(2, 1, lastRow-3, lastCol);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var date2 = new Date(row[24]); // creates a new Date (date in string) object
var todayFull2 = new Date(); // creates a new Date (now) object
if (date2.getDate() == todayFull2.getDate() && date2.getMonth() == todayFull2.getMonth() && date2.getYear() == todayFull2.getYear()) {
etc 等等
You're comparing two different data types: 您正在比较两种不同的数据类型:
var date = row[32]; // reads in a String object
var todayFull = new Date(); // creates a new Date (now) object
...
if (date == todayFull) { // compares Date with String
...
}
You might be better off creating the Date object when you read the value from your Sheet, and then comparing the actual dates in milliseconds (at time 00:00:00:00 of given date) of those Date objects, as you appear to be intending to do: 当您从工作表中读取值时,最好创建Date对象,然后比较这些Date对象的实际日期(以毫秒为单位)(给定日期的时间00:00:00:00),就像您看到的那样。打算做:
var date = new Date(row[32]); // creates a new Date (date in string) object
var todayFull = new Date(); // creates a new Date (now) object
todayFull.setHours(0,0,0,0) // sets time to midnight of current day
...
// compare Date with Date
if (date.getTime() == todayFull.getTime()) {
...
}
See MDN's excellent documentation on Javascript's Date . 请参阅MDN 关于Javascript日期的出色文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.