简体   繁体   中英

How to use Google Sheet's app script to add 90 days to user inputted date and email if new date is today's date

How can I get a date in google sheets to auto generate 90 days after user inputted date. So, I have two columns, in column J, I would input a date when I first visited the patient. Then in Column K the date should automatically populate for 90 days after the first visit. But this second date must auto populate and once we reached this date, the script should automatically send an email. I tried to go the easy way by using the google sheet itself but the problem is that once you delete a row, the formula for the entire column is deleted.

Before asking this question, I tried to look at this other solution: How to add days to a date from Google Sheets? but I am still kind of lost.

Now, I also know there is a function called TODAY() in google sheets itself and I tried that too. But here is the problem, what if someone/a user deletes a row, well then my TODAY() formula will be lost for rest of the rows. This is why I need to do this in App script. Can anyone please help me with this please? I have the following code so far.

在此处输入图像描述

function DateCompare() {
   const ss = SpreadsheetApp.getActiveSheet();
   const dataRange = ss.getDataRange();
   const dataRangeVals = dataRange.getValues();
   const headers = 2;  
   var getNewDate = new Date();
   var getDate = new Date().getDate();
   var getMonth = new Date().getMonth()+1;
   var getYear = new Date().getFullYear();
   var dateGlued = getMonth + "/" + getDate + "/" + getYear;  

   for(n=2;n<dataRangeVals.length;++n){
     var cell = dataRangeVals[n][10]; // x is the index of the column starting from 0
     var formattedDate = Utilities.formatDate(new Date(cell), "GMT", "MM/dd/YYYY");
  if (dateGlued.valueOf() === formattedDate.valueOf()) {
     Logger.log("Date Matched!")
   }
 } 
}

function ifItsBeen90Days() {  
   const ss = SpreadsheetApp.getActiveSheet();
   const dataRange = ss.getDataRange();
   const dataRangeVals = dataRange.getValues();
   const headers = 2;  

 dataValues
   .filter(row => row[10] !== '' && row[11] !== "") //filtered data where row[9] is empty
   .forEach(row => {

    let message =
    row[0] +
    '\n' +
    row[1] +
    '\n' +
    row[2] +
    '\n' +
    row[3] +
    '\n' +
    row[4] +
    '\n' +
    row[5]; 
  let email = row[13];
  let subject = 'Sending emails from a Spreadsheet';
  MailApp.sendEmail(email, subject, message); 
  Logger.log(`${subject}: ${message} sent to ${email}`);
  });
 }

Why use GAS when you can simply use a formula to check the criteria you required. The criteria (based on the image) is:

  1. 90 Days after the date which was entered in column J

So that formula equates to the following, just make sure to format the column as Date.

=IF(ISDATE(J2); J2+90; "")

So now we've got the date set up.

Then in order to automatically send E-Mails on that calculated date, you would need to run a trigger on a daily basis. That trigger then runs a particular function, which would then check if any rows contain today's date. If yes, then send out the email, if no, then do not. It is best-practice to log the activity to the sheet, so you have a visible confirmation that it has been sent.

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