链接到Google Apps脚本中的其他HTML页面

时间:2013-03-27 19:50:04

标签: html google-apps-script web-applications

从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>

3 个答案:

答案 0 :(得分:42)

虽然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()中,我们只需要解析查询字符串以确定要投放的页面。

这是脚本,有两个样本页面,包含可在它们之间翻转的按钮。

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>

答案 1 :(得分:0)

Google Apps脚本网络应用程序主要设计用于单页网络应用程序。但是它也可以用作多页应用程序(不推荐)。这是一个示例网络应用程序:

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>

参考文献:

答案 2 :(得分:0)

显示如何返回多个页面的其他答案非常好。另一种选择(取决于您所做工作的复杂程度)是使用 CSS 来根据点击或选择的内容显示和隐藏单个网页的部分内容。

示例显示和隐藏选择

<!DOCTYPE html>
<html>

<head>
    <base target="_top">
  <style>
    .show2 {
      display: none;
    }
  </style>
</head>

<body>
  <h1>Show by Selection Example</h1>
    <div>
        <p>Select an option.</p>
        <select id="idSelect" name="selection">
        <option value="idShow1">Show 1</option>
        <option value="idShow2">Show 2</option>
    </select>
    </div>
  <div id="idShow1" class="show1">
    <h2>Default Div</h2>
    <p>Showing 1</p>
  </div>
  <div id="idShow2" class="show2">
    <h2>Other Div</h2>
    <p>Showing 2</p>
  </div>
</body>

<script>
  var mySelect = document.getElementById("idSelect");
  mySelect.onchange = function() {
    selected = this.value;
    document.getElementById("idShow1").style.display = "none";
    document.getElementById("idShow2").style.display = "none";
    document.getElementById(selected).style.display = "block";
  }
</script>
</html>