链接到Google Apps脚本中的另一个HTML页面 [英] Linking to another HTML page in Google Apps Script

查看:83
本文介绍了链接到Google Apps脚本中的另一个HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从ScriptDbConsole.html链接到legend.html时,我收到以下错误消息:


对不起,您请求的文件的确如此不存在。请检查
地址,然后再试一次。


这通常在正常环境下工作,但在这里我不会。它在script.google.com中。



在script.google.com项目中创建一个新的.html文件时,它将在与创建.html文件相同的位置创建该文件其他人,所以这段代码实际上应该正确工作?如何从ScriptDbConsole.html打开legend.html?

 < a href ='legend.html'target ='_ blank '>在新窗口中打开< / a> 


解决方案

虽然HtmlService允许您提供HTML,但它不是托管页面,而且您无法直接通过URL访问Apps脚本项目中的各种html文件。相反,您的Web应用将在发布时拥有一个网址,并且这是您拥有的唯一网址。



以下是您可以从脚本中提供单独页面的方法,并让它们的行为类似于html文件链接。


$ b $ < doGet()函数在被调用时传递一个事件,我们可以利用它来指出我们想要提供哪个页面。如果我们的Web应用程序ID是< SCRIPTURL> ,那么请求一个特定页面的URL和查询字符串如下所示:

  https://script.google.com/macros/s/<SCRIPTURL>/dev?page=my1 

使用模板化的HTML,我们可以即时生成必要的URL +查询字符串。在我们的 doGet()中,我们只需要解析查询字符串以确定要提供哪个页面。



这里是脚本,带有两个包含按钮的示例页面。



Code.gs



  / ** 
*获取作为WebApp运行的Google Apps脚本的URL。
* /
函数getScriptUrl(){
var url = ScriptApp.getService()。getUrl();
返回网址;
}

/ **
*获取主页或请求的页面。
*期望在查询字符串中有一个页面参数。
*
* @param {event} e传递给doGet的事件,包含查询字符串
* @returns {String / html}要服务的Html
* /
函数doGet(e){
Logger.log(Utilities.jsonStringify(e));
if(!e.parameter.page){
//当没有特定页面请求时,返回主页
返回HtmlService.createTemplateFromFile('my1')。evaluate();

//使用页面参数从脚本中选择html文件
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>


When linking from ScriptDbConsole.html to legend.html I get the following error message:

Sorry, the file you have requested does not exist. Please check the address and try again.

This would normally work in a normal environment, but not here I guess. It's in script.google.com.

When creating a new .html file in script.google.com project, it creates it at the same location as it did for the others, so this code should actually work right? How can I open legend.html from ScriptDbConsole.html?

<a href='legend.html' target='_blank'>Open in new window</a>

解决方案

While the HtmlService allows you to serve HTML, it is not "hosting" pages, and you cannot access the various html files in your Apps Script project by URL directly. Instead, your Web App will have a URL when it is published, and that is the only URL you have.

Here's a way that you can serve separate pages from your script, and have them behave similarly to html file links.

The doGet() function is passed an event when called, and we can take advantage of that to indicate which page we want served. If our Web App ID is <SCRIPTURL>, here is what a URL plus a querystring requesting a specific page will look like:

https://script.google.com/macros/s/<SCRIPTURL>/dev?page=my1

Using templated HTML, we can generate the necessary URL + querystring on the fly. In our doGet(), we just need to parse the querystring to determine which page to serve.

Here's the script, with two sample pages containing buttons to flip between them.

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>

这篇关于链接到Google Apps脚本中的另一个HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆