当从 ScriptDbConsole.html 链接到 legend.html 时,我收到以下错误消息:
Sorry, the file you have requested does not exist. Please check the address and try again.
这通常可以在正常环境中工作,但我猜这里不行。它在 script.google.com 中。
在 script.google.com 项目中创建一个新的 .html 文件时,它会在与其他文件相同的位置创建它,所以这段代码实际上应该可以正常工作吗?如何从 ScriptDbConsole.html 打开 legend.html?
<a href='legend.html' target='_blank'>Open in new window</a>
最佳答案
虽然 HtmlService 允许您提供 HTML,但它不是“托管”页面,您不能直接通过 URL 访问 Apps 脚本项目中的各种 html 文件。相反,您的 Web 应用程序在发布时会有一个 URL,这是您唯一拥有的 URL。
这里有一种方法,您可以从脚本中提供单独的页面,并使它们的行为类似于 html 文件链接。
doGet()
函数在调用时会传递一个事件,我们可以利用它来指示我们想要提供哪个页面。如果我们的 Web App ID 是 <SCRIPTURL>
,这是一个 URL 加上请求特定页面的查询字符串的样子:
https://script.google.com/macros/s/<SCRIPTURL>/dev?page=my1
使用模板化 HTML,我们可以即时生成必要的 URL + 查询字符串。在我们的 doGet()
,我们只需要解析查询字符串以确定要提供哪个页面。
这是脚本,包含两个示例页面,其中包含用于在它们之间翻转的按钮。
代码.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>
关于html - 链接到 Google Apps 脚本中的另一个 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15668119/