如何将参数传递到嵌入在网站页面中的 Google Apps 脚本小工具?

发布于 2024-12-27 04:36:36 字数 547 浏览 0 评论 0原文

我有一个嵌入 Google 协作平台页面中的 Google Apps 脚本小工具。我想向小工具传递一个页面参数,这样当使用如下 URL 打开页面时:

https://sites.google.com/a/mydomain/mysite/mypage?myparameter=1

我可以在 Apps 脚本小工具代码中访问页面参数的值,如下所示:

function doGet(e) {
  var app = UiApp.createApplication();
  app.add(app.loadComponent("MyComponent"));
  var myparam = e.parameter.myparameter;    
  return app;    
}

目前,e 的值。 parameter.myparameter 即将返回为空。有没有办法设置我的 Apps 脚本来支持此功能?欢迎任何方法。

I have a Google Apps Script gadget that is embedded in a Google Sites page. I would like to pass the gadget a page parameter, such that when the page is opened with URL like:

https://sites.google.com/a/mydomain/mysite/mypage?myparameter=1

I can access the value of the page parameter in the Apps Script gadget code like so:

function doGet(e) {
  var app = UiApp.createApplication();
  app.add(app.loadComponent("MyComponent"));
  var myparam = e.parameter.myparameter;    
  return app;    
}

Currently, the value of e.parameter.myparameter is coming back as null. Is there a way to setup my Apps Script to support this? Any approaches are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不爱素颜 2025-01-03 04:36:36

也许下面的链接会对您有所帮助 - 我自己还没有尝试过......但我会在接下来的几天内尝试一下。
http://code.google.com/p /google-apps-script-issues/issues/detail?id=535

Maybe the link bellow will help you - I have not tried it myself yet...but I will try it out in the next days.
http://code.google.com/p/google-apps-script-issues/issues/detail?id=535

梦行七里 2025-01-03 04:36:36

我将其发布在已接受答案中链接的 code.google.com 页面上,但将参数传递到 Apps 脚本的方法是添加“https://sites.google.com/feeds" 到 Apps 脚本范围。 (有关如何添加显式范围的信息,请参阅此网站。)添加此范围后,将进行以下操作:

in Code.gs:

function doGet(e) {

  var htmlTemplate = HtmlService.createTemplateFromFile("page");
  htmlTemplate.urlParams = e.parameters;
  return htmlTemplate.evaluate();

}

in page.html:

...
<head>
...
<script>urlParams = <?!= JSON.stringify(urlParams) ?>;</script>
</head>
...

urlParams 现在可用作 JS 代码中的变量,格式如下:

urlParams = { key1: [value1, value2, ...], key2: [value1, value2] }

I posted this on the code.google.com page linked in the accepted answer, but the way to have parameters passed through to Apps Script is by adding "https://sites.google.com/feeds" to the Apps Script scope. (See this site for information about how to add explicit scopes.) Once this scope is added, the following works:

in Code.gs:

function doGet(e) {

  var htmlTemplate = HtmlService.createTemplateFromFile("page");
  htmlTemplate.urlParams = e.parameters;
  return htmlTemplate.evaluate();

}

in page.html:

...
<head>
...
<script>urlParams = <?!= JSON.stringify(urlParams) ?>;</script>
</head>
...

urlParams is now available as a variable in your JS code in the following form:

urlParams = { key1: [value1, value2, ...], key2: [value1, value2] }
蓝海 2025-01-03 04:36:36

此示例将参数描述为“&name=value”,但我无法做到让它在 Google Apps 网站或个人 Google 网站中运行。 (似乎以不同的方式处理身份验证)

当我对值进行硬编码时,该示例似乎工作正常,所以也许我只是没有正确解析它或其他什么,当我更好地理解这一点时,我会尝试在这里跟进,但我也尚未找到对这些功能的充分解释。

一个假设是谷歌改变了一些东西,我注意到菜单结构似乎与我假设的不符,因为我看到很多对[分享]按钮/菜单的引用。

This example describes a parameter as "&name=value" however I have not been able to get it working in either a Google Apps Site, or in a Personal Google Site. (which seem to handle authentication in different ways)

The example seems to work fine when I hard-code the value so maybe I am just not parsing it right or something, I will try to follow up here when I understand this better but I also have not yet found adequate explanations of these features.

One Hypothesis is that Google changed something, I note the menu structure does not seem to match what I assume it use to be since I see many references to a [share] button/menu.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文