Create a Custom Menu with Apps Script
Google Sheets allows you to quite simply create custom menus, such as:
Simple Menu
In this example, the onOpen function adds the new menu upon opening the document:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Sheets-Pratique')
.addItem('Example 1', 'example1')
.addItem('Example 2', 'example2')
.addToUi();
}
function example1() {
Browser.msgBox('Easy!');
}
function example2() {
Browser.msgBox('Nice!');
}
If Example 1 is clicked, the example1 function will be executed (the principle is the same for Example 2 and the example2 function):
Separators
If needed, you can add separators to better distinguish groups:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Sheets-Pratique')
.addItem('Example 1', 'example1')
.addItem('Example 2', 'example2')
.addItem('Example 3', 'example3')
.addSeparator()
.addItem('Test 1', 'test1')
.addItem('Test 2', 'test2')
.addToUi();
}
Sub-menus
It is also possible to add sub-menus:
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Sheets-Pratique')
.addItem('Example 1', 'example1')
.addSubMenu(ui.createMenu('Example 2')
.addItem('Test 1', 'example2.test1')
.addItem('Test 2', 'example2.test2')
.addItem('Test 3', 'example2.test3'))
.addItem('Example 3', 'example3')
.addToUi();
}
For example, the functions of the sub-menu have been grouped in the example2 object:
const example2 = {
test1: function() {
Browser.msgBox('Clicked on "Test 1"!');
},
test2: function() {
Browser.msgBox('Clicked on "Test 2"!');
},
test3: function() {
Browser.msgBox('Clicked on "Test 3"!');
}
}
function example1() {
Browser.msgBox('Clicked on "Example 1"!');
}
function example3() {
Browser.msgBox('Clicked on "Example 3"!');
}