// Function to send email notification when a particular cell is edited
function sendEmailNotification(e) {
// Check if the event object is valid and contains the necessary properties
if (!e || !e.source || !e.range) {
return; // Exit the function if the event object is invalid
}
const sheet = e.source.getActiveSheet();
const range = e.range;
// Check if the edited range is within the header row (assuming the header is in row 1)
if (range.getRow() === 1) {
return; // Exit the function if the header row is edited
}
const editedRow = range.getRow();
const editedColumn = range.getColumn();
// Define the recipient column (J) and the columns to include in the email (A, C, E, G, H)
const recipientColumn = 10; // Column J
const columnsToInclude = [1, 3, 5, 7, 8]; // Columns A, C, E, G, H
// Check if the edited column is the recipient column (J)
if (editedColumn !== recipientColumn) {
return; // Exit the function if the edit is not in the recipient column
}
// Get the recipient's email address from column J
const recipient = sheet.getRange(editedRow, recipientColumn).getValue();
// Ensure that a recipient email is specified for the task
if (!recipient) {
return; // No recipient email found, so exit the function
}
// Extract data from the edited row for the specified columns
const headerRow = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
const taskDetails = columnsToInclude
.map(colIndex => `<tr><td style="padding: 8px; border: 1px solid #ddd; background-color: #f2f2f2;"><b>${headerRow[colIndex - 1]}:</b></td><td style="padding: 8px; border: 1px solid #ddd;">${sheet.getRange(editedRow, colIndex).getValue()}</td></tr>`)
.join("");
// Get the subject and the hyperlink URL from the G column (Email PDF Files and Subject)
const subject = sheet.getRange(editedRow, 7).getValue(); // Assuming G column (7) contains the subject text
const hyperlinkUrl = sheet.getRange(editedRow, 7).getFormula().match(/"(.*?)"/)[1]; // Extract the URL from the formula
// Format the received date and time for display
const receivedDate = new Date(); // Replace this with the actual receivedDate variable from your existing code
const formattedReceivedDate = Utilities.formatDate(receivedDate, Session.getScriptTimeZone(), "EEE, dd MMM yyyy HH:mm");
// Create the email body with a polished and professional template
const emailBody = `
<html>
<body style="font-family: Arial, sans-serif; font-size: 14px;">
<p>Dear ${recipient},</p>
<p>A new task has been assigned to you. Please find the details below and take the necessary actions accordingly:</p>
<table style="border-collapse: collapse; border: 1px solid #ddd; width: 100%;">
${taskDetails}
<tr>
<td style="padding: 8px; border: 1px solid #ddd; background-color: #f2f2f2;"><b>Subject:</b></td>
<td style="padding: 8px; border: 1px solid #ddd;"><a href="${hyperlinkUrl}" style="color: #0078d4; text-decoration: none;">${subject}</a></td>
</tr>
</table>
<p>Kindly respond by replying to this email if you have any questions or require any further information.</p>
<p>Thank you and best regards,</p>
<p>Your Name</p>
</body>
</html>
`;
const emailSubject = "Task Assignment: " + subject; // Enhance the subject with task information
// Send the email as HTML with the emailBody
MailApp.sendEmail({
to: recipient,
subject: emailSubject,
htmlBody: emailBody
});
}