Home > Net >  I want to make a code that is sent by e-mail on a fixed date
I want to make a code that is sent by e-mail on a fixed date

Time:07-07

The code is as follows:

function sendmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("test");

  var today = new Date();
  today.setHours(0,0,0,0); 
  Logger.log(today);
  
  var dday = sheet.getRange("j15").getValue();
  Logger.log(dday);
 

  if(dday == today) sendEmail(dday)

  function sendEmail(value){
  var valuechange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test").getRange("j15").getValue();
  var recipient = "[email protected]";
  var subject = "test";
  var body = "test";
  MailApp.sendEmail(recipient, subject, body);
  };
}

The output of the above Logger function is the same (the function setHours initialized because the new date command reflects the current time). I didn't fill in the time on my sheet j15. The format of the values in j15 is 2022-07-07)

But maybe the variables dday and today don't match. I guess the format doesn't match? I ask for your advice.

CodePudding user response:

Try this:

function sendmail() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("test");
  const dt = new Date();
  dt.setHours(0, 0, 0, 0);
  let dday = new Date(sh.getRange("J15").getValue());
  dday.setHours(0, 0, 0, 0)
  if (dday.valueOf() == dt.valueOf()) {
    var recipient = "[email protected]";
    var subject = "test";
    var body = Utilities.formatDate(dday,Session.getScriptTimeZone(),"MM/dd/yyyy");
    MailApp.sendEmail(recipient, subject, body);
  }
}
  • Related