The onOpen(e) trigger fires automatically whenever a user opens the spreadsheet. It's most commonly used to add a custom menu to the spreadsheet's toolbar, but it can also display alerts, refresh data, or set up the environment for the user.
Every time someone opens the Sales Tracker, this menu appears in the toolbar with quick access to the team's most-used automations.
Showing a Welcome Alert
functiononOpen(){var ui =SpreadsheetApp.getUi();var today =Utilities.formatDate(newDate(),Session.getScriptTimeZone(),"MMMM dd, yyyy"); ui.alert("Sales Tracker","Welcome! Today is "+ today +".\nRemember to update deal statuses before end of day.", ui.ButtonSet.OK);}
Refreshing Summary Stats on Open
This example recalculates key metrics and writes them to a Dashboard sheet every time the file is opened:
With the sample data, the Dashboard updates to show 2 Closed, 1 In Progress, and $398/mo total MRR each time the file opens.
Conditional Logic Based on Day
Show a reminder only on Mondays:
functiononOpen(){var day =newDate().getDay();// 0 = Sunday, 1 = Mondayif(day ===1){SpreadsheetApp.getUi().alert("Monday Reminder","Don't forget to update your deal statuses for the weekly digest!",SpreadsheetApp.getUi().ButtonSet.OK);}addSalesMenu();}
onOpen Limitations
The simple onOpen trigger runs with limited permissions — it cannot send emails, make HTTP requests, or access external APIs. For those use cases, create an installable onOpen trigger:
functioncreateOnOpenTrigger(){var ss =SpreadsheetApp.getActiveSpreadsheet();ScriptApp.newTrigger("onOpenInstallable").forSpreadsheet(ss).onOpen().create();}functiononOpenInstallable(e){// Can send emails, access Drive, call external APIsvar lastOpened =PropertiesService.getScriptProperties().getProperty("lastOpened");PropertiesService.getScriptProperties().setProperty("lastOpened",newDate().toString());Logger.log("Previously opened: "+ lastOpened);}