To run a script automatically when a Google Sheet is opened, you must use the reserved function name onOpen(e). This is a simple trigger that tells Google Apps Script to execute specific code every time a user with edit access opens the spreadsheet file. This is primarily used to create custom menus, set initial cell values, or refresh data dashboards without requiring the user to manually trigger a script from the editor.
What You'll Need
- A Google account with access to Google Sheets.
- A spreadsheet where you have editor or owner permissions.
- Basic familiarity with the Google Apps Script editor (though no advanced coding knowledge is required for simple triggers).
Step-by-Step
- Open the Google Sheet you wish to automate.
- Navigate to the top menu and select Extensions > Apps Script. This opens a new browser tab containing the script editor. Note that menu labels may vary slightly depending on your region or account type.
- If the editor is empty, you will see a default function called
myFunction(). Delete all existing code in the editor window to start with a clean slate. - Type or paste the following code exactly as shown. For this example, we will create a custom menu that appears in the toolbar when the sheet opens:
function onOpen(e) { var ui = SpreadsheetApp.getUi(); ui.createMenu('Custom Tools') .addItem('Run My Process', 'myMainFunction') .addSeparator() .addItem('Help', 'showHelpDialog') .addToUi(); } - Below the
onOpenfunction, you must define the functions that the menu items call. If you don't, the script will throw an error when a user clicks the menu item. Add these placeholder functions:function myMainFunction() { SpreadsheetApp.getUi().alert('The main process is running!'); }function showHelpDialog() { SpreadsheetApp.getUi().alert('This is the help menu.'); } - Click the Save icon (the floppy disk) in the toolbar or press
Ctrl + S(Windows) orCmd + S(Mac). Give your project a name, such as "Auto-Open Script". - To test the script without refreshing the entire browser page, select
onOpenfrom the function dropdown menu at the top of the editor and click Run. - The first time you run the script, Google will prompt you for permissions. Click Review Permissions, select your account, click Advanced, and then click Go to [Project Name] (unsafe). Finally, click Allow.
- Return to your Google Sheet. You should now see a menu labeled "Custom Tools" next to the standard Help menu. If it isn't there, refresh the browser page.
Common Mistakes
One of the most frequent errors is misspelling the function name. The trigger is case-sensitive; it must be written exactly as onOpen. Writing onopen or OnOpen will result in the script failing to trigger automatically because Google's system specifically looks for the camelCase version.
Another common mistake is attempting to perform actions that require high-level authorization within a simple trigger. Simple triggers (like onOpen) run in a restricted mode. For example, a simple onOpen script cannot access services that require authentication, such as sending an email via MailApp or accessing another user's private files. If your script needs to do these things, you must use an "Installable Trigger" instead of a simple trigger.
Users also often forget to save the project before refreshing the sheet. If the code is written but not saved in the Apps Script editor, the Google Sheet will continue to run the previous version of the script (or no script at all), leading to confusion during the debugging process.
Tips
To keep your spreadsheet fast, avoid placing heavy calculations or large data fetches inside the onOpen function. Because this script runs every time the file is opened, a slow script will make the spreadsheet feel laggy or unresponsive for several seconds while the user waits for the UI to load. If you have a complex process, use onOpen only to create a menu button, then let the user trigger the heavy process manually.
When creating custom menus, use addSeparator() to group related functions. This improves the user experience, especially if you have more than five different script options. It makes the menu look professional and organized.
If you are sharing the sheet with others, remember that they will also need to authorize the script the first time they run a function from your custom menu. You cannot "pre-authorize" the script for other users; Google's security model requires each user to grant permission for the script to act on their behalf.
Troubleshooting
If the script does not run when you open the sheet, first check the Executions tab in the Apps Script editor (the clock icon on the left sidebar). This log shows every time a script was triggered and whether it completed successfully or failed. If you see a "Failed" status, click on the execution to see the specific error message.
If the execution log is empty, it means the trigger didn't fire at all. Double-check the spelling of onOpen. If the spelling is correct, ensure the script is bound to the sheet (meaning you opened the editor via the Extensions menu of that specific sheet) rather than being a standalone script. Standalone scripts cannot use simple onOpen triggers for a specific spreadsheet unless they are linked via an installable trigger.
If you receive a "Permission Denied" error in the execution log, it is likely because your onOpen function is trying to perform a restricted action (like accessing an external API or sending an email). To fix this, you must convert the simple trigger to an installable trigger. In the Apps Script editor, click the Triggers (clock) icon, click + Add Trigger, select the onOpen function, and set the event source to "From spreadsheet" and the event type to "On open".
Frequently Asked Questions
Does the script run for people with View-only access?
No. Simple onOpen triggers only execute for users who have edit permissions for the spreadsheet. Viewers will not see custom menus or experience any automated script actions.
Can I run a script on open for a mobile device?
Generally, no. Custom menus and onOpen triggers that modify the UI do not function in the Google Sheets mobile app for iOS or Android. These scripts are designed for the desktop browser experience.
How many scripts can I run on open?
You can only have one function named onOpen. If you need to run multiple different tasks, you should create one onOpen function and then call other functions from within it. For example, onOpen could call setupMenu() and refreshData() sequentially.
Will the script run every time I switch tabs within the sheet?
No. The onOpen trigger only fires when the entire spreadsheet file is loaded or refreshed. To run a script when a specific tab (sheet) is selected, you would need to use the onSelectionChange(e) trigger, though it is more limited in scope.
Is there a limit to how long the onOpen script can run?
Yes. Simple triggers have a maximum execution time (typically 30 seconds). If your script exceeds this limit, Google will terminate it automatically to prevent the spreadsheet from freezing. For longer tasks, use a manual trigger or a time-driven trigger.