Search code examples
google-apps-scriptgoogle-sheetsgoogle-analyticsgoogle-analytics-4google-ad-manager

Google Apps Script not sending email


We are trying to get e-mail notifications in cases there would be some dramatic changes in our revenue data. Could anyone please indicate possible errors why it wouldn't send e-mail?

function sendEmail() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet1"); 
  const data = sh.getRange("B2:L80" + sh.getLastRow()).getValues();
  data.forEach(r=>{
     let overdueValue = r[9];  
     if (overdueValue === "TRUE"){
         let name = r[10];
         let message = "Reach out to " + name;
         let subject = "Reach out to this person.";
         //MailApp.sendEmail("[email protected]", subject, message);
         GmailApp.sendEmail("[email protected]", subject, message);  
     }
  });
}

Solution

  • Try changing if (overdueValue === "TRUE") to if (overdueValue === true)

    Updated Code:

    function sendEmail() {
      const ss = SpreadsheetApp.getActive();
      const sh = ss.getSheetByName("Sheet1"); 
      const data = sh.getRange("B2:L80" + sh.getLastRow()).getValues();
      data.forEach(r=>{
         let overdueValue = r[9];  
         if (overdueValue === true){
             let name = r[10];
             let message = "Reach out to " + name;
             let subject = "Reach out to this person.";
             //MailApp.sendEmail("[email protected]", subject, message);
             GmailApp.sendEmail("[email protected]", subject, message);  
         }
      });
    }
    

    Notes:

    • overdueValue is a boolean so trying to check if the value is a string will always end up being false.
    • You could also convert overdueValue to a string which could then be checked as you previously did. You'd just need to change the capitalization of TRUE: if (overdueValue.toString() === "true")