To create a custom menu in Apps Script, follow these steps:
- Open your Google Sheets document and navigate to “Tools” in the top menu bar.
- Select “Script editor” to open the Google Apps Script editor.
- In the editor, click on “File” and select “New” > “Script file”.
- Name your new script file and click “OK”.
- Add the code to create a custom menu using the class in the new script file
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Menu Item 1', 'menuItem1')
.addItem('Menu Item 2', 'menuItem2')
.addSeparator()
.addItem('Menu Item 3', 'menuItem3')
.addToUi();
}
function menuItem1() {
// Code to run when Menu Item 1 is selected
}
function menuItem2() {
// Code to run when Menu Item 2 is selected
}
function menuItem3() {
// Code to run when Menu Item 3 is selected
}
- Save the script by clicking “File” > “Save”.
- Return to your Google Sheets document and refresh the page. You should now see your custom menu in the top menu bar.
In this example, the onOpen()
the function creates a custom menu with three menu items and a separator. Each menu item is linked to a separate function that will be called when the item is selected. You can customize the menu items and their associated functions to meet your specific needs.