简体   繁体   English

找不到Google Apps脚本库嵌套函数

[英]Google Apps Script Library nested function not found

I have a library with the following code 我有一个包含以下代码的库

function onOpen() {
  SpreadsheetApp.getUi()
      .createMenu('Generate Code')
      .addItem('iOS', 'generateIOS')
      .addItem('Android', 'generateAndroid')
      .addToUi();
}

and in a different file in the same library I have 在同一个库中的另一个文件中

function generateAndroid(){
  generateFile('Android');
}

function generateIOS(){
  generateFile('iOS');  
}

Now, I'm using the library in a script 现在,我在脚本中使用库

function onOpen(){
 Library.onOpen(); 
}

But I get 但是我明白了

Script function not found: generateIOS For more information, see https://developers.google.com/apps-script/reference/base/menu#addItem(String,String)

If I create a generateIOS function in the script that's using the library, it works. 如果我在使用该库的脚本中创建了generateIOS函数,则该函数会起作用。 How do I solve this problem? 我该如何解决这个问题?

generateAndroid() and generateIOS() don't exist in the global namespace, they exist in the library's namespace. generateAndroid()generateIOS()在全局命名空间中不存在,它们存在于库的命名空间中。 So when you add items to the UI which call "generateIOS" and "generateAndroid", the script tries to run them from the same context as onOpen() and fails to find them. 因此,当您将项目添加到名为“ generateIOS”和“ generateAndroid”的UI时,脚本试图从与onOpen()相同的上下文中运行它们,但找不到它们。 Remember that addItem() takes a pair of Strings as parameters, so it's a pretty dumb function and doesn't know that you're referring to the "Library" functions instead of the global ones. 请记住, addItem()将一对Strings作为参数,因此它是一个非常愚蠢的函数,并且不知道您是在指“库”函数而不是全局函数。

There are two possible solutions I see: 我看到了两种可能的解决方案:

1) Add global functions that simply pass the function call along to Library. 1)添加仅将函数调用传递给Library的全局函数。

// inside the global context
function generateAndroid(){
    Library.generateAndroid();
}
function generateIOS(){
    Library.generateIOS();
}

2) Try adding the Library namespace to the addItem() string (note: I have NO idea if this will even work but it's worth a shot. 2)尝试将Library命名空间添加到addItem()字符串中(注意:我不知道这是否还能工作,但是值得一试。

.addItem('iOS', 'Library.generateIOS')
.addItem('Android', 'Library.generateAndroid')

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

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