Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Google sheets script send one email instead of many

I have a script that checks data from sheet and if number from one column is bigger than 19, then it sends an notification email with the information from row.

I have new data every day so it sends around 1-10 emails per day.

I would like to have just one email with the data from all the rows instead of many emails with the data from separate rows.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is that somehow doable?

Thanks!

function sendEmail() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('sheet'); 
  const data = sh.getRange('B2:D75'+sh.getLastRow()).getValues();
  data.forEach(r=>{
     let overdueValue = r[2];  
     if (overdueValue > 19){ // if value is bigger than 19 then send email
         let name = r[0];
         let message ='https:/' + name + '    ' + 'https://' + name ;
         let subject = 'Subject: ' + name
         MailApp.sendEmail('example@gmail.com', subject, message); 
     }
  });
    
}

>Solution :

In your script, how about the following modification?

Modified script:

function sendEmail() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('sheet');
  const data = sh.getRange('B2:D75' + sh.getLastRow()).getValues();
  const messages = data.reduce((ar, r) => {
    let overdueValue = r[2];
    if (overdueValue > 19) {
      let name = r[0];
      let message = 'https:/' + name + '    ' + 'https://' + name;
      ar.push(message);
    }
    return ar;
  }, []);

  let subject = "Subject"; // Please set the subject of the email.
  MailApp.sendEmail('example@gmail.com', subject, messages.join("\n"));
}
  • In this case, each value of message is splitted by \n and it is included in the text body.

Reference:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading