使用个人域通过Web应用程序传递参数

时间:2018-01-06 13:24:27

标签: google-apps-script

我正在尝试创建一个使用iFrame在个人网域上投放的网络应用,并传递各种参数。

gs文件:

function doGet() {
return HtmlService.createTemplateFromFile('test.html')
     .evaluate() 
     .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

html文件:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Hello world!
    <div id="result"></div>
  </body>
</html>

<script>

google.script.url.getLocation(function(location) {
var parameters = location.parameters;
console.log(parameters.id);
document.getElementById('result').innerHTML = parameters.id;
});

</script>

使用webapp URL时输出为:

Hello World 1234

使用个人域URL时,输出为:

Hello World 未定义

https://script.google.com/a/formapprovals.com/macros/s/AKfycbw3RWnpvnpqKUa1DL2iYzDUxFSBbdP9sW5llymWlgiqLlU8Ykza/exec?id=1234

http://example.com?id=1234

帮助!这让我起了墙。

1 个答案:

答案 0 :(得分:1)

您可以在域html的脚本标记中使用以下客户端代码从域搜索字符串中获取和更改搜索字符串参数,而不是应用程序脚本Web应用程序html。

<iframe id="myIframe"
 src="https://script.google.com/a/macros/s/Published_Web_App_ID/exec">
</iframe>

<script>
  var loc = window.location.search;//Get the search string of the domain
  loc = loc.slice(1);
  console.log('loc: ' + loc)

  var params,theID;
  params = loc.replace("&",",");
  params = params.replace("=",'":"');
  params = '{"' + params + '"}';
  console.log('params: ' + params)

  params = JSON.parse(params);

  console.log('params: ' + params)

  theId = params.id.toString();
  console.log('theId: ' + theId)

  var myFrame = document.getElementById('myIframe');
  console.log('myFrame: ' + myFrame)

  var frameSrc = myFrame.src.toString();
  console.log('frameSrc: ' + frameSrc)
  console.log('typeof frameSrc: ' + typeof frameSrc)

  console.log('theId: ' + theId)
  var newSrc = frameSrc + "?id=" + theId;
  console.log('newSrc: ' + newSrc)

  myFrame.src = newSrc;//Set the new src and the iframe will refresh
</script>