Reputation: 83255
How do I add an item to an existing menu (in Google Docs) in Google Apps Script?
I can create a new menu and add an item to that:
DocumentApp.getUi().createMenu('MyMenu')
.addItem('Insert My Thing', 'myFunction')
.addToUi();
But it seems a bit ridiculous to add a whole menu for a single item that should really go under the existing "Insert" menu.
Upvotes: 8
Views: 13408
Reputation: 1
I've found a little trick to get a menu and add an item in the same script or another one.
In the same script, you can use the function getMenu() in another function and put .addItem() behind.
Main menu (menu.gs)
function getMenu() {
return menu = SpreadsheetApp.getUi()
.createMenu('Main menu')
.addItem('Title', 'function');
}
function onOpen() {
getMenu().addToUi();
}
function onInstall() {
getMenu().addToUi();
}
Also, with another script, you can import it as a Library and use it like before.
Another menu (menu2.gs / Where Service is menus.gs imported by Libraries)
function getMenu() {
return Service.getMenu()
.addItem('Another Title', 'anotherFunction');
}
function onOpen() {
getMenu().addToUi();
}
function onInstall() {
getMenu().addToUi();
}
Upvotes: 0
Reputation: 1023
Yes and no.
Yes, you can add your menu ONLY into the existing 'Add-ons'.
No, but nowhere else other than your own customized menu.
The code below may help:
function onOpen(e) {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createAddonMenu()
.addItem('Sort Current Column with Header until Blank Rows', 'sortCurrentColumn')
.addToUi();
}
function onInstall(e) {
onOpen(e);
}
Upvotes: 5
Reputation: 10887
Via Google Developers documentation
// To create an additional Menu-Item to an existing Main-Menu
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('First item', 'menuItem1')
.addSeparator()
.addItem('Second item', 'menuItem2')
.addToUi();
// To Create a Menu-Item to a Sub-Menu in an existing Main-Menu
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('First item', 'menuItem1')
.addSeparator()
.addSubMenu(ui.createMenu('Sub-menu')
.addItem('Second item', 'menuItem2'))
.addToUi();
Upvotes: -1
Reputation: 46792
You can do what you want with custom menus (add, combine...) but you can't in any way modify built in menus, they are not accessible from Google-Apps-Script.
Upvotes: 3
Reputation: 7350
Hmm, Is this in a spreadsheet? I added the following code to a spreadsheet - and it correctly replaced the old menu which had one item with a new menu that had the TWO menu items.
function someOtherFunction(){
}
function addMenu(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Add Menu",
functionName : "addMenu"
},{
name : "Menu 2",
functionName : "someOtherFunction"
}];
sheet.addMenu("Test Menu", entries);
}
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Add Menu",
functionName : "addMenu"
}];
sheet.addMenu("Test Menu", entries);
};
Upvotes: 1
Reputation: 1059
Currently it is not possible. Even though the documentation says
A document, spreadsheet, or form can only contain one menu with a given name. If the same script or another script adds a menu with the same name, the new menu will replace the old.
when I tried the following code
DocumentApp.getUi().createMenu('Tools')
.addItem('Tool_item', 'toolItem')
.addToUi();
another Tools menu was created:
Upvotes: 7