Reputation: 137
UPDATED: This is totally possible. I just was closing the host window before the async method completed. My interpretation was that the running context would persist, even if the UI was closed out. Nope.
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.
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.
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.
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.");
}
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>
Upvotes: 2
Views: 5078
Reputation: 3084
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.
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);
}
Upvotes: 3
Reputation: 3845
Instead of using HtmlService.createTemplateFromFile
you could use:
HtmlService.createHtmlOutputFromFile('simpleDialog');
Here you can find more information about this function https://developers.google.com/apps-script/guides/html/
Upvotes: -1