Google Apps Script Tutorial: Create a Custom Sheets Menu
Google Apps Script adds JavaScript-powered automation to Google Workspace. This first example creates a custom menu in a spreadsheet and runs a small function when you choose a menu item.
Open a script bound to your spreadsheet
Open a Google spreadsheet and choose Extensions > Apps Script. This creates or opens a container-bound script project associated with that spreadsheet. A bound project can add menus and work with its parent file.
Add a custom menu
Replace the starter code in the script editor with this example, or adapt an existing onOpen function by adding the menu-building statements to it.
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu("My Tools")
.addItem("Say hello", "sayHello")
.addToUi();
}
function sayHello() {
SpreadsheetApp.getUi().alert("Hello from Apps Script!");
}
Save and try the menu
- Save the project and give it a descriptive name.
- Return to the spreadsheet and reload it.
- Open My Tools in the menu bar and select Say hello.
The example displays a message and does not change spreadsheet data. The onOpen simple trigger runs when a user with edit access opens the spreadsheet. It does not run for users who only view or comment on the file.
Important setup notes
- If the project already has an
onOpenfunction, merge the menu creation into that function. A project should not define multiple functions with the same name. - Simple triggers have restrictions on services that require authorization. If a later script requests access, review the requested permissions before approving them.
- Only a container-bound script can add a custom menu to its current spreadsheet.