I'm trying to create an email notification tool that will be trigger with a UI interfase, now the code runs perfect, includes the right Email Body, the right Email subject, etc. However, I'm missing a part where IF there is nothing to notify that row can be excluded from the batch of emails, I tried to just remove the email addresses from the emails column but that causes the code to crash. I'm trying to evaluate if orders is blank.
Thanks!
function emailNotifications() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TestingSheet");
var lr = ss.getLastRow();
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();
for (var i = 2;i<=lr;i ){
var emails = ss.getRange(i, 2).getValue();
var regionName = ss.getRange(i, 1).getValue();
var orders = ss.getRange(i, 3).getValue();
var messageBody = templateText.replace("{region}",regionName).replace("
{orders}",orders);
var subjectLine = "Important: New FBA Orders for " regionName " Region";
MailApp.sendEmail(emails, subjectLine, messageBody);
}
}
CodePudding user response:
Added an if statement
function emailNotifications() {
const ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TestingSheet");
const lr = ss.getLastRow();
const templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();
for (let i = 2; i <= lr; i ) {
let emails = ss.getRange(i, 2).getValue();
let regionName = ss.getRange(i, 1).getValue();
let orders = ss.getRange(i, 3).getValue();
let messageBody = templateText.replace("{region}", regionName).replace("{orders}", orders);
let subjectLine = "Important: New FBA Orders for " regionName " Region";
if (email) {
MailApp.sendEmail(emails, subjectLine, messageBody);
}
}
}