Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a URL string parameter passed to Google Apps Script doGet(e)

Given this URL:

https://script.google.com/macros/s/MacroName/dev?theArg="69.28.15.332"

The important part is on the end:

?theArg="69.28.15.332"

I'm trying to pass information to an Apps Script in the URL. Why won't my .gs Google Apps Script function get the value of the string at the end of the URL? This is the doGet(e) function.

function doGet(e){
  var passedInIP = e.parameter.theArg;
  Logger.log(passedInIP);

  if (passedInIP === "69.28.15.332") 
  {
    return HtmlService.createHtmlOutput("<h2>something</h2>")
  }
};

I get this error msg printed in the browser:

The script completed but did not return anything

The Logger.log does log something. It logs [Date Time EDT] "69.28.15.332" and the value is exactly the same in the log, as the value I'm checking for. But the equality test fails.

like image 765
Alan Wells Avatar asked Mar 10 '14 02:03

Alan Wells


People also ask

What is doGet E?

Requirements for web apps It contains a doGet(e) or doPost(e) function. The function returns an HTML service HtmlOutput object or a Content service TextOutput object.

How do I handle GET and POST HTTP requests in Google Apps Script?

Handle POST Requests with Google ScriptsThe callback function doPost is invoked when an HTTP POST request is make to your Google Script URL that is published as a web app with anonymous access. const doPost = (request) => { console. log(request); return ContentService. crateTextOutput(JSON.

How do I trigger a Google script submit?

Here, the event is the submission of the Google Form, the effect is the function call ( onFormSubmit ). After creating the function, your script editor should look like this. Click on the stopwatch button to open the Apps Script Dashboard & create a trigger. Click on the stopwatch icon to create a trigger.

How do I view Google App script logs?

Use the Apps Script execution log To view these logs, at the top of the editor, click Execution log. When you run a function or use the debugger, the logs stream in real time. You can use either the Logger or console logging services in the built-in execution log.


1 Answers

The argument is passed "as it is", you can test it using a code like below :

function doGet(e){
  var passedInIP = e.parameter.theArg;
  Logger.log(passedInIP);

  if (passedInIP == "69.28.15.332") 
  {
    return HtmlService.createHtmlOutput("<h2>something</h2>")
  }
    return HtmlService.createHtmlOutput(passedInIP)
}; 

This will return "69.28.15.332" including quotes...

So you have 2 possibilities to choose from :

  1. remove the quotes in your url
  2. add quotes in your condition like this '"69.28.15.332"' (single quotes+quotes)

I would choose the first one but for no good reason ;-)

like image 153
Serge insas Avatar answered Sep 24 '22 08:09

Serge insas