我目前正在尝试制作一个 Google 表格插件供我的办公室使用。我已经完成了在市场上私下发布它并在整个域范围内分发它的工作,但我遇到了菜单项未正确添加的问题。我一直在阅读有关 AuthMode 的信息,据我了解,我的用户最初都是 AuthMode.NONE,应该能够添加简单的菜单。也许我对“简单菜单”的理解不同。在我们将所有计算机切换到 ChromeOS 之前,我真的需要在一周内完成这项工作(不是我的决定,而是 IT 的决定)
以下是我认为应该是所有相关代码的内容。每个功能也被正确定义。
function onOpen(e) {
SpreadsheetApp.getUi().createAddonMenu('Usage Macros')
.addItem('Summarize', 'firstRun')
.addSubMenu(SpreadsheetApp.getUi().createAddonMenu('Rate')
.addItem('PG&E', 'pge')
.addItem('CARE', 'care'))
.addSubMenu(SpreadsheetApp.getUi().createAddonMenu('Proposal Type')
.addItem('SunRun 0%', 'sunRun0')
.addItem('SunRun 1.9%', 'sunRun19')
.addItem('SunRun 2.5%', 'sunRun25')
.addItem('SunRun 2.9%', 'sunRun29')
.addItem('SunRun 3.5%', 'sunRun35')
.addItem('SunPower 0%', 'sunPower0')
.addItem('SunPower 2%', 'sunPower2')
.addItem('SunNova 0%', 'sunNova0')
.addItem('SunNova 0.9%', 'sunNova09')
.addItem('SunNova 1.9%', 'sunNova19')
.addItem('SunNova 2.9%', 'sunNova29')
.addItem('Cash', 'cash')
.addItem('GreenSky', 'greenSky')
.addItem('EnerBank', 'enerBank'))
.addItem('Finalize','kleanUp')
.addToUi();
}
function onInstall(e) {
onOpen(e);
}
我还应该提到添加了“Summarize”菜单项和“firstRun”,因为最初 onOpen 函数调用了另外 2 个函数,这些函数更改了一些格式并从打开的文档中删除了无用的信息。我从 onOpen 中删除了它,并为他们添加了一个菜单项,希望它能有所帮助。可惜它没有。
最佳答案
基于document :
If an add-on tries to create a top-level menu using the createMenu(name) syntax shown above, the name argument is ignored and the script is given an entry in the Add-ons menu under the add-on's published name.
这是我对您的代码所做的:
使用createAddonMenu()
function onOpen(e) {
var menu = SpreadsheetApp.getUi().createAddonMenu()
menu.addItem('Summarize', 'firstRun')
.addSubMenu(menu
.addItem('PG&E', 'pge')
.addItem('CARE', 'care'))
.addSubMenu(menu
.addItem('SunRun 0%', 'sunRun0')
.addItem('SunRun 1.9%', 'sunRun19')
.addItem('SunRun 2.5%', 'sunRun25')
.addItem('SunRun 2.9%', 'sunRun29')
.addItem('SunRun 3.5%', 'sunRun35')
.addItem('SunPower 0%', 'sunPower0')
.addItem('SunPower 2%', 'sunPower2')
.addItem('SunNova 0%', 'sunNova0')
.addItem('SunNova 0.9%', 'sunNova09')
.addItem('SunNova 1.9%', 'sunNova19')
.addItem('SunNova 2.9%', 'sunNova29')
.addItem('Cash', 'cash')
.addItem('GreenSky', 'greenSky')
.addItem('EnerBank', 'enerBank'))
.addItem('Finalize','kleanUp')
.addToUi();
}
结果
使用createMenu()
function onOpen(e) {
var menu = SpreadsheetApp.getUi()
menu.createMenu('Usage Macros')
.addItem('Summarize', 'firstRun')
.addSubMenu(menu.createMenu('Rate')
.addItem('PG&E', 'pge')
.addItem('CARE', 'care'))
.addSubMenu(menu.createMenu('Proposal Type')
.addItem('SunRun 0%', 'sunRun0')
.addItem('SunRun 1.9%', 'sunRun19')
.addItem('SunRun 2.5%', 'sunRun25')
.addItem('SunRun 2.9%', 'sunRun29')
.addItem('SunRun 3.5%', 'sunRun35')
.addItem('SunPower 0%', 'sunPower0')
.addItem('SunPower 2%', 'sunPower2')
.addItem('SunNova 0%', 'sunNova0')
.addItem('SunNova 0.9%', 'sunNova09')
.addItem('SunNova 1.9%', 'sunNova19')
.addItem('SunNova 2.9%', 'sunNova29')
.addItem('Cash', 'cash')
.addItem('GreenSky', 'greenSky')
.addItem('EnerBank', 'enerBank'))
.addItem('Finalize','kleanUp')
.addToUi();
}
结果
希望这对您有所帮助!
关于javascript - Google 表格附加组件,未添加自定义菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38277842/