Question

It's a simple question but the answer seems to elude me.

I am trying to pass a parameter to a script in the following way: https://script.google.com/macros/s/AKfycbzygHQ2qQ.../exec?id=1234&value=Hello

The script is a simple one:

function doGet(e) {
  Logger.log('in doGet function');
  for (var i in e.parameter) {
    Logger.log('parameter ' + i+1 + ' is ' + e.parameter[i]);
  } 
}

and getting the following in the execution transcript:

[13-10-01 10:21:20:722 ICT] Starting execution
[13-10-01 10:21:20:734 ICT] Logger.log([parameter is undefined, []]) [0 seconds]
[13-10-01 10:21:20:735 ICT] Execution succeeded [0.0 seconds total runtime]

What did I get wrong here?

Thanks!

Was it helpful?

Solution

simply use e.parameter:

function doGet(e) {
  var app = UiApp.createApplication();

  var label = app.createLabel('Parameter value a:'+e.parameter.a+' Parameter value b:'+e.parameter.b);
  app.add(label);

  return app;
}

publish it,

get the URL, you should obtain something like:

https://script.google.com/macros/s/AKfycbzvmy136r2B1RcIYd7QUmv9-1BBqD9WIwpt3NX9b3Wc3yZJazOj/exec

add "?a=Something&b=somethingElse" to the URL:

https://script.google.com/macros/s/AKfycbzvmy136r2B1RcIYd7QUmv9-1BBqD9WIwpt3NX9b3Wc3yZJazOj/exec?a=Something&b=somethingElse 

and reload the page, you should see:

Parameter value a:Something -- Parameter value b:somethingElse

on thw browser page.

OTHER TIPS

Use the following modified code to get what you can expect in e. You can check by running this code using a service URL with a URL parameter like val=123

function doGet(e) {
  Logger.log('in doGet function');

  Logger.log(JSON.stringify(e));

}

After running this from the service URL, I got

[13-10-01 12:28:22:408 IST] in doGet function
[13-10-01 12:28:22:409 IST] {"queryString":"val=123","parameter":{"val":"123"},"contextPath":"","parameters":{"val":["123"]},"contentLength":-1}

Now you can use your JavaScript skills to get the required content from this JSON.

Wager Ahmad got me on the RIGHT track here, I had the same problem and trying for 2 days. Some knowledge I lack in JavaScript and in Google Apps Script came together.

Try this, should work and eventually so "easy" :)

   function doGet(e) 
   {
   var c =  arguments[0].queryString
   Logger.log(c);
   }

Thanks a lot Waqar! :)

Saido

Not sure what went on but it appear but the key provided in the URL in the Deploy App window was different than the one once you click the "Test web app for your latest code" (that is besides the dev vs. exec). Once I copied the link displayed in the browser rather than the one provided in the deploy app window then it worked. However, I am still not sure whether there is something wrong with my steps or is it with Google. Anyone can shed a light on this? I thought it should be identical!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top