javascript - Google脚本从code.gs返回到index.html

标签 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"
}

请帮忙。

最佳答案

根据documentation

"google.script.run is an asynchronous client-side JavaScript API available in HTML-service pages that can call server-side Apps Script functions."

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

google.script.run
    .withFailureHandler(function(err){
        console.error("error occured", e);
    })
    .withSuccessHandler(function(res){
        alert(res);
    })
    .getAmountOfImages();

res 成功处理程序回调函数中的参数将保存来自 Google 应用脚本的响应。

关于javascript - Google脚本从code.gs返回到index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36304114/

相关文章:

JavaScript 三元 if x && y else

javascript - 我可以在 highcharts 中让数据值为空吗?

Jquery:在没有事件的情况下执行函数

csv - 将 CSV 附件导入谷歌表格

javascript - 如何显示来自 GAS 的数据并自动更新每个单元格?

javascript - 尽管选项 reloadOnSearch 设置为 false,但强制 AngularJS 重新加载路由

javascript - 对 Django 服务器的简单 Ajax 调用导致响应时间很慢 - 慢 TTFB

javascript - Ajax 中的无限滚动

javascript - jquery mobile显示html内容

google-apps-script - Google Apps 脚本 : Access the form key that allows user to edit their responses