简体   繁体   English

如何在新创建的 Google 表格中使用我的插件菜单?

[英]How can I get my add-ons menus available in newly created Google Sheets?

I'm having a bit of trouble understanding the workflow of a Google Sheet add-on.我在理解 Google 表格插件的工作流程时遇到了一些麻烦。 I'm using it in a sheet in which the script is bound too and it works perfectly fine.我在一个也绑定了脚本的工作表中使用它,它工作得非常好。 However, when I create a new Spreadsheet I can see my add-on menu created under Add-ons but not its submenus.但是,当我创建一个新的电子表格时,我可以看到我在Add-ons下创建的附加菜单,但看不到它的子菜单。 Also, if I look at the executions I get: Exception: You do not have permission to perform that action.另外,如果我查看我得到的处决: Exception: You do not have permission to perform that action. when trying to execute Spreadsheet.getUi() to create the menus.尝试执行Spreadsheet.getUi()来创建菜单时。

I have deployed a new version and adjusted the settings in the Marketplace SDK to match with those of the deployment.我已经部署了一个新版本,并调整了 Marketplace SDK 中的设置以匹配部署的设置。 Also, I have all the scopes of the script matching in the OAuth consent screen in my google project and in the Marketplace SDK.此外,我在我的谷歌项目和市场 SDK 的 OAuth 同意屏幕中拥有脚本匹配的所有范围。

The only way it works is if I go into Add-ons > Manage add-ons And click on Use in this document唯一可行的方法是,如果我 go 进入Add-ons > Manage add-ons ,然后单击Use in this document

What is the correct workflow here to have my add-on menus available automatically in newly created Spreadsheets?在新创建的电子表格中自动使用我的附加菜单的正确工作流程是什么?

The onOpen (e) is a simple trigger , and it is under a series of restrictions , specially onOpen (e)是一个简单的触发器,它受到一系列限制,特别是

They cannot access services that require authorization.他们无法访问需要授权的服务。

Before you click "Use in this document" , the add-on is in installed mode and the event object e.authMode has value AuthMode.NONE , so your trigger can't access the Properties service.在单击“在本文档中使用”之前,插件处于安装模式,并且事件 object e.authMode的值为AuthMode.NONE ,因此您的触发器无法访问Properties服务。 Only after you click "Use in this document" the trigger goes into "enabled mode" ( e.authMode === AuthMode.LIMITED ), and you can access Properties .只有在您单击“在本文档中使用”后,触发器才会进入“启用模式”( e.authMode === AuthMode.LIMITED ),并且您可以访问Properties

To fix that, you need to test if the add-on is installed or enabled, and show a menu option that allows the user to enable it if it is only installed.要解决这个问题,您需要测试该插件是否已安装或启用,并显示一个菜单选项,允许用户在仅安装时启用它。 See the example below:请参见下面的示例:

function onOpen (e) {
  var menu = SpreadsheetApp.getUi().createAddonMenu(); // Or DocumentApp.
  if (e && e.authMode == ScriptApp.AuthMode.NONE) {
    // Add a normal menu item (works in all authorization modes).
    menu.addItem('Enable add-on', 'enableAddOn');
  } else {
    // Add a menu item based on properties (doesn't work in AuthMode.NONE).
    var properties = PropertiesService.getDocumentProperties();
    var workflowStarted = properties.getProperty('workflowStarted');
    if (workflowStarted) {
      menu.addItem('Check workflow status', 'checkWorkflow');
    } else {
      menu.addItem('Start workflow', 'startWorkflow');
    }
  }
  menu.addToUi();
}

function enableAddOn () {
  onOpen();
}

When it is only installed, user sees only the option "Enable add-on".仅安装时,用户只能看到“启用附加组件”选项。 By clicking on it, the add-on becomes enabled, so the function can simply call onOpen() and the if..else will load the correct menu options.通过单击它,插件将启用,因此 function 可以简单地调用onOpen()并且if..else将加载正确的菜单选项。

Notice that you only need this fix because your simple trigger onOpen (e) is using the Properties service.请注意,您只需要此修复程序,因为您的简单触发器onOpen (e)正在使用Properties服务。 If you remove that, there is no need to test the installed/enabled mode.如果删除它,则无需测试已安装/启用模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM