I have a script that reads the information from a spreadsheet, and displays it in HTML. I try to adapt a button to it that activates the updating of the information when changes are made in the spreadsheet. I only manage to execute the part of window.location.reload(true);
. But the data is not updated.
These are the codes:
function doGet() {
return HtmlService
.createTemplateFromFile('Index')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function getData() {
return SpreadsheetApp
.openById('IDdelaspreadsheet')
.getActiveSheet()
.getDataRange()
.getValues();
}
function onSubmit(e){
doGet();
getData();
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<? var data = getData(); ?>
<table>
<? for (var i = 0; i < data.length; i++) { ?>
<tr>
<? for (var j = 0; j < data[i].length; j++) { ?>
<td><?= data[i][j] ?></td>
<? } ?>
</tr>
<? } ?>
</table>
// no es del código original. Aquí trato de incluir el botón
<form>
<input onclick="MostrarResultados()" type="button" value="Mostrar" />
</form>
<script type="text/javascript">
function MostrarResultados() {
google.script.run.onSubmit();
window.location.reload(true);
}
</script>
// hasta aquí la parte que inserto, no es del código original
</body>
</html>
The code has several problems.
google.script.run.onSubmit();
On the client side, it calls a server-side function, but there is no "callback" function that waits for the response from the server.onSubmit(e)
on the server side callsdoGet(e)
, but this is a function that is reserved to be called via the application's URL.window.location.reload(true);
In response from Erick Koleda dated 2013, to a question about the use of this code, HTMLService restricts access to the window object. I'm not entirely sure how it affects when usedHtmlService.SandboxMode.IFRAME
. To achieve the same I use another approach which I describe in the next point.Web application example
When the web application is loaded, the data is loaded using
window.onload
calling the functionactualizarTabla()
which in turn callsgoogle.script.run
usingwithSuccessHandler
so that once the server responds with the spreadsheet data in HTML table format, that data is passed to the element of the page in which we want to show them. To update the data, the button calls the same function.Code.gs
Index.html
References: