I'm very new to this and my job has a Google sheet that has all of our lead information from FB, and I'm trying to have a function (onEdit) that will send out an email to the lead based on one cell's data. For example, when A2 contains "Follow Up", then it sends out an email to the address in D2, and the body text comes from J2 (the canned response for the "Follow Up" data validation). Right now this works for row 2, however when inputting "Follow Up" in any other row it sends every row in the list an email. For example, when triggering the event in A29 it sends the body text in column J to every email in the list. I'm trying to make the event only send one email at a time (based on the active cell meeting validation requirements).
I've done my best looking at other posts on here to piece this code together using a for loop to also check for new rows (new rows are consistently being added to the sheet). The three if statements are probably sloppy, but I don't know how to clean that up yet into one singular if statement. I was thinking of adding a column that states whether to the email has been sent already, and updating the for loop or if statement to not send an email to those rows.
var sheetname = "Prometheus";
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var ac = sheet.getActiveCell();
var endRow = sheet.getLastRow();
var startRow = 1;
for (var i = endRow; i >= startRow; i--) {
var rangeToCheck = sheet.getRange(i, 1, 1, 21);
if (ac.getValue() == "Appt. Set (appointment is setup)") {
data = rangeToCheck.getValues();
Logger.log(data)
MailApp.sendEmail({
to: data[0][4],
subject: "Appt. Confirmation",
body: data[0][10]
}); break;
} else if (ac.getValue() == "Lead") {
data = rangeToCheck.getValues();
Logger.log(data)
MailApp.sendEmail({
to: data[0][4],
subject: "Welcome",
body: data[0][11]
}); break;
} else if (ac.getValue() == "Follow Up") {
data = rangeToCheck.getValues();
Logger.log(data)
MailApp.sendEmail({
to: data[0][4],
subject: "Follow Up",
body: data[0][12]
});
}
}
}
I expect when my active cell meets the condition in the if statement, for only one email to be sent to the contact info within the same row. Right now it's sending emails to everyone in the sheet when the condition is met in one row.