Home > Software design >  Sending an email notification if an event happens next week, in Google Apps Script
Sending an email notification if an event happens next week, in Google Apps Script

Time:10-13

The task is pretty simple, I have a Google Sheet that contains a column with calculated dates. I want to be alerted by email, every time there is a date in that column that is less than a week away from today. My current code is:

function DueDateAlert() {
  
  var due_date_range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Main").getRange("J2:J56"); 
  var due_date = due_date_range.getDisplayValues(); 
  var now = new Date();
  var recipients = "[email protected]";
  var nextWeek = now.setDate(now.getDate()   7);
  var counter = 0;
  for(i in due_date){
    if (i < nextWeek){
      counter = counter 1  
    }}
  if (counter > 0){
      var message = " Task coming up ,Please consult the  Schedule "; 
      var subject = 'Task Alert';
      MailApp.sendEmail(recipients, subject, message);

}

The problem is that the script executes no matter the value in the cell. For some reason I get alerted even if the cells are empty. Any ideas? Much appreciated.

CodePudding user response:

The line: if (i < nextWeek)

i is a number. (From zero to 55, I suppose).

nextWeek is a date.

You're comparing number with date. It doesn't make sense.

Probably you need: if (new Date(dueDate[i]) < nextWeek).

  • Related