ugh*_*tjs 5 javascript jquery google-docs google-apps-script
试图找出用于制作 Google 文档插件的 Google Apps 脚本。我有:
function helloWorld() {
return "Hello World";
}
Run Code Online (Sandbox Code Playgroud)
在我调用的 code.gs 下:
console.log("This should say Hello World: " + google.script.run.helloWorld())
Run Code Online (Sandbox Code Playgroud)
它返回:
This should say Hello World: undefined
Run Code Online (Sandbox Code Playgroud)
我缺少的显而易见的东西是什么?
google.script.run不会像您期望通常的 Apps 脚本函数那样返回值。相反,你应该使用.withSuccessHandler(functionToRun)
像这样:
google.script.run
.withSuccessHandler(functionToRun)
.helloWorld();
function functionToRun(argument) {
console.log("This should say Hello World: " + argument);
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,服务器端 Apps 脚本函数helloWorld将运行客户端函数functionToRun()并将结果helloWorld()作为参数传递。