我的 Javascript IF 语句似乎认为来自 google sheet 和 sheet 的两个相同值不匹配

My Javascript IF statement seems to think two identical values from a google sheets sheet do not match

我正在尝试编写一个脚本,将列中的日期与今天的日期进行比较,如果列中的日期匹配,则会发送一封电子邮件。

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rotDate = sheet.getRange("F6").getValue();
  var today = sheet.getRange("F1").getValue();
  //var today = new Date().toLocaleDateString();  // Today's date, without time
  var dumpCell = sheet.getRange("J3");
  var dumpCell2 = sheet.getRange("J4");
  var dumpCell3 = sheet.getRange("J5");
  if(rotDate==today) {
    //dumpCell is there to dump a value in a cell if the IF statement is true
    dumpCell.setValue(rotDate);
    MailApp.sendEmail("this is where my email would go", "subject", "body");
    }
  //these dump the compared vars into cells so they can be checked against one another manually
  dumpCell2.setValue(rotDate)
  dumpCell3.setValue(today)
}

这是我所了解的。 F6 和 F1 中的值是相同的,我已经将它们键入、重新键入、复制和粘贴等。但是由于某种原因,我的 if 语句不会 运行。它表现得好像这两个值不同,我不知道为什么。

如果我将 var rotDatevar today 更改为匹配字符串,例如 "123" 那么它似乎可以按预期工作。

这是我测试数据的截图sheet。那里还有其他列和其他数据,这些数据旨在用于更多测试,但我没有做到那么远。

有谁知道我可能做错了什么?

在尝试了多种方法后,我使用 Jon Lin 在这里的回答中的代码片段破解了它: Compare two dates Google apps script

在意识到错误在于尝试比较两个日期(无论是相邻单元格中的日期,还是函数为 运行 时程序生成的日期)之后,我知道我必须做一些更好的格式化与我打算比较的数据。这是我修复后的代码,现在可以正常工作了:

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rotDate = sheet.getRange("F6").getValues();
  var today = new Date();
  //sample values
  var sYyyy = Utilities.formatDate(new Date(rotDate), "GMT+8","yyyy");
  var sMm = Utilities.formatDate(new Date(rotDate), "GMT+8","MM");
  var sDd = Utilities.formatDate(new Date(rotDate), "GMT+8","dd");
  //Test Values
  var tYyyy = Utilities.formatDate(new Date(today), "GMT+8","yyyy");
  var tMm = Utilities.formatDate(new Date(today), "GMT+8","MM");
  var tDd = Utilities.formatDate(new Date(today), "GMT+8","dd");  
  //var rotDate = sheet.getRange("F6").getValue();
  //var today = sheet.getRange("F1").getValue();
  //var today = new Date().toLocaleDateString();  // Today's date, without time
  var dumpCell = sheet.getRange("J3");
  var dumpCell2 = sheet.getRange("J4");
  var dumpCell3 = sheet.getRange("J5");
  if (sYyyy + sMm + sDd == tYyyy + tMm + tDd) {
  //if(rotDate===today) {
    //dumpCell is there to dump a value in a cell if the IF statement is true
    dumpCell.setValue(rotDate);
    MailApp.sendEmail("tv18766@gmail.com", "subject", "body");
    }
  //these dump the compared vars into cells so they can be checked against one another manually
  dumpCell2.setValue(rotDate)
  dumpCell3.setValue(today)
}