我想知道Google Apps脚本是否可以显示一个HTML文件的内容
返回HtmlService.createHtmlOutputFromFile('0');
,在文件中有一个按钮,当按下按钮时,它将导致屏幕清除当前信息,并用第二个html文件替换自身
返回HtmlService.createHtmlOutputFromFile('1');
。 *请注意,我不想链接到google文档或另一个网页。只是想知道您是否可以在同一脚本文档中的html文件之间切换。如果可能的话,我的理由是要有一个“菜单”页面,该页面将显示不同的主题,并将所有内容保持在一个文档中以使其更有条理。
最佳答案
在某种程度上是可能的。我们将暗示有一个功能,可以从HTML文件中获取信息,然后将其写在页面上。如果要能够导航,则每个拥有的.html
文件必须在<script></script>
中具有以下功能
function changePage(page) {
document.open(); //should work fine without this
document.write(page);
document.close(); //should work fine without this
}
该位将处理使用新内容更改整个页面的内容。现在,我们需要以某种方式获取该内容。这将通过Google脚本中
.gs
文件内部的函数完成,如下所示function newPage(page) {
return HtmlService.createHtmlOutputFromFile(page).getContent()
}
然后在页面内部,每当要更改为新页面时,都需要一个按钮:
<button onclick="google.script.run.withSuccessHandler(changePage).newPage('success')">Next Page</button>
在这种情况下,
newPage()
函数需要一个字符串,该字符串将说明html的名称是什么。因此,遵循逻辑,我们将做return HtmlService.createHtmlOutputFromFile('success').getContent()`
一旦我们按下该按钮,并且该脚本完成并返回了我们想要的HTML字符串,我们就可以使用它来设置当前页面的HTML。
这是如何工作的完整示例。您可以在
Index.html
和success.html
之间导航代码
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function newPage(page) {
return HtmlService.createHtmlOutputFromFile(page).getContent()
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<script>
function changePage(page) {
document.open();
document.write(page);
document.close();
}
</script>
<body>
First Page
<button onclick="google.script.run.withSuccessHandler(changePage).newPage('success')">Next Page</button>
</body>
</html>
success.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<script>
function changePage(page) {
document.open();
document.write(page);
document.close();
}
</script>
<body>
It worked!
<button onclick="google.script.run.withSuccessHandler(changePage).newPage('Index')">First Page</button>
</body>
</html>
关于javascript - Google Apps脚本切换html文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41156292/