I have four tabs that contains similar sets of information. When a dropdown is set to "NEW" in Tab 1, I would like it to email a specific person. The problem I am getting is when Tab 2 is edited in the same area, it sends the same email.
What I would like to do is have each tab send an email to a different person when an edit is made only in that specific tab.
Example: Tab 1 is edited > email is sent to Person 1; Tab 2 is edited > email is sent to Person 2
Right now, when either Tab 1 or Tab 2 is edited, it sends an email to the same person.
I am sure I will need a new script for Tab 2, Tab 3, etc., but I want to figure out how to have only Tab 1's edits send an email.
Here is the script I am working with:
function onEdit(e) {
let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// EVENT VARIABLES
let range = e.range;
let row = e.range.getRow();
let col = e.range.getColumn();
let cellValue = sheet.getActiveCell().getValue();
let projectName = sheet.getRange(row,1).getValue();
let user = Session.getActiveUser().getEmail();
let cellLocation = sheet.getActiveCell().getA1Notation();
let url = "https://docs.google.com/spreadsheets/d/1bAJ0Asma4lX1tW3HYezW_Al6llkpaIKWSLptfjV-pAI/edit#gid=712318935"
if ( col == 1 && cellValue == "NEW") {
// Browser.msgBox('It works');
MailApp.sendEmail(
'email1@email.com',
'Staff Overview Update',
"There has been an update to the Staff Overview sheet requiring your attention: " + url + '&range=' + cellLocation,
{name: 'Team Leader'}
);
};
}
try:
function onEdit(e) {
let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
let sheetName = sheet.getName(); // Get the name of the active sheet
// EVENT VARIABLES
let range = e.range;
let row = e.range.getRow();
let col = e.range.getColumn();
let cellValue = sheet.getActiveCell().getValue();
let projectName = sheet.getRange(row,1).getValue();
let user = Session.getActiveUser().getEmail();
let cellLocation = sheet.getActiveCell().getA1Notation();
let url = "https://docs.google.com/spreadsheets/d/1bAJ0Asma4lX1tW3HYezW_Al6llkpaIKWSLptfjV-pAI/edit#gid=712318935";
if (col == 1 && cellValue == "NEW") {
let recipientEmail;
// Determine the recipient based on the sheet name
if (sheetName === "Tab 1") {
recipientEmail = 'email1@email.com';
} else if (sheetName === "Tab 2") {
recipientEmail = 'email2@email.com';
} else if (sheetName === "Tab 3") {
recipientEmail = 'email3@email.com';
} else if (sheetName === "Tab 4") {
recipientEmail = 'email4@email.com';
}
if (recipientEmail) {
MailApp.sendEmail(
recipientEmail,
'Staff Overview Update',
"There has been an update to the " + sheetName + " sheet requiring your attention: " + url + '&range=' + cellLocation,
{name: 'Team Leader'}
);
}
}
}
function onEdit(e) {
let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
let sheetName = sheet.getName(); // Get the name of the active sheet
let sheetId = sheet.getSheetId(); // Get the unique ID of the active sheet
// EVENT VARIABLES
let range = e.range;
let row = e.range.getRow();
let col = e.range.getColumn();
let cellValue = sheet.getActiveCell().getValue();
let projectName = sheet.getRange(row,1).getValue();
let user = Session.getActiveUser().getEmail();
let cellLocation = sheet.getActiveCell().getA1Notation();
let baseUrl = "https://docs.google.com/spreadsheets/d/1bAJ0Asma4lX1tW3HYezW_Al6llkpaIKWSLptfjV-pAI/edit";
if (col == 1 && cellValue == "NEW") {
let recipientEmail;
// Determine the recipient based on the sheet name
if (sheetName === "Tab 1") {
recipientEmail = 'email1@email.com';
} else if (sheetName === "Tab 2") {
recipientEmail = 'email2@email.com';
} else if (sheetName === "Tab 3") {
recipientEmail = 'email3@email.com';
} else if (sheetName === "Tab 4") {
recipientEmail = 'email4@email.com';
}
if (recipientEmail) {
let url = `${baseUrl}#gid=${sheetId}&range=${cellLocation}`;
MailApp.sendEmail(
recipientEmail,
'Staff Overview Update',
`There has been an update to the ${sheetName} sheet requiring your attention: ${url}`,
{name: 'Team Leader'}
);
}
}
}