首页 > 解决方案 > 谷歌应用脚​​本中的 webapp javascript 函数

问题描述

我不是英语用户,但我正在尝试正确书写。请得到这个

我想要

A== 谷歌应用脚​​本 .gs

var result = solvethis(1,3);
Logger.Log(result) // 4

B== webapp javascript .html

<script>
    function solvethis(a,b){

    return a+b
    }
</script>

我为什么要这样做。

我想使用一些带有 make javascript => alasql.js 的数据库

但在 .gs -> alasql.js 中不起作用。

所以我必须使用html。

我不知道很多,但我知道 dget。


1===.gs

function dget(e){
 return HtmlService.createTemplateFromFile("index").evaluate();
}
alasql(select * )

2===.html

<script src="https://cdn.jsdelivr.net/npm/alasql@0.5"></script>

---------------- => 它不起作用。

我不想直接打开 webapp 页面。但我想要 .gs 中的 js 库

我怎样才能做到这一点?

标签: javascriptgoogle-apps-script

解决方案


不能从服务器调用客户端函数,唯一的例外是为客户端发起的对服务器函数的调用指定的成功或失败处理程序。请注意,您的服务器端代码无法知道哪个客户端函数已注册为响应处理程序,或者即使根本没有注册。

所以不,你不能“只是”从你的 .gs 文件中调用一个 .html 定义的函数。

请查看HTMLService 指南和 API 参考

// client.html (you write the UI and other stuff for the sidebar or modal or webapp page)
function calledByServer(serverOutput) {
  console.log({ serverOutput }); // or whatever you want to do with the response
}
function doServer(...argsArray) { //invoke somehow, e.g. a client-side click handler
  google.script.run
    .withSuccessHandler(calledByServer)
    .ServerSideFunctionName(argsArray);
}

// Code.gs
function ServerSideFunctionName(foo) {
  console.log(foo); // Stackdriver logging, since Logger is instance specific
  // Do something with foo
  return someValueToSendToClient;
}

如果您想使用该库,则必须在纯客户端代码中构建并使用 UI 来使用它。


推荐阅读