Google表格当用户打开工作表或点击工作表中的链接时,自动打开添加 [英] Google Sheets Adds-on open automatically when user opens a sheet or clicks a link in worksheet

查看:194
本文介绍了Google表格当用户打开工作表或点击工作表中的链接时,自动打开添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了Google表格附加组件。但是,它在附加菜单中有点隐藏。有没有办法在用户打开工作表时或用户单击工作表中的链接时自动启用附加功能?我搜索了Google表格文档,但没有发现任何内容。



EDIT1:



是一个不好的用户体验,如何通过点击表单中的链接打开?让用户选择通过点击由插件插入的表单中的链接来打开边栏。

当然,您可以获取一个UI组件,以便在打开文档时自动打开附加组件。这是附加组件的可怕行为,不应该发布它,因为它会干扰其他附加组件。但仍然可以这样做。



虽然有一些限制。


  • 必须为该文档启用加载项。请参阅附加授权生命周期

  • 根据附件执行的操作,可能需要由用户授权。例如,如果UserA和UserB共享一个文档,并且UserA安装并授权附加组件,UserB也会看到启用的附加组件,但需要单独授权以允许其在他们的帐户上运行。 li>


它可以像调用 showSidebar()到< c $ c> onOpen()函数,用于不需要任何授权的附加组件。




$ b $函数onOpen(e){
var ui = SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('Show sidebar','showSidebar')
.addItem('Show dialog','showDialog')
.addToUi();

//显示边栏
showSidebar();

$ / code>

但是如果我们想要启用和禁用该行为选项? c onOpen()可以有一个菜单项来控制行为,只有在启用时才打开附加组件,而不是盲目打开附加组件栏。

/ **
*添加一个包含项目的自定义菜单以显示侧边栏和对话框。
*
* @param {Object} e一个简单的onOpen触发器的事件参数。
* /
function onOpen(e){
var ui = SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('Show sidebar','showSidebar ')
.addItem('Show dialog','showDialog')
.addSeparator()
.addItem(autoEnabled _()?禁用自动侧栏:启用自动侧栏 ,autoToggle_)
.addToUi();

//如果启用了自动侧边栏,则显示侧边栏
if(autoEnabled_())showSidebar();

$ / code>

现在我们已经介绍了两个与自动侧边栏控件相关的功能, autoEnabled _() autoToggle _()。第一个似乎告诉我们什么启用状态是为自动侧边栏,而第二个似乎改变状态,并提供作为一个菜单项中的字符串参数(...所以它不能接受一个参数)。 p>

由于自动显示仅在文档打开时很重要,因此我们需要一些方法来记住用户在文档使用之间的设置。这可以通过使用 PropertyService 来解决。但是,我们需要小心,因为该服务需要授权。由于这是一个附加组件,它将在首次安装时与 ScriptApp.AuthMode.NONE 一起运行,因此我们不能依赖于能够访问该服务。因此,在这种情况下,将它封装在 try ... catch 块中。



为了保持,最好的办法是在一个地方保留像PropertyService这样的混乱功能。 杂乱,因为它依赖字符串映射来存储和检索数据,并且在我们的代码中的简单拼写错误会引入难以找到的错误。为了降低这种长期的质量成本,我们可以将我们需要的两个功能合并为一个,通过使状态切换为读取当前设置的子情况。下面是结果 autoEnabled _()函数,其中 autoToggle _()仅仅提供一个参数给 / ** $ b $ b *获取自动补充工具栏的状态,并可以选择更改它。
*
* @var {any} optSet(可选)任何truthy值都会改变设置。
*
* @returns {Boolean}如果启用则返回true,否则返回false。
*如果ScriptApp.AuthMode.NONE总是返回false。
* /
function autoEnabled_(optSet){
try {
var autoState = PropertiesService.getUserProperties()。getProperty('autoState');
}
catch(e){
//用ScriptApp.AuthMode.NONE调用
返回false;


if(optSet){
autoState =(autoState =='enabled')?'disabled':'enabled';
PropertiesService.getUserProperties()
.setProperty('autoState',autoState);
//重新运行onOpen函数来更新菜单
onOpen({authMode:ScriptApp.AuthMode.LIMITED});
}

return autoState =='enabled';
}

/ **
*切换自动补充工具栏的状态。
* /
函数autoToggle_(){autoEnabled _('toggle');} //删除下划线并从调试器调用以启用无日志UI

下面显示完整的摘要。要试用它,首先在编辑器中使用作为模板提供的通用添加脚本,然后用代码片段中的代码替换原始的 onOpen()。 / p>

/ ** *添加一个包含项目的自定义菜单,以显示侧边栏和对话框。 * * @param {Object} e一个简单的onOpen触发器的事件参数。 * / function onOpen(e){var ui = SpreadsheetApp.getUi().createAddonMenu().addItem('Show sidebar','showSidebar').addItem('Show dialog','showDialog').addSeparator().addItem (autoEnabled _()?禁用自动补充工具栏:启用自动补充工具栏,autoToggle_).addToUi(); //显示侧边栏如果自动侧边栏启用if(autoEnabled_())showSidebar();} / ** *获取自动侧边栏的状态,并可选地改变它。 * * @var {any} optSet(可选)任何truthy值都会改变设置。 * * @returns {Boolean}如果启用则返回true,否则返回false。 *如果ScriptApp.AuthMode.NONE总是返回false。 * / function autoEnabled_(optSet){try {var autoState = PropertiesService.getUserProperties()。getProperty('autoState'); } catch(e){//调用ScriptApp.AuthMode.NONE返回false; } if(optSet){autoState =(autoState =='enabled')?'disabled':'enabled'; PropertiesService.getUserProperties().setProperty('autoState',autoState); //重新运行onOpen函数来更新菜单onOpen({authMode:ScriptApp.AuthMode.LIMITED}); } return autoState =='enabled';} / ** *切换自动补充工具栏的状态。 * / function autoToggle_(){autoEnabled _('toggle');} //删除下划线并从调试器中调用以启用无UI的日志

$ b

致谢



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

I created an google sheets add-on. However, it is a bit hidden in the adds-on menu. Is there a way to enable the Adds-on automatically when user opens a sheet or when user clicks a link in a sheet? I searched on Google Sheets documentation but found nothing.

EDIT1:

Since opening an addon sidebar automatically seem to be a bad user experience, how about opening by clicking a link in a sheet? Let user choose to open the sidebar by clicking a link in a sheet that is inserted by an addon.

解决方案

Sure, you can get a UI component for an add-on to open automatically when a document is opened. This is HORRIBLE behavior for an add-on, and it should never be published because it would interfere with other add-ons. But still, it can be done.

There are some restrictions, though.

  • The add-on must be enabled for that document. Refer to Add-on Authorization Lifecycle.
  • Depending on the operations the add-on performs, it may need to authorized by the user. For example, if UserA and UserB share a document, and UserA installs and authorizes the add-on, UserB will see the add-on enabled as well, but would need to authorize it separately to allow it to run on their account.

It could as simple as adding a call to showSidebar() in your onOpen() function, for an add-on that doesn't require any kind of authorization.

function onOpen(e) {
  var ui = SpreadsheetApp.getUi()
      .createAddonMenu()
      .addItem('Show sidebar', 'showSidebar')
      .addItem('Show dialog', 'showDialog')
      .addToUi();

    // Display sidebar
    showSidebar();
}

But what if we want to have the option to have that behaviour enabled and disabled? Instead of blindly opening the add-on sidebar, the onOpen() could have a menu item to control behavior, and only open the add-on when enabled.

/**
 * 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();
}

Now we've introduced two functions related to control of the auto-sidebar, autoEnabled_() and autoToggle_(). The first appears to tell us what the enabled state is for the auto-sidebar, while the second appears to change state, and is provided as a string parameter in a menu item (... so it cannot accept a parameter).

Since the auto-display only matters when a document is being opened, we will need some way to remember the user's setting between uses of the document. That can be addressed using the PropertyService. We need to be careful, though, because that service requires authorization. Since this is an add-on which will run with ScriptApp.AuthMode.NONE when first installed, we can't rely on being able to access the service. So, wrap it up in a try...catch block in that case.

To keep the code easy to maintain, it's best if we keep "messy" functions like the PropertyService in one place. "Messy", because it relies on string maps for storage and retrieval of data, and simple typos in our code would introduce bugs that would be difficult to find. To reduce this long-term quality cost, then, we can combine the TWO functions we need into one, by making the state toggle a sub-case of reading the current setting. Here's the resulting autoEnabled_() function, with autoToggle_() simply providing a parameter back to autoEnabled_() to change state.

/**
 * 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

The complete snippet appears below. To try it out, start with the generic "Add on" script provided as a template in the editor, and replace the original onOpen() with the code from the snippet.

/**
 * 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

Acknowledgement

Thanks to Serge for presenting this idea to me some time back, when we were first collaborating on an add-on idea!

这篇关于Google表格当用户打开工作表或点击工作表中的链接时,自动打开添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆