Google Apps Script Adding A Custom Menu
Google Apps Script, Menu System, Control, Do More for Free
This tutorial uses Google Apps Script. If you need to review the Google Apps Script Primer, here it is: Take Me To The Primer
When you create a program in Apps Script, you need the ability to run it on demand, you may want to test specific features before scheduling, or you just love menus and need as many as possible.
I normally follow these steps to make a menu.
Step 1: Make a new file, and called it “Menu”. Remeber, the “.gs” is added automatically
Step 2: Paste this code to build the menu
function onOpen() {
var ui = SpreadsheetApp.getUi(); // Get the user interface object to add a custom menu
// Creates a custom menu with the name 'Folder Data'
ui.createMenu('Folder Data')
.addItem('Clear IDs', 'clearSheet') // Adds an item to the custom menu
.addItem('List Folder Contents', 'listFolderContents') // Adds another item to the custom menu
.addItem('Run the AI on the most Recent Submission', 'OpenAI')
.addToUi(); // Adds the custom menu to the UI
}
Step 3: Let’s review the areas to customize
function onOpen() {
var ui = SpreadsheetApp.getUi(); // Get the user interface object to add a custom menu
// Creates a custom menu with the name 'Folder Data'
//Change this value to whatever you want to displat
ui.createMenu('Folder Data')
...
//You can see the sub items here, and the functions that they run
//if you have a function called, listFolderContents();
//You leave off the (); and use ' ' as seen below
.addItem('Clear IDs', 'clearSheet') // Adds an item to the custom menu
.addItem('List Folder Contents', 'listFolderContents') // Adds another item to the custom menu
.addItem('Run the AI on the most Recent Submission', 'OpenAI')
.addToUi(); // Adds the custom menu to the UI
}
Step 4: When everything is ready, hit save
Step 5: Refresh the Google Sheet, Doc, etc that you are working in. The Apps Scrip will close, and the menu will load after a few seconds
The most common mistake is forgetting to omitt the () from the function name.
Enjoy making menus. And for the record, I have no idea how big these can become, so feel free to update me if you try to find the max element size.
Copyright © Domain Seven LLC. All rights reserved.
For permissions to use or share any content behind our paywall, please email us at: tonydeprato@domain7.tech .