0

Below is a code that sends emails with alerts created from data in a spreadsheet.

The line if(ce2 != 'no value'){ Has a Syntax error as do the following two if statements: if(al2 != 'no value'){ and if(cc2 != 'no value'){; however, the first if statement, if(ce2 != 'no value' || al2 != 'no value' || cc2 != 'no value'){, does not. I cannot find the error. I'm sure it's something stupid I haven't clicked onto, though.

I have tried using the correct if statement in place of the ones with the error, but it still has the error. I have played with the positioning of the +'s to see if that has any effect. The function works if the if lines and their corresponding }'s are commented out. I also changed 'empty' to 'no value' in case that was a 'reserved' word or some such.

function emailAlerts() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('EmailAlerts');
  var sub = s.getRange('F5').getValue();
  var nm2 = s.getRange('C11').getValue();
  var em2 = s.getRange('D11').getValue();
  var ce2 = s.getRange('E11').getValue();
  var al2 = s.getRange('F11').getValue();
  var cc2 = s.getRange('G11').getValue();

  if(ce2 != 'no value' || al2 != 'no value' || cc2 != 'no value'){
    MailApp.sendEmail(em2, sub, '',{
      htmlBody: 'Good morning '+nm2+
      ',<br><br><b>Your alerts:</b><br><br>'+
      if(ce2 != 'no value'){
          '<b>CONTRACTS EXPIRING</b><br>'+
          ce2+'<br><br>'+
        }
        if(al2 != 'no value'){
          '<b>AUDIO LINES EXPIRING</b><br>'+
          al2+'<br><br>'+
          }
        if(cc2 != 'no value'){
          '<b>COLD CALLS TO FOLLOW UP</b><br>'+
          cc2+'<br><br>'+
          }
      'Kind regards,<br>XXX'
      });
    }
  }

1 Answer 1

1

The issue is because your nested if statements have code, but no semi-colon terminated expressions. You should create a string with the message you want to send, use ifs to append to it, then send the email. Something like:

var htmlBodyMessage = 'Good morning' + nm2;

if(ce2 != 'no value')
{
    htmlBodyMessage += 'CONTRACTS EXPIRING' + ce2;
}
if(al2 != 'no value')
{
    htmlBodyMessage += 'AUDIO LINES EXPIRING'+ al2;
}
if(cc2 != 'no value')
{
    htmlBodyMessage += 'COLD CALLS TO FOLLOW UP</b><br>'+ cc2;
}

MailApp.sendEmail(em2, sub, '', { htmlBody: htmlBodyMessage });
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I now have the error missing ; before statement for the second line of code var cc3 = s.getRange('G12').getValue(); string htmlBodyMessage = 'Good morning' + nm2 + ',<br><br><b>YOUR ALERTS:</b><br><br>'+ ...; if(ce2 != 'no value')
Right, I got it to work after removing +... and replacing string with var. Thanks for your help!
@c0de No problem, if you could click the tick mark to the left of my question so other people know this solved your problem that'd be great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.