Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 896
DrewCo Avatar asked Jan 13 '12 19:01

DrewCo


2 Answers

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

like image 96
java4africa Avatar answered Sep 24 '22 11:09

java4africa


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] }
like image 30
TobyRush Avatar answered Sep 21 '22 11:09

TobyRush