当从ScriptDbConsole.html链接到legend.html我收到以下错误信息:
对不起,您所要求的文件不存在。 请检查地址并再试一次。
这将在一个正常的环境下正常工作,但不是在这里,我猜。 它在script.google.com。
当创建在script.google.com项目中的新.html文件,它在相同的位置创建它,因为它没有其他人,所以这段代码实际上应该工作的权利? 如何从ScriptDbConsole.html打开legend.html?
<a href='legend.html' target='_blank'>Open in new window</a>
虽然HtmlService允许您提供HTML,它没有“托管”的页面,您无法通过网址直接访问Google Apps脚本项目的各种HTML文件。 相反,你的Web App将有一个URL发布时,这是你唯一的URL。
这里有一个方法,你可以成为您的脚本单独的页面,并让他们的行为类似于HTML文件的链接。
该doGet()
函数传递事件调用时,我们可以利用这一点来表示,我们希望提供的页面。 如果我们的Web应用程序ID是<SCRIPTURL>
这里是一个URL加一个查询字符串请求特定的页面将是什么样子:
https://script.google.com/macros/s/<SCRIPTURL>/dev?page=my1
使用HTML模板,我们可以生成必要的URL查询字符串+在运行。 在我们doGet()
我们只需要解析查询字符串,以确定提供哪个页面。
这里的脚本,包含按钮它们之间翻转两个示例页面。
Code.gs
/**
* Get the URL for the Google Apps Script running as a WebApp.
*/
function getScriptUrl() {
var url = ScriptApp.getService().getUrl();
return url;
}
/**
* Get "home page", or a requested page.
* Expects a 'page' parameter in querystring.
*
* @param {event} e Event passed to doGet, with querystring
* @returns {String/html} Html to be served
*/
function doGet(e) {
Logger.log( Utilities.jsonStringify(e) );
if (!e.parameter.page) {
// When no specific page requested, return "home page"
return HtmlService.createTemplateFromFile('my1').evaluate();
}
// else, use page parameter to pick an html file from the script
return HtmlService.createTemplateFromFile(e.parameter['page']).evaluate();
}
my1.html
<html>
<body>
<h1>Source = my1.html</h1>
<?var url = getScriptUrl();?><a href='<?=url?>?page=my2'> <input type='button' name='button' value='my2.html'></a>
</body>
</html>
my2.html
<html>
<body>
<h1>Source = my2.html</h1>
<?var url = getScriptUrl();?><a href='<?=url?>?page=my1'> <input type='button' name='button' value='my1.html'></a>
</body>
</html>
谷歌Apps脚本web应用程序主要是设计用于单页web应用程序的应用程序。 (不推荐),但它也可以被用来作为一个多页的应用程序。 下面是一个示例web应用程序:
Code.gs:
//@return Base Url
function getUrl() {
return ScriptApp.getService().getUrl()
}
//@return Html page raw content string
function getHtml(hash) {
return HtmlService.createHtmlOutputFromFile(hash).getContent()
}
//@return provided page in the urlquery '?page=[PAGEID]' or main index page
function doGet(e) {
var page = e.parameter.page
return HtmlService.createHtmlOutputFromFile(page || 'index')
.addMetaTag('viewport', 'width=device-width, initial-scale=1')
.setTitle('App Demo')
}
page1.html
<h3>This is Page 1</h3>
<p>Hello World!</p>
page2.html
<h4>This is Page2</h4>
<p>Goodbye World!</p>
的index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top" />
<title>Single Page App</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
h1 {
text-align: center;
margin: 2px;
text-transform: uppercase;
background-color: green;
}
span:hover,
a:hover {
background-color: yellowgreen;
}
body {
background-color: brown;
color: white;
font-size: 2em;
}
a:visited {
color: white;
}
</style>
</head>
<body>
<h1><span id="type">Single</span> Page App Demo</h1>
<div id="main">Loading...</div>
<script>
//Change base url
google.script.run
.withSuccessHandler(url => {
$('base').attr('href', url)
})
.getUrl()
//Function to handle hash change
function change(e) {
let hash = e.location.hash
if (!hash) {
main()
return
}
google.script.run
.withSuccessHandler(htmlFragment => {
$('#main').html(htmlFragment)
})
.getHtml(hash)
}
google.script.history.setChangeHandler(change)
//Function to add Main page html
function main() {
$('#main').html(`
<ul>
<li><a href="#page1">Page1</a></li>
<li><a href="#page2">Page2</a></li>
</ul>`)
}
//Loads Main html from main function
//Adds toggle to span to change to a Multiple page app
$(() => {
main()
$('#type').on('click', () => {
let hf = $('a').attr('href')
if (!hf) return
hf = hf.indexOf('#') + 1
$('#type').text(hf ? 'Multiple' : 'Single')
$('a').each((i, el) => {
$(el).attr('href', (i, v) =>
hf ? '?page=' + v.slice(1) : '#' + v.slice(6)
)
})
})
})
</script>
</body>
</html>
参考文献:
- Web应用程序指南
- setChangeHandler