Add item to existing menu in Google Apps Script
如何在Google Apps脚本中将项目添加到现有菜单(在Google文档中)?
我可以创建一个新菜单并向其中添加一个项目:
1 2 3 | DocumentApp.getUi().createMenu('MyMenu') .addItem('Insert My Thing', 'myFunction') .addToUi(); |
但是为单个项目添加整个菜单应该看起来确实很荒谬,而该菜单应该真正位于现有的"插入"菜单下。
当前不可能。即使文档说
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.
当我尝试以下代码时
1 2 3 | DocumentApp.getUi().createMenu('Tools') .addItem('Tool_item', 'toolItem') .addToUi(); |
创建了另一个"工具"菜单:
是,不是。
是的,您只能将菜单添加到现有的"附加组件"中。
否,除了您自己的自定义菜单外,没有其他地方。
以下代码可能会有所帮助:
1 2 3 4 5 6 7 8 9 10 11 | 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); } |
您可以使用自定义菜单(添加,合并...)来做您想做的事,但是您不能以任何方式修改内置菜单,这些菜单无法从Google-Apps-Script访问。
嗯,这在电子表格中吗?我将以下代码添加到电子表格-它正确地将具有一个项目的旧菜单替换为具有两个菜单项的新菜单。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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); }; |
通过Google Developers文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // 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(); |