Add item to existing menu in Google Apps Script

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 element to it:

DocumentApp.getUi().createMenu('MyMenu')
  .addItem('Insert My Thing', 'myFunction')
  .addToUi();

But it seems a little ridiculous to add a whole menu for a single item, which really should go to the existing "Insert" menu.

+6
source share
5 answers

Currently, maybe not . Although the documentation states

A document, spreadsheet or form can contain only 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 one.

when i tried the following code

DocumentApp.getUi().createMenu('Tools')
  .addItem('Tool_item', 'toolItem')
  .addToUi();

"":

enter image description here

+4

, , (, ...), , Google-Apps- Script.

+2

.

, "".

, , .

:

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);
}
+1

, ? - , , TWO.

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);
};
0

Google

// 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();
0
source

All Articles