Home > OS >  Send 2 different emails based on 2 conditions
Send 2 different emails based on 2 conditions

Time:08-09

So I'm trying out Apps Script and want my code to send an email based whether a condition is True or False. I was able to execute and receive an email when the only conditional is True but I'm quite unsure how to add the else statement as seen below as an error sign appears once I save the script

function sendMailEdit(e) {
  const rData = e.source.getActiveSheet().getRange(e.range.rowStart, 1, 1, 15).getValues();
  var paticipateTicket = rData[0][1];
  var ticketType = rData[0][2];
  var ticketNumber = rData[0][14];
  var company = rData[0][3];
  var name = rData[0][4];
  var email = rData[0][5];
  var remarks = rData[0][1];


  if (e.range.columnStart != 17 || e.value != "Yes") return;{
     var html = `<h2> Hello ${name} !</h2><p>Your purchase of a ${paticipateTicket} under ${company} is confirmed with ticket number ${ticketNumber}. <p>See you at the tournament at the details below.</p>
  Your ticket number is ${ticketNumber} with a ticket type of  and you'll be participating under ${company} </p><p><img src = "https://drive.google.com/uc?export=view&id=1nMiIfUgfFPYn4YOzXmEwJlaiNRPyOHsI" width = "200 px" length = "300 px"></p>`;
  var msg = name   "Your payment has been verified. Your ticket number is "   ticketNumber   ".Participating under "   company
  GmailApp.sendEmail(email, "2022 Charity Golf Tournament", msg, { htmlBody: html });
  }

  else (e.range.columnStart != 17 || e.value != "No") return;{
  var html = `<h2> Hello ${name} !</h2><p>Your purchase of a ${paticipateTicket} under ${company} is has been paused and here’s why: </p> <p> ${remarks}</p>
  
  var msg1 = name   "Your payment has been paused. Below is the reason why”
  GmailApp.sendEmail(email, "2022 Charity Golf Tournament", msg, { htmlBody: html });
}  

}

CodePudding user response:

There were some missing characters and in one case the wrong " symbol was used, try replacing the if-else block with this:

if (e.range.columnStart != 17 || e.value != "Yes"){
    var html = `<h2> Hello ${name} !</h2><p>Your purchase of a ${paticipateTicket} under ${company} is confirmed with ticket number ${ticketNumber}. <p>See you at the tournament at the details below.</p>
  Your ticket number is ${ticketNumber} with a ticket type of  and you'll be participating under ${company} </p><p><img src = "https://drive.google.com/uc?export=view&id=1nMiIfUgfFPYn4YOzXmEwJlaiNRPyOHsI" width = "200 px" length = "300 px"></p>`;
    var msg = name   "Your payment has been verified. Your ticket number is "   ticketNumber   ".Participating under "   company
  GmailApp.sendEmail(email, "2022 Charity Golf Tournament", msg, { htmlBody: html });

  } else if (e.range.columnStart != 17 || e.value != "No"){
    var html = `<h2> Hello ${name} !</h2><p>Your purchase of a ${paticipateTicket} under ${company} is has been paused and here’s why: </p> <p> ${remarks}</p>`
    var msg1 = name   "Your payment has been paused. Below is the reason why";
    GmailApp.sendEmail(email, "2022 Charity Golf Tournament", msg1, { htmlBody: html });

}  

Google Apps Script view after updating the code:

enter image description here

CodePudding user response:

Try it this way:

function sendMailEdit(e) {
  const sh = e.range.getSheet();
  const [, paticipateTicket, ticketType, company, name, email, , , , , , , , , ticketNumber] = sh.getRange(e.range.rowStart, 1, 1, 15).getValues()[0];
  if (e.range.columnStart != 17 || e.value != "Yes") {
    var html = `<h2> Hello ${name} !</h2><p>Your purchase of a ${paticipateTicket} under ${company} is confirmed with ticket number ${ticketNumber}. <p>See you at the tournament at the details below.</p>
  Your ticket number is ${ticketNumber} with a ticket type of  and you'll be participating under ${company} </p><p><img src = "https://drive.google.com/uc?export=view&id=1nMiIfUgfFPYn4YOzXmEwJlaiNRPyOHsI" width = "200 px" length = "300 px"></p>`;
    var msg = name   "Your payment has been verified. Your ticket number is "   ticketNumber   ".Participating under "   company
    GmailApp.sendEmail(email, "2022 Charity Golf Tournament", msg, { htmlBody: html });

  } else if (e.range.columnStart != 17 || e.value != "No") {
    var html = `<h2> Hello ${name} !</h2><p>Your purchase of a ${paticipateTicket} under ${company} is has been paused and here’s why: </p> <p> ${remarks}</p>`
    var msg1 = name   "Your payment has been paused. Below is the reason why";
    GmailApp.sendEmail(email, "2022 Charity Golf Tournament", msg1, { htmlBody: html });
  }
}  

remarks is undefined

  • Related