如何使用独立的Google Apps脚本浏览html页面? [英] How to navigate html pages with standalone Google Apps Script?

查看:31
本文介绍了如何使用独立的Google Apps脚本浏览html页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HTMLService创建独立的Google Apps脚本网络应用.我希望能够编写代码以单击应用程序HTML页面上的链接,然后转到链接中的页面.除了作为Google Web App之外,它与常规网页非常相似.普通的>点击我不起作用.

I am using the HTMLService to create a standalone google apps script web app. I want to be able to write code to take clicks on links on the HTML page for the app and go to the pages that are in the links. Much like a regular web page, except as a Google Web App. The normal ">Click me doesn't work.

这是我在做什么:

Code.gs
/*
 * Main function, sets up webapp ui.
 */ 
function doGet() {
    return HtmlService.createHtmlOutputFromFile('Index')
   .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

 function navigateTo(url) {
   return HtmlService.createHtmlOutputFromFile(url)
  .setSandboxMode(HtmlSerivce.SandboxMode.IFRAME);
}


<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
    function handleNavigation() {
      //Logger.log("Hello from handleNavigation, within Index.html");
      google.script.run.navigateTo('EditUsers')
      document.getElementById("test").innerHTML = "Success!";
    }
  </script>
  </head>
  <body>
    <h1>Home</h1>
    This is the beginning of the Project Status Web App<br/>
    <a href="EditUsers.html">Edit Users</a><br/><button            onclick="handleNavigation()">Button Time!</button>
    <a id="test" href="EditProjects.html">Edit Projects</a><br/>
    <a href="ViewProjectStatuses.html">View Project Statuses</a><br/>
  </body>
</html>

推荐答案

设置页面导航系统包含多个部分.如果页面"中的HTML已经存在,则您可能希望也可能不想从服务器获取所有新内容,然后再次注入该内容.如果有内容更改,则可能要从服务器检索内容,然后注入新内容.如果页面中的内容不需要更新,并且内容已经存在,那么您要做的就是显示页面,然后隐藏上一页.您可能希望页面导航的代码检查所有这些情况,并采取相应的措施.

Setting up a page navigation system has multiple parts. If the HTML in a "page" is already present, then you may or may not want to get all new content from the server and inject the content again. If there is content that changes, you may want to retrieve content from the server, and inject new content. If the content in the page doesn't need to be updated, and content is already present, then all you need to do is show the page, and hide the previous page. You may want the code for the page navigation to check for all of those situations, and act accordingly.

每次用户导航到新页面时,您可以从服务器获取相同的内容,在这种情况下,代码将更加简单.因此,不必每次都检查现有内容.

You can get the same content from the server every time that a user navigates to a new page, and in that case, the code would be much simpler. So, it's not necessary to check for existing content every time.

如果要显示的页面"中没有内容,则需要 withSuccessHandler()来从服务器接收新的HTML.

If no content is in the "page" that you want to show, you will need a withSuccessHandler() to receive the new HTML from the server.

现在您拥有:

google.script.run.navigateTo('EditUsers')

您需要:

google.script.run
  .withSuccessHandler(injectNewHtml)
  .navigateTo('EditUsers');

function injectNewHtml() {
  //Code here to inject HTML into the new page

  //Code to show page being navigated to

  //Code to hide all other pages

};

如果您要检查现有内容,则可以使用以下内容:

If you want to check for existing content, you could use something like this:

var numberOfChildNodes = document
              .getElementById(elementOfPage).childNodes.length;

if (numberOfChildNodes === 0) {
  google.script.run
    .withSuccessHandler(injectNewHtml)
    .navigateTo('EditUsers');
};

这篇关于如何使用独立的Google Apps脚本浏览html页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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