Javascript Google脚本从code.gs返回index.html
如何从code.gs返回到index.html 我曾在“Index.html”中尝试过这个方法,但不起作用 index.htmlJavascript 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
$('#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();
}