Search code examples
google-apps-scriptgoogle-sheetsdate-comparison

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


I'm trying to write a script where the dates in a column are compared against todays date, and if the dates in the column match, an email is sent.

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

This is as far as I've gotten. The Values in F6 and F1 are identical, I've typed them out, retyped them, copied and pasted, etc. But for some reason, my if statement just won't run. It behaves as if the two values are different, and I can't work out why.

If I change var rotDate and var today to matching strings, eg "123" then it seems to work as expected.

enter image description here

This is a screenshot of my test data sheet. There are other columns there with other data which were meant to be used for more testing, but I didn't get that far.

Does anyone know what I might be doing wrong?


Solution

  • After trying a variety of approaches, I cracked it using a code snippet from Jon Lin's answer here: Compare two dates Google apps script

    After realizing that the fault was with trying to compare two dates (either a date in an adjacent cell, or a procedurally generated date whenever the function is run, I knew I had to do some better formatting with the data I was intending to compare. This is my repaired code that now works as expected:

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