Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google脚本从code.gs返回index.html_Javascript_Jquery_Google Apps Script - Fatal编程技术网

Javascript Google脚本从code.gs返回index.html

Javascript Google脚本从code.gs返回index.html,javascript,jquery,google-apps-script,Javascript,Jquery,Google Apps Script,如何从code.gs返回到index.html 我曾在“Index.html”中尝试过这个方法,但不起作用 index.html $('#test').click(function() { alert(google.script.run.getAmountOfImages()); }); 代码.gs function getAmountOfImages() { return "Tom" } 请帮忙。根据 “google.script.run是一个异步客户端J

如何从code.gs返回到index.html

我曾在“Index.html”中尝试过这个方法,但不起作用

index.html

   $('#test').click(function() {
        alert(google.script.run.getAmountOfImages());
    });
代码.gs

function getAmountOfImages()
{
  return "Tom"
}
请帮忙。

根据

“google.script.run是一个异步客户端JavaScript API 在HTML服务页面中提供,可以调用服务器端应用程序脚本 功能。”

因此,您的结果将在回调中返回。因此,您必须使用处理程序函数,即
带FailureHandler
带SuccessHandler

google.script.run
    .withFailureHandler(function(err){
        console.error("error occured", e);
    })
    .withSuccessHandler(function(res){
        alert(res);
    })
    .getAmountOfImages();
成功处理程序回调函数中的
res
参数将保存来自google应用程序脚本的响应。

是您需要的

请参见下面的示例

代码.gs

function getAmountOfImages()
{
  return "Tom"
}
函数runOnGsAndGetValue(){
返回“你好,世界!”;
}
函数runOnGsOnly(){
Logger.log(“Hello World”);//在Appscript上,转到“查看”->“日志”查看日志
}
sample.html

   $('#test').click(function() {
        alert(google.script.run.getAmountOfImages());
    });

你好,世界


仅在Code.gs中运行代码

在Code.gs中运行代码并返回一个值 函数setText(文本){ var div=document.getElementById('para'); div.innerHTML=文本; } 函数onSuccess(str){ setText(str); } 功能失效(错误){ setText(“错误:+ERROR.message”); } 函数runAndGet(){ google.script.run.withFailureHandler(onFailure)。withSuccessHandler(onSuccess)。runOnGsAndGetValue(); } 函数onlyRun(){ google.script.run.runOnGsOnly(); }