Installable triggers in Google Apps Script allow you to create custom event triggers that are not tied to a specific user or document.
Here are two examples for each type of installable trigger, along with the corresponding code snippets:
OnEdit Trigger:
Example 1: Send email notification when a specific cell is edited.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function sendNotification(e) { var editedRange = e.range; var editedValue = editedRange.getValue(); var editedSheet = editedRange.getSheet().getName(); // Check if the edited cell matches your criteria if (editedSheet === "Sheet1" && editedRange.getA1Notation() === "A1" && editedValue === "Trigger") { var recipientEmail = "recipient@example.com"; var subject = "Cell A1 was edited!"; var body = "The value of cell A1 has been changed to 'Trigger'."; MailApp.sendEmail(recipientEmail, subject, body); } } |
Example 2: Logging the timestamp and user when any cell is edited.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function logEditDetails(e) { var editedRange = e.range; var editedSheet = editedRange.getSheet().getName(); var editedCell = editedRange.getA1Notation(); var editedValue = editedRange.getValue(); var editTimestamp = e.timestamp; var editUser = e.user.getEmail(); // Log the details of the edit event Logger.log("Edit Details:"); Logger.log("Sheet: " + editedSheet); Logger.log("Cell: " + editedCell); Logger.log("Value: " + editedValue); Logger.log("Timestamp: " + editTimestamp); Logger.log("User: " + editUser); } |
OnFormSubmit Trigger:
Example 1: Generate a PDF receipt when a form is submitted.
Generate a PDF receipt when a form is submitted. Click here for a full dedicated tutorial on how to generated PDF receipts when a Google Form is submitted.
1 2 3 4 5 6 7 8 9 |
function generatePDFReceipt(e) { var submittedValues = e.namedValues; // Generate PDF receipt using the submitted form data var pdfContent = // Code to generate the PDF content based on the submitted values // Save the PDF to Google Drive or send it via email // Code to save or send the PDF } |
Example 2: Update a spreadsheet with the submitted form data.
1 2 3 4 5 6 7 8 9 10 11 12 |
function updateSpreadsheet(e) { var submittedValues = e.namedValues; // Get the target spreadsheet and sheet var spreadsheetId = "your-spreadsheet-id"; var sheetName = "Sheet1"; var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName); // Append the submitted form data to the sheet var row = Object.values(submittedValues); sheet.appendRow(row); } |
These examples demonstrate different use cases for each type of installable trigger in Google Apps Script. Remember to adjust the code to match your specific requirements and customize the trigger logic as needed.