Google apps script 使用个人域通过web应用传递参数

Google apps script 使用个人域通过web应用传递参数,google-apps-script,Google Apps Script,我正在尝试创建一个web应用程序,它使用iFrame在个人域上提供服务,并传递各种参数 gs文件: function doGet() { return HtmlService.createTemplateFromFile('test.html') .evaluate() .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL); } html文件: <!DOCTYPE html> <htm

我正在尝试创建一个web应用程序,它使用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>

你好,世界!
google.script.url.getLocation(函数(位置){
var参数=location.parameters;
console.log(parameters.id);
document.getElementById('result').innerHTML=parameters.id;
});
使用webapp URL时,输出为:

你好,世界 1234

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

你好,世界 未定义


救命啊!这让我非常恼火。

您可以在域html的脚本标记中使用以下客户端代码从域搜索字符串获取并更改搜索字符串参数,而不是应用程序脚本web app 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>

var loc=window.location.search//获取域的搜索字符串
loc=loc.切片(1);
console.log('loc:'+loc)
var-params,theID;
参数=位置替换(“&”,“,”);
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');
log('myFrame:'+myFrame)
var frameSrc=myFrame.src.toString();
log('frameSrc:'+frameSrc)
log('typeof frameSrc:'+typeof frameSrc)
console.log('theId:'+theId)
var newSrc=frameSrc+“?id=“+theId;
console.log('newSrc:'+newSrc)
myFrame.src=newSrc//设置新的src,iframe将刷新

iframe标记中的
src
属性必须包含搜索字符串参数<代码>您尚未显示iframe标记。如果您想将搜索字符串放入站点的URL,而不是web应用程序发布的URL,则需要使用“onload”功能获取搜索字符串,更改iframe中的
src
,然后重新加载iframe。非常感谢。我自己永远也不会得到这个。