简体   繁体   中英

Google Sheets Scripts - send an email based on a cell being today's date

I've looked all over, but I can't seem to piece the code together from all the different answers out there.

I have 4 columns that are populated by the following: Date Processed, Email, Name, Removal Date

What I'd like to do is send an email to a static email address, including the email address that needs to be removed when cells in the "Removal Date" column equal today's date. I also need this to run once per day.

I've figured out the daily running with the triggers. I've pasted the code below that I'm trying to run. My thought was to put the data into an array, evaluate the 4th column for the dates. Then when the date is today, it returns the entire row so I can use the email listed from the 2nd column.

function SendEmail() 
{
  var presentDay = new Date();
  var rows = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
  var filteredEmails = rows.filter(function(row)
  {
    var removalDate = row[4];
    if (removalDate === Date)
    {
      return row
    }
  })

  Logger.log(filteredEmails);

  var emailAddress = "staticEmail@gmail.com";
  var message = ' needs to be removed'; 
  var subject = 'Member needs to be removed';


        //MailApp.sendEmail(emailAddress, subject, row[2] + message);
    
  
  
}

Try something like this:

function sendEmails() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Emails';
  const sr = 2; //data start row
  const dt = new Date();
  const tdv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
  const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, sh.getLastColumn()).getValues();
  const fvs = vs.filter(r => {
    let d = new Date(r[4]);
    let dv = new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf()
    return dv == tdv;
  });
  fvs.forEach(r => {});//send emails with this loop
}

If you wish to compare dates numerically then you must use valueOf() or getTime();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM