简体   繁体   English

如何将 Google Sheet 自定义功能作为插件

[英]How to make a Google Sheet custom function as an Add-On

I'm tying to create a custom function for google sheets that will be accessible with an Add-On to Google Sheets.我打算为 Google 表格创建一个自定义函数,可以通过 Google 表格的附加组件访问该函数。

The problem is when I run the Add-On as a test, when I type in my custom formula it gives me this error: #NAME?问题是当我将附加组件作为测试运行时,当我输入自定义公式时,它给了我这个错误:#NAME? Error Unknown function: 'myfunction'.错误未知函数:'myfunction'。

For simplicity sake, I have just been using the following simple function so I know the issue isn't with my functions code为简单起见,我刚刚使用了以下简单函数,所以我知道问题不在于我的函数代码

function myFunction(input) {
  var output = input * 2;
  return output;
}

I also tried to write custom function and publish as Add-on, but get "Error Unknown function" in the Spreadsheet.我还尝试编写自定义函数并作为插件发布,但在电子表格中得到“错误未知函数”。

Wasted several days on it, and finally got it working.折腾了好几天,终于搞定了。

  1. You must have a add-on menu.您必须有一个附加菜单。

    Ref: Custom Functions with add-ons?参考: 带有附加组件的自定义功能?

  2. You must define the scopes in your manifest.您必须在清单中定义范围。 And the scopes must be the same as your project.并且范围必须与您的项目相同。

Ref: https://issuetracker.google.com/issues/36763437#comment22参考: https : //issuetracker.google.com/issues/36763437#comment22

eg add this to your appsscript.json file:例如,将其添加到您的 appsscript.json 文件中:

  "oauthScopes": [
    "https://www.googleapis.com/auth/spreadsheets"
  ],

After scouring the web and questions/answers on Stack Overflow, I believe that as of May 2017 this is an Open Bug with GAS Add-ons.在网上搜索 Stack Overflow 上的问题/答案后,我相信截至 2017 年 5 月,这是一个带有 GAS 附加组件的开放错误。 I'm curious if you have come to the same conclusion.我很好奇你是否得出了同样的结论。

I tried Google's own date_add_and_substract Add-on example and couldn't get it to work myself.我尝试了 Google 自己的date_add_and_substract Add-on 示例,但我自己无法使其正常工作。 It's supposed to illustrate the difference between the Installed for Current User and Enabled in Current Document Add-on states.它应该说明为当前用户安装在当前文档加载项中启用状态之间的区别。 When the Enabled , the Add-on is supposed to load the following Customer Functions for working with Dates:Enabled 时,附加组件应该加载以下用于处理日期的客户函数:

  • DATEADD()
  • DATESUBTRACT()
  • DATETEST()

When I run the standalone script as Test as Add-on , enabled it, then enter any of those Custom Function into the cell I get that same #NAME?当我将独立脚本作为Test as Add-on 运行时,启用它,然后将这些自定义函数中的任何一个输入到单元格中,我得到相同的#NAME? Unknown function: 'XXXXXXX' Error. Unknown function: 'XXXXXXX'错误。

GAS 插件中的示例函数 BUG

In fact this seems to be one of the Open Bug Tickets .事实上,这似乎是Open Bug Tickets 之一

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

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