英文:
Google Apps Script Libraries no longer linking
问题
我有3个Google Apps脚本,我试图通过库选项允许它们彼此通信,但我还没有成功。曾经有一段时间,我将其中2个脚本链接起来,它能正常工作,然后我决定将其中一个脚本拆分成2个,但现在库不起作用了,我觉得我漏掉了一些明显的东西。
第三个脚本中列出了SendToSlack和EmployeeList库,看起来没问题。然后我尝试使用以下代码来调用它们:
// 这个代码在外部JS文件中
google.script.run.withSuccessHandler(populateNamesList).EmployeeList.getNames();
// 这个代码在标准GS文件中
SendToSlack.initalise(chan, botName, message, iconEmoji);
这段代码以前是有效的。现在当我加载页面时,我会得到以下错误:
Uncaught TypeError: Cannot read property 'getNames' of undefined
当我点击按钮来激活第二行时,我会得到以下错误:
Uncaught at userClicked
我认为代码是正确的,但我不知何故破坏了库?
编辑:甚至在线IDE中的自动完成也可以正常工作。所以我很确定代码是正确的。我不明白为什么发布后不起作用。
英文:
I have 3 Google Apps Scripts which I'm trying to allow to talk to each other through the library option, but I haven't managed to get it to work. At one point I had 2 linked and it worked, and then I decided to split one script into 2, but now the libraries aren't working and I feel like I'm missing something obvious.
The SendToSlack and EmployeeList libraries are listed in the third script and it looks fine. I've then tried to call these with:
// this one is in an external JS file
google.script.run.withSuccessHandler(populateNamesList).EmployeeList.getNames();
// this one is in a GS file as standard
SendToSlack.initalise(chan, botName, message, iconEmoji);
This code worked before. Now when I load the page I get the error:
Uncaught TypeError: Cannot read property 'getNames' of undefined
And when I click a button to activate the second line, I get:
Uncaught at userClicked
I think the code is correct but I've somehow broken the libraries?
Edit: The autocomplete in the online IDE even works. So I'm pretty sure the code is correct. I don't understand why it doesn't work when published.
答案1
得分: 0
通过使用google.script.run,您可以执行在您的服务器端应用脚本中定义的任何函数。根据文档:
> 执行具有相应名称的服务器端应用脚本函数。
但是,您不能像您所做的那样指定任何变量。这些函数必须在脚本的全局范围内声明,以便它们可以运行。在这种情况下,如果您想调用通过EmployeeList
全局对象访问的getNames()
函数,您应该在您的脚本中创建一个封装调用的全局函数。例如:
Code.gs
function getEmployeeNames() {
return EmployeeList.getNames();
}
Page.html
...
<script>
google.script.run.withSuccessHandler(populateNamesList).getEmployeeNames();
</script>
...
英文:
By using google.script.run you can execute any function defined in your server-side Apps Script. From the documentation:
> Executes the server-side Apps Script function with the corresponding name.
However, you cannot specify any variable as you are doing. The functions must be declared on the global scope of the script so that they can be ran. In this case, if you want to call the function getNames()
accessible through the EmployeeList
global object, you should create a global function in your script that encapsulates the call. For instance:
Code.gs
function getEmployeeNames() {
return EmployeeList.getNames();
}
Page.html
...
<script>
google.script.run.withSuccessHandler(populateNamesList).getEmployeeNames();
</script>
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论