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.
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:
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.
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);
}
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);
};
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();