我创建了一个Google工作表附加组件。但是,它在附加菜单中有点隐藏。有没有办法在用户打开工作表或用户单击工作表中的链接时自动启用Adds-on?我搜索了Google表格文档但没有找到任何内容。
EDIT1:
由于打开插件边栏自动似乎是一种糟糕的用户体验,如何通过单击工作表中的链接来打开?让用户选择通过单击插件插入的工作表中的链接来打开侧边栏。
答案 0 :(得分:3)
当然,您可以获取加载项的UI组件,以便在打开文档时自动打开。这对于加载项来说是可怕的行为,它永远不应该被发布,因为它会干扰其他加载项。但是,它仍然可以完成。
但是有一些限制。
这可能就像在showSidebar()
函数中添加对onOpen()
的调用一样简单,因为附加内容不需要任何授权。
function onOpen(e) {
var ui = SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('Show sidebar', 'showSidebar')
.addItem('Show dialog', 'showDialog')
.addToUi();
// Display sidebar
showSidebar();
}
但是如果我们想要启用和禁用该行为的选项怎么办? onOpen()
可以有一个控制行为的菜单项,而不是盲目地打开附加组件侧边栏,而只在启用时打开加载项。
/**
* Adds a custom menu with items to show the sidebar and dialog.
*
* @param {Object} e The event parameter for a simple onOpen trigger.
*/
function onOpen(e) {
var ui = SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('Show sidebar', 'showSidebar')
.addItem('Show dialog', 'showDialog')
.addSeparator()
.addItem(autoEnabled_()?"Disable auto-sidebar":"Enable auto-sidebar", "autoToggle_")
.addToUi();
// Display sidebar if auto-sidebar is enabled
if (autoEnabled_()) showSidebar();
}
现在我们已经介绍了与控制自动侧边栏autoEnabled_()
和autoToggle_()
相关的两个功能。第一个似乎告诉我们自动侧边栏的启用状态是什么,而第二个似乎是更改状态,并在菜单项中作为字符串参数提供(...因此它不能接受参数)。
由于自动显示仅在打开文档时很重要,因此我们需要一些方法来记住用户在文档使用之间的设置。可以使用PropertyService
解决这个问题。但是,我们需要小心,因为该服务需要授权。由于这是一个在首次安装时与ScriptApp.AuthMode.NONE
一起运行的附加组件,因此我们无法依赖于能够访问该服务。因此,在这种情况下将其包装在try...catch
块中。
为了使代码易于维护,如果我们保持"凌乱"那么它是最好的。像PropertyService这样的函数在一个地方。 " Messy",因为它依赖于字符串映射来存储和检索数据,我们的代码中的简单拼写错误会引入难以找到的错误。为了降低这种长期质量成本,我们可以将我们需要的两个功能合二为一,通过使状态切换为读取当前设置的子案例。这是生成的autoEnabled_()
函数,autoToggle_()
只是将参数提供回autoEnabled_()
以更改状态。
/**
* Get status of auto-sidebar, and optionally change it.
*
* @var {any} optSet (optional) Any truthy value will change the setting.
*
* @returns {Boolean} Returns true if enabled, false if not.
* Always returns false if ScriptApp.AuthMode.NONE.
*/
function autoEnabled_(optSet) {
try {
var autoState = PropertiesService.getUserProperties().getProperty('autoState');
}
catch (e) {
// Called with ScriptApp.AuthMode.NONE
return false;
}
if (optSet) {
autoState = (autoState == 'enabled')?'disabled':'enabled';
PropertiesService.getUserProperties()
.setProperty('autoState',autoState);
// Re-run the onOpen function to update menu
onOpen({authMode:ScriptApp.AuthMode.LIMITED});
}
return autoState == 'enabled';
}
/**
* Toggle state of auto-sidebar.
*/
function autoToggle_() {autoEnabled_('toggle');} // remove underscore and call from debugger to enable logs w/o UI
完整的代码段如下所示。要试用它,请从通用"添加"开始。脚本在编辑器中作为模板提供,并使用代码段中的代码替换原始onOpen()
。
/**
* Adds a custom menu with items to show the sidebar and dialog.
*
* @param {Object} e The event parameter for a simple onOpen trigger.
*/
function onOpen(e) {
var ui = SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('Show sidebar', 'showSidebar')
.addItem('Show dialog', 'showDialog')
.addSeparator()
.addItem(autoEnabled_()?"Disable auto-sidebar":"Enable auto-sidebar", "autoToggle_")
.addToUi();
// Display sidebar if auto-sidebar is enabled
if (autoEnabled_()) showSidebar();
}
/**
* Get status of auto-sidebar, and optionally change it.
*
* @var {any} optSet (optional) Any truthy value will change the setting.
*
* @returns {Boolean} Returns true if enabled, false if not.
* Always returns false if ScriptApp.AuthMode.NONE.
*/
function autoEnabled_(optSet) {
try {
var autoState = PropertiesService.getUserProperties().getProperty('autoState');
}
catch (e) {
// Called with ScriptApp.AuthMode.NONE
return false;
}
if (optSet) {
autoState = (autoState == 'enabled')?'disabled':'enabled';
PropertiesService.getUserProperties()
.setProperty('autoState',autoState);
// Re-run the onOpen function to update menu
onOpen({authMode:ScriptApp.AuthMode.LIMITED});
}
return autoState == 'enabled';
}
/**
* Toggle state of auto-sidebar.
*/
function autoToggle_() {autoEnabled_('toggle');} // remove underscore and call from debugger to enable logs w/o UI

感谢Serge在一段时间之前向我提出这个想法,当时我们第一次合作一个附加的想法!