Dashboard Mode - Triggers (onEdit, onOpen)
Dashboard Goal
This dashboard shows how to use Google Sheets triggers onEdit and onOpen to automate tasks. It answers: How can we automatically update or notify when data changes or when the sheet opens?
Jump into concepts and practice - no test required
This dashboard shows how to use Google Sheets triggers onEdit and onOpen to automate tasks. It answers: How can we automatically update or notify when data changes or when the sheet opens?
| Task | Status | Last Updated |
|---|---|---|
| Prepare Report | Pending | 2024-06-01 09:00 |
| Send Email | Done | 2024-06-01 08:30 |
| Update Website | Pending | 2024-05-31 17:00 |
| Backup Data | Done | 2024-06-01 07:45 |
| Team Meeting | Scheduled | 2024-06-02 10:00 |
=COUNTIF(B2:B6, "Pending")onOpen trigger script.onEdit trigger script.function onOpen() { const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Dashboard'); sheet.getRange('E1').setValue(new Date()); }function onEdit(e) { const logSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('EditLog'); const range = e.range; const oldValue = e.oldValue || 'N/A'; const newValue = range.getValue(); logSheet.appendRow([new Date(), range.getA1Notation(), oldValue, newValue]); }+----------------------+--------------------+ | Pending Tasks: [ 2 ] | Last Open: [time] | +----------------------+--------------------+ | Edit Log Table | | Timestamp | Cell | Old Value | New Value | |-------------|------|-----------|--------------| | 2024-06-01 | B3 | Pending | Done | | ... | ... | ... | ... | +---------------------------------------------+
When you open the sheet, the onOpen trigger runs and updates the Last Sheet Open Time KPI automatically.
When you edit any cell, the onEdit trigger runs and adds a new row to the Edit Log Table with details about the change.
The Pending Tasks Count updates automatically if you change any task status in the data table.
Try this: Edit the status of a task from "Pending" to "Done".
Question: Which components update?
onOpen trigger do in Google Sheets?onOpen trigger runs a script automatically when the spreadsheet is opened by a user.onEdit, which runs on cell changes, onOpen activates only on opening the file.onEdit trigger function in Google Sheets Apps Script?onEdit trigger function must accept an event object parameter e to access edit details.e and braces.onEdit function in Google Sheets Apps Script:function onEdit(e) {
if (e.range.getA1Notation() === 'A1') {
e.source.getActiveSheet().getRange('B1').setValue('Edited!');
}
}A1?e.range.getA1Notation().onOpen function is intended to show a custom menu, but it doesn't work:function onOpen() {
var ui = SpreadsheetApp.getUi;
ui.createMenu('My Menu')
.addItem('Say Hello', 'sayHello')
.addToUi();
}SpreadsheetApp.getUi without parentheses, so it references the function but does not call it.getUi() calls the method and returns the UI object needed to create the menu.onEdit trigger that automatically timestamps column B when a user edits column A in the same row. Which script correctly does this?e.range.getColumn() === 1.setValue(new Date()).