Apps脚本 - 无法在html模板中的js函数中使用google.script.run? [英] Apps Script - Can't use google.script.run from within a js function in an html template?

查看:87
本文介绍了Apps脚本 - 无法在html模板中的js函数中使用google.script.run?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:这是完全可能的。我只是在异步方法完成之前关闭主机窗口。我的解释是,即使UI被关闭,运行上下文也会持续存在。不。

我想确认是否可以通过google.script.run从电子表格容器绑定项目中调用Apps脚本功能HTMLService加载的HTML模板中的JavaScript函数。我最初的发现表明不(见下文),但我可能会遗漏一些东西。

I want to confirm whether Apps Script functions in a spreadsheet container-bound project can or cannot be called via google.script.run from within a JavaScript function in an HTML template that was loaded by the HTMLService. My initial findings indicate "not" (see below), but I might be missing something.

google.script.run调用,在JS函数中调用时调用通过onclick处理程序,始终路由到错误处理函数,并显示以下错误代码: NetworkError:由于HTTP 0导致连接失败

The google.script.run call, when made from within the JS function called by the onclick handler, consistently routes to the error handler function, with the following error code: NetworkError: Connection failure due to HTTP 0.

如果我直接从按钮的onclick处理程序调用google.script.run代码,它就会运行。我希望能够在一个函数中使用它,这样我就可以建立一组数据来传递给我正在计划实现的实际方法。

If I call the google.script.run code directly from the onclick handler of a button, it does run. I'd prefer to be able to use it in a function, so that I can build up a set of data to pass into the real method I'm planning for the implementation.

我查了这个错误,但在这个简单的测试中,我没有引用库。

I did review this bug logged with Google, but in this simple test I am not referencing a library.

以下是更多细节:
- 新版本的工作表
- 使用最简单的代码创建的新电子表格和AS项目,以重新创建问题。

Here's more detail: - New version of sheet - New Spreadsheet and AS project created with the simplest code to recreate the issue.

我在应用程序中有以下代码容器绑定项目中的脚本文件:

I have the following code in an apps script file in the container-bound project:

function onOpen() 
{
   var testMenu = SpreadsheetApp.getUi() 
      .createMenu('Custom Menu');

      testMenu.addItem('Show UI in sidebar', 'showSidebar');
      testMenu.addToUi();
};

function showSidebar(){
  var html = HtmlService.createTemplateFromFile('simpleDialog');
  SpreadsheetApp.getUi().showSidebar(html.evaluate());
}

function doWorkInAppsScriptFile()
{
    Logger.log("doWorkInAppsScriptFile called.");
}

在simpleDialoghtml文件中,也是在Apps脚本项目中,我有以下测试代码:

In the "simpleDialog" html file, which is also in the Apps Script project, I have the following test code:

<h2>google script run test</h2>
<div style="width:98%;margin:10px">
    <div style="width:98%;margin:10px;padding:10px;text-align:center">
        <input type="button"  id="saveButton" onclick="onSave()" value="Save" style="background:DarkGreen;color:White;font-weight:bold;margin:10px"/>
    </div>
</div>

<script type="text/javascript">

function onSave(){          
  google.script.run.withFailureHandler(onFailure).doWorkInAppsScriptFile();     
  //Moving the following line to an onSuccess handler, and calling .withSuccessHandler,
  // allows the code to close the window appropriately. Leaving this in causes the error discussed in this q.    
  google.script.host.close();
}

function onFailure(error)
{
    alert("onFailure: " + error);
}

</script>


推荐答案

这里的罪魁祸首是 onSave() function:

The culprit here is this line in onSave() function:

google.script.host.close();

删除/评论此行,脚本运行正常。您看到的错误是因为您关闭了该行的侧栏,切断了与服务器端脚本的连接。

Remove/comment this line and the script runs fine. The error you see is because you close the sidebar with that line, severing connection to the server-side script.

如果您确实需要在<$ c之后关闭侧栏$ c> onSave()已完成执行,将该行移至成功处理程序回调:

If you do need to close the sidebar after onSave() has finished executing, move that line to the success handler callback:

function onSave(){          
  google.script.run
    .withSuccessHandler(onSuccess)
    .withFailureHandler(onFailure)
    .doWorkInAppsScriptFile();        
}

function onSuccess(data) 
{
  // do something with returned data
  // close the sidebar
  google.script.host.close();
}

function onFailure(error)
{
  alert("onFailure: " + error);
}

这篇关于Apps脚本 - 无法在html模板中的js函数中使用google.script.run?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆