Edo*_*lga 3 html javascript google-apps-script
我使用 Google App Script 实现了库,但使用 google.script.run 从库中调用函数时遇到了一些困难。
这是我的图书馆的代码:
代码.gs
function ShowSideBar() {
var html = HtmlService.createTemplateFromFile('Index_librairie').evaluate()
.setTitle('Console de gestion')
.setWidth(300);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showSidebar(html);
}
function execution_appeler_par_html(){
Logger.log("execution_appeler_par_html");
}
Run Code Online (Sandbox Code Playgroud)
索引_libraire.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
google.script.run.withSuccessHandler(work_off).execution_appeler_par_html();
function work_off(e){
alert(e);
}
</script>
</head>
<body>
test de ouf
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我使用库的电子表格:Code.gs
function onopen() {
lbrairietestedouard.ShowSideBar();
}
Run Code Online (Sandbox Code Playgroud)
Google.script.run 不会重新配置 execution_appeler_par_html() 函数。我应该使用 libraryname.execution_appeler_par_html() 但这个语法在 google.script.run 的配置中不起作用
似乎google.script.run无法查看对象或自执行匿名函数的内部。就我而言,将任何代码放入对象或 IIFE 会导致控制台中抛出“不是函数”类型的错误。
您可以通过声明一个函数来解决这个问题,该函数将调用库和对象中的嵌套方法。
.GS 文件
function callLibraryFunction(func, args){
var arr = func.split(".");
var libName = arr[0];
var libFunc = arr[1];
args = args || [];
return this[libName][libFunc].apply(this, args);
}
Run Code Online (Sandbox Code Playgroud)
在 JavaScript 中,每个对象的行为都类似于键值对的关联数组,包括在这种情况下“this”将指向的全局对象。尽管 'libName' 和 'libFunc' 都是 String 类型,我们仍然可以使用上述语法在全局对象中引用它们。apply() 只是在“this”上调用函数,使结果在全局范围内可用。
以下是从客户端调用库函数的方法:
google.script.run.callLibraryFunction("Library.libraryFunction", [5, 3]);
Run Code Online (Sandbox Code Playgroud)
我不认为这个解决方案是我自己的——这是我不久前在 Bruce McPherson 的网站上看到的。您可以想出其他可能更适合您的情况的临时解决方案,但我认为这是最通用的。
| 归档时间: |
|
| 查看次数: |
1005 次 |
| 最近记录: |