-1

I am trying to send an automated email to a bunch of people in my office using Google Apps Script from a Google Sheet. We built it and it worked fine but now we have to add another recipient for each email as a CC. This isn't a huge problem either but I cant figure out how to ignore the null values (not all have a second person to CC). Here is my script, I might just be nesting the if statement incorrectly?

  for (var i = 0; i <= numRows - 1; i++) {
var priorityValues = priority[i][0];
var autostatValues = autostat[i][0];
if(priorityValues == 'P0' && autostatValues != 'Launched') {
  // if it's P0, do something with the data.
  var emailFinal = email[i][0] + '@company.com';
  var ttFinal = tt[i][0] + '@company.com';
  var bugFinal = bugNumber[i][0];
  var sumFinal = sumtotal[i][0];
  var subject = 'Please update your info';
  var msg = 

  '<html><body>message is here
  </body></html>'
    ;


   if(tt[i][0] !== null) {  
    MailApp.sendEmail(emailFinal, subject, "", {htmlBody: msg, noReply:true, cc: ttFinal})}
  else {
    MailApp.sendEmail(emailFinal, subject, "", {htmlBody: msg, noReply:true})}

        }

}

  • Thanks, I didn't realize I missed that. I did forget to switch around the order too, just edited the original. The issue I am getting is that it still gives me there error for '@company.com' not being an email address, even though it should be ignored if tt is null. – Ben Vickery May 06 '16 at 19:10

1 Answers1

0

Missing (unassigned) values are not null in JavaScript, they are undefined. For an undefined value, tt[i][0] !== null returns true. See What is the difference between null and undefined in JavaScript?

Solution: instead of if (tt[i][0] !== null), use if (!tt[i][0]).

The code would also work with if (tt[i][0] != null) or if (tt[i][0] !== undefined).

But !== is the negation of strict comparison ===, which cares about the difference between undefined and null.

Community
  • 1
  • 1