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.
Each menu item in the custom menu is associated with a Google Apps Script function. When a menu item is selected, its corresponding function will be run. For example, if the menu item Setting A is selected, the corresponding function settingA () will be run and this function will display a toast notification.
Apps Script can add new menus in Google Docs, Sheets, Slides, or Forms, with each menu item tied to a function in a script. (In Google Forms, custom menus are visible only to an editor who opens the form to modify it, not to a user who opens the form to respond.)
Note: Editor add-ons can have menu items as well, but use special rules they are defined. You can also assign an Apps Script function to an image or drawing in Google Sheets, so long as the script is bound to the spreadsheet. The example below shows how to set this up.
(In Google Forms, custom menus are visible only to an editor who opens the form to modify it, not to a user who opens the form to respond.) A script can only create a menu if it is bound to the document, spreadsheet, or form.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With