Problem: You need an automated way to send email alerts whenever data changes in a Google Sheet, but you’re not sure how to set up the script, trigger it reliably, and handle failures.
Answer: Use a Google Apps Script bound to the sheet, create an onEdit or time‑driven trigger, log every attempt to a hidden sheet, and add simple retry logic for temporary send failures. The steps below walk you through the whole process, from prerequisites to a realistic example and a post‑launch checklist.
Prerequisites and Planning
- Google account ownership: The script runs under the account that creates it. Ensure you have edit rights on the target spreadsheet and permission to send email from that account.
- Spreadsheet layout: Decide which columns trigger an email (e.g., a status column changing to "Ready"). Create a hidden sheet named Log to store timestamps, row IDs, and success/failure flags.
- Permission scope: The script needs the https://www.googleapis.com/auth/script.send_mail scope. When you first run the script, Google will prompt you to grant it.
- Failure handling strategy: Determine whether you want a single retry, exponential back‑off, or a manual review queue. This guide uses a simple one‑retry approach.
Step‑by‑Step Implementation
- Open the spreadsheet and create the script. In Google Sheets, click Extensions → Apps Script. A new project opens with a default Code.gs file.
- Write the core function. Below is a minimal version that sends an email when column C changes to "Ready".
function sendAlert(e) {
var sheet = e.range.getSheet();
if (sheet.getName() !== 'Data') return; // only run on the Data sheet
var editedCol = e.range.getColumn();
var statusCol = 3; // column C
if (editedCol !== statusCol) return;
var newValue = e.value;
if (newValue !== 'Ready') return;
var row = e.range.getRow();
var rowData = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getValues()[0];
var email = rowData[4]; // assume column E holds the recipient address
var subject = 'Item Ready for Processing';
var body = 'The item in row ' + row + ' is now ready. Details:\n' + rowData.join('\n');
try {
MailApp.sendEmail(email, subject, body);
logResult(row, true, '');
} catch (err) {
// Simple retry once after a short pause
Utilities.sleep(2000);
try {
MailApp.sendEmail(email, subject, body);
logResult(row, true, '');
} catch (err2) {
logResult(row, false, err2.toString());
}
}
}
function logResult(row, success, errorMsg) {
var logSheet = SpreadsheetApp.getActive().getSheetByName('Log');
logSheet.appendRow([new Date(), row, success ? 'SUCCESS' : 'FAIL', errorMsg]);
}
Assumptions:
- The data you monitor lives on a sheet named Data.
- Column E contains a valid email address.
- A hidden sheet Log already exists.
- Save and test the function manually. Select sendAlert from the dropdown and click the Run button. Google will ask for authorization – grant it.
- Install an onEdit trigger. In the Apps Script editor, click the clock icon (Triggers) → Add Trigger. Choose:
- Function: sendAlert
- Event source: From spreadsheet
- Event type: On edit
- Deploy as a web app (optional). If you need external systems to fire the workflow via HTTP, click Deploy → New deployment, select Web app, set Execute as to "Me" and Who has access to "Anyone within your domain" (or "Anyone" if you must). Add a thin wrapper function that reads POST data and calls sendAlert.
- Set up error monitoring. Open the Log sheet and create a filter view that shows rows where the status column equals "FAIL". You can also create a time‑driven trigger that runs a summary function each night and emails you a list of failures.
Realistic Example: Order Confirmation Alerts
Imagine a small online retailer in Pakistan who tracks orders in a sheet called Orders. When the Status column (column D) changes to "Shipped", the owner wants an email to the customer with tracking details.
Implementation steps:
- Rename the script’s Data sheet to Orders and update statusCol to 4.
- Place the customer's email in column F.
- Modify the email body to include the tracking number from column G. var tracking = rowData[6]; var body = 'Your order is on the way!\nTracking #: ' + tracking;
- Test by editing a row’s status to "Shipped". Check the Log sheet for a SUCCESS entry and verify the email landed in the inbox.
This example shows how a few column adjustments turn a generic workflow into a business‑specific notification system without writing a separate script for each use case.
Common Mistakes and How to Fix Them
- Trigger not firing. Why: The script is bound to a different spreadsheet or the trigger was created on the wrong project. Fix: Open the script from the exact sheet you want to monitor, then recreate the onEdit trigger.
- Emails go to spam. Why: Sending many automated messages from a new account can trigger spam filters. Fix: Add a clear subject line, avoid excessive links, and ask recipients to mark the email as "Not spam".
- Permission errors. Why: The script lacks the send_mail scope or the user revokes access later. Fix: Re‑run the function to trigger the authorization flow, and keep the owner’s account active.
- Log sheet fills up quickly. Why: Every edit creates a row, even when the condition isn’t met. Fix: Log only when an email is attempted (as in the code above) or set a time‑driven cleanup script that purges entries older than 90 days.
- Rate‑limit errors. Why: Google limits daily email sends for free accounts (≈100 per day). Fix: Upgrade to a Google Workspace account or batch notifications into a single summary email.
Setup Checklist
- Confirm you own the Google account that will run the script.
- Identify the source sheet, target columns, and email address column.
- Create a hidden Log sheet with headers: Timestamp, Row, Status, Error.
- Copy the core function into Code.gs and adjust column indices.
- Run the function once to grant MailApp permission.
- Add an onEdit trigger pointing to sendAlert.
- Test with a single row edit; verify email receipt and log entry.
- Optional: Deploy as a web app if external calls are needed.
- Set up a nightly summary trigger to email you any FAILED log rows.
- Document the workflow for teammates and note the owner’s email for future permission changes.
Conclusion
Building a dependable email notification workflow with Google Apps Script is a matter of three things: a clear trigger, a concise send routine, and robust logging. By following the steps above you can turn a simple spreadsheet into an automated communication hub, catch failures early, and keep the process under your control. When the workflow grows—multiple sheets, richer templates, or integration with a CRM—consider moving the script to a standalone project and using the Advanced Gmail Service for richer formatting.
Ready to see a full ecommerce‑focused Apps Script in action? View the Apps Script ecommerce source‑code package. Need help deploying or customizing the workflow? Ask us about deployment or custom workflow support.