在Google Script HTML Service中刷新div或整页
我在刷新Google Apps脚本中的HTML页面或div内容时遇到了困难。 我在编程方面自学成才,所以我对整个工作知之甚少; 请耐心等待我的代码。
基本上,我有一个应用程序,一旦用户点击按钮,它就会更改Google文档中的信息并刷新应用程序以显示更改。 我需要有关刷新部分的帮助。
这是html服务部分的片段:
... New Requests
<? for (var i = 0; i <? for (var j = 0; j Name: School: Program Type:
First Choice at: <input onclick="google.script.run.finalTime('','First')" type="button" value="Finalize First Choice">
Second Choice at: <input onclick="google.script.run.finalTime('','Second')" type="button" value="Finalize Second Choice">
Third Choice at: <input onclick="google.script.run.finalTime('','Third')" type="button" value="Finalize Third Choice">
<input onclick="google.script.run.deletePre('')" type="button" value="Delete" class="button">
基本上,脚本从电子表格中提取信息。 一旦我点击一个按钮,它是否可以刷新脚本甚至页面,以便用户不必手动? 我尝试在没有有意义的结果的情况下搜索类似的查询,并且我尝试添加“.reload()”和“.html(data)”的方法并不是很有效。 我有什么想法可以解决这个问题吗?
旧线程,但也许这将有助于某人……如何重建和显示整个页面……
index.html的一部分
Refresh
code.gs的一部分
function getNewHtml(e) { var html = HtmlService .createTemplateFromFile('index') // uses templated html .evaluate() .getContent(); return html; }
您可以使用window.location.href
导致加载HTML App的浏览器选项卡进行刷新。
window.myFunctionToRefresh = function() { window.location.href = "https://script.google.com/macros/s/YourAppsScriptID_Here/exec"; return false; };
我使用它,它的工作原理。 。 。 但在某些情况下页面会刷新,但Apps脚本无法完全重新加载。 我仍然不确定为什么会这样。 所以,我无法完全解释或认可它。
您可以使用DOM方法和属性将新HTML注入Web页面,而无需重新加载页面。
document.getElementById('element_ID_Here').innerHTML = "Hello World";
jQuery有自己的方法:
$("#element_ID_Here").html("Hello World");
您已经知道如何使用google.script.run
,因此您可以调用服务器,获取一些HTML内容作为字符串,返回它,并使用withSuccessHandler(functionToRunOnSuccess)
注入新的HTML。
让我们想象你想要替换“主体”中的所有内容:
给那个DIV一个id:
从服务器获取内容HTML内容作为字符串,当.gs
代码返回某些内容时,运行withSuccessHandler
,然后将其注入该DIV。 如果你的withSuccessHandler
被命名为onGotHtml
:
function onGotHtml(argMyNewHTML) { document.getElementById('idMainBody').innerHTML = argMyNewHTML; };