简体   繁体   English

在链接到Google Apps脚本中的另一个HTML页面时传递参数

[英]Passing parameters while Linking to another html page in google apps script

First, this is a google-app-script issue... I can't seem to capture the second (or subsequent) parameters within the HTML page (ie "item" in this example)... I've seen many examples using "location.search" and "window.location.search", but none of these seem to work. 首先,这是一个google-app-script问题...我似乎无法捕获HTML页面中的第二个(或后续)参数(在此示例中为“ item”)...我已经看到很多示例使用“ location.search”和“ window.location.search”,但这些似乎都不起作用。 Could it possibly be as simple as "location.search" is not the correct usage? 可能会因为“ location.search”这么简单而不是正确的用法?

Example

Code.gs 代码

var myParam;
/**
 * 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) );
  Logger.log(e.parameter.page);
  var pgToLoad = e.parameter.page;

  if (!e.parameter.page) {
    Logger.log('!e.parameter.page')
    // When no specific page requested, return "home page"
    // return HtmlService.createTemplateFromFile('my1').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
    return HtmlService.createTemplateFromFile('my1').evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
  }
  Logger.log('there is something for the page');
  // else, use page parameter to pick an html file from the script
  // return HtmlService.createTemplateFromFile(pgToLoad).evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
  return HtmlService.createTemplateFromFile(pgToLoad).evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

I have multiple HTML files, but they are basically the same as my1.html below... my1.html 我有多个HTML文件,但它们与下面的my1.html基本相同... my1.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1>Source = my1.html</h1>
    <p id=myParam>Placeholder</p>
    <?var url = getScriptUrl();?><a href='<?=url?>?page=my2&item=1-234'> <input type='button' name='button' value='my2.html'></a>
    <?var url = getScriptUrl();?><a href='<?=url?>?page=my3&item=1-345'> <input type='button' name='button' value='my3.html'></a>
  </body>
</html>
<script>
function getParam(sname)
{
  var params = location.search;
  var sval = "";
  params = params.split("&");
  // split param and value into individual pieces
  for (var i=0; i<params.length; i++)
  {
    temp = params[i].split("=");
    if ( temp[0] == sname ) { sval = temp[1]; }
  }
  return sval;
}
function changeItem() {
  var param = getParam("item");
  var myItem = "Item:-"+param+"-";
  document.getElementById("myParam").innerHTML = myItem;
}
window.onload = changeItem;
</script>

I think I know what you want to do. 我想我知道你想做什么。 It looks like you are getting the search string parameters from the doGet(e) function on the server side, then you are trying to get the same search string parameters again on the "client side" from the onload function? 看来您是从服务器端的doGet(e)函数获取搜索字符串参数,然后又试图从onload函数的“客户端”获取相同的搜索字符串参数? If this is the case, I would abandon trying to get the search string parameters from the client side. 如果是这种情况,我会放弃尝试从客户端获取搜索字符串参数。

You could store the search string parameters in the browsers sessionStorage : 您可以将搜索字符串参数存储在浏览器的sessionStorage

window.sessionStorage.setItem("searchStringOne","Value One");

and to retrieve: 并检索:

var valueOne = window.sessionStorage.getItem("searchStringOne");

Session Storage Information 会话存储信息

Here's an example to show how to get the query string parameters to serve different html templates using html-service of app script : 这是一个示例,展示了如何使用应用脚本的html-service获取查询字符串参数以服务于不同的html模板:

function doGet(e) {
   Logger.log( Utilities.jsonStringify(e) );

  // Load a home page when no parameter is specified

  if (!e.parameter.redirect) {
  var template = HtmlService.createTemplateFromFile('home');

  var htmlOutput =    template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setTitle('Home');
  return htmlOutput;
  }

  //get the page from parameter and load it  

 else{
 var template=HtmlService.createTemplateFromFile(e.parameter['redirect']);

 var htmlOutput = template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME).setTitle('Other Page');

 return htmlOutput;
 }

}

function getScriptUrl() {

  var url = ScriptApp.getService().getUrl();

  return url;
}

then HTML will look like this : 那么HTML将如下所示:

home.html home.html

        <?var url = getScriptUrl();?>

        You are on Home Page.

        <a href='<?=url?>?redirect=pagex'>Goto PageX</a>



        <a href='<?=url?>?redirect=pagey'>Goto PageY</a>

pagex.html pagex.html

You are on PageX

pagey.html pagey.html

You are on PageY 

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM