Comparing Dates in Google Sheets Script Editor

后端 未结 1 990
感情败类
感情败类 2021-01-27 07:53

I am very new to this, so please bear with me. I am trying to get my code to look at each cell in a column, then compare that date to the current date, and if they match send an

1条回答
  •  遥遥无期
    2021-01-27 08:32

    Dates can be frustrating to work with, so consider doing the whole thing (loop, if then statement...) with an integer first and then returning to the date part if you're having trouble. That said, try adjusting the top of your code to look like the following:

    var ss =SpreadsheetApp.getActiveSpreadsheet();
    var removalDateVal = ss.getSheetByName("Expirations").getRange("E2").getValue();
    var removalDate = new Date(removalDateVal);
    
    var currentDateVal = ss.getSheetByName("Alerts").getRange("C2").getValue();
    var currentDate = new Date(currentDateVal);
    

    That will give you two date objects. But BEWARE! These dates contain time as well as calendar date so they may not equal each other even when they appear to. Use setHours() to zero out the date as seen below.

    currentDate.setHours(0,0,0);
    removalDate.setHours(0,0,0);
    

    Other notes, it's best practice to set a variable for a spreadsheet and worksheet as shown by Google here. It makes the code much more readable.

    0 讨论(0)
提交回复
热议问题