The OnOpen trigger in Google Apps Script is a type of event trigger that runs a script when a user opens a Google Sheets file or a Google Docs document. It allows you to perform certain actions or customize the behavior of the file when it is accessed by users.
Here are five examples of using the OnOpen trigger:
Displaying a custom menu: You can use the OnOpen trigger to create a custom menu that appears when the file is opened, providing users with additional functionality. Here’s an example that adds a custom menu to a Google Sheets file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function onOpen() { var ui = SpreadsheetApp.getUi(); ui.createMenu("Custom Menu") .addItem("Option 1", "option1Function") .addItem("Option 2", "option2Function") .addToUi(); } function option1Function() { // Code for option 1 } function option2Function() { // Code for option 2 } |
Setting up initial formatting: You can use the OnOpen trigger to automatically format the sheet or document when it is opened, ensuring consistent formatting for all users. Here’s an example that sets the font color and background color for a range in a Google Sheets file:
1 2 3 4 5 6 |
function onOpen() { var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("A1:C10"); range.setFontColor("red"); range.setBackground("yellow"); } |
Displaying a welcome message: The OnOpen trigger can be used to show a custom message or notification to users when they open the file. This can be helpful for providing instructions or important information. Here’s an example that displays a welcome message in a dialog box:
1 2 3 4 |
function onOpen() { var ui = SpreadsheetApp.getUi(); ui.alert("Welcome to this Google Sheets file!"); } |
Updating external data: You can use the OnOpen trigger to automatically update external data sources or APIs when the file is opened, ensuring the latest data is available. Here’s an example that fetches data from an API and updates a range in a Google Sheets file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function onOpen() { var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getRange("A1:A10"); // Fetch data from an API var url = "https://api.example.com/data"; var response = UrlFetchApp.fetch(url); var data = JSON.parse(response.getContentText()); // Extract the values you want to update in the range var values = data.map(function(item) { return [item.value]; }); // Update the range with the fetched data range.setValues(values); } |
Enforcing access permissions: The OnOpen trigger can be used to enforce access permissions by restricting certain actions or displaying an access denied message based on user roles or conditions. Here’s an example that checks if the user has editing permission and displays an access denied message if not:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
function onOpen() { var user = Session.getActiveUser(); var userHasEditPermission = hasEditPermission(user); if (!userHasEditPermission) { var ui = SpreadsheetApp.getUi(); ui.alert("Access Denied. You do not have permission to edit this file."); } } function hasEditPermission(user) { var file = SpreadsheetApp.getActiveSpreadsheet(); var protection = file.getProtection(); var protectionTypes = protection.getProtectionTypes(); for (var i = 0; i < protectionTypes.length; i++) { var users = protection.getEditors(protectionTypes[i]); for (var j = 0; j < users.length; j++) { if (users[j].getEmail() === user.getEmail()) { return true; // User has edit permission } } } return false; // User does not have edit permission } |
These examples showcase different ways to utilize the OnOpen trigger in Google Apps Script. You can customize the code snippets to match your specific requirements and enhance the user experience when opening Google Sheets files or Google Docs documents.