Add item to existing menu in Google Apps Script
Asked Answered
H

6

8

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.

Harijan answered 25/11, 2013 at 17:1 Comment(0)
U
7

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:

enter image description here

Upu answered 25/11, 2013 at 17:47 Comment(3)
Huh...it seems confusing that the user would have a whole different menu for something that he would normally expect to be under "Insert", but I guess that the way it is.Harijan
I agree, I think the integration of custom menus is not the best at the moment. I especially hate the delay between opening the document and the moment a custom menu is added. It's not possible to extend the context menu either which I find very limiting. But I guess that's the way Google tries to separate core functions from extensions.Upu
The current title of the source of the quote is "Custom Menus in G Suite". It doesn't mention that built-in menus could be customized.Roadster
A
5

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);
}
Accumulation answered 2/2, 2017 at 15:52 Comment(0)
G
3

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.

Griffith answered 25/11, 2013 at 19:59 Comment(0)
M
1

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);
};
Madel answered 25/11, 2013 at 19:5 Comment(2)
Apologies...I should have specified the is Google Docs.Harijan
I think the question was about adding an item to one of the default menus (e.g. File, Edit, View). In case of custom menus you are right.Upu
L
0

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();
}
Lew answered 7/6, 2023 at 12:41 Comment(0)
H
-1

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();
Hungry answered 22/4, 2016 at 15:44 Comment(1)
This does answer the question, but merely repeats what the OP has already explained in his question.Useless

© 2022 - 2024 — McMap. All rights reserved.