DrewCo
DrewCo

Reputation: 954

How to pass parameters to Google Apps Script Gadget embedded in a Sites Page?

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.

Upvotes: 17

Views: 15858

Answers (3)

TobyRush
TobyRush

Reputation: 756

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] }

Upvotes: 1

Jason K.
Jason K.

Reputation: 417

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.

Upvotes: 0

java4africa
java4africa

Reputation: 212

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

Upvotes: 2

Related Questions