Return Output from Function Call in doGet()

Here is a little useful tip I was struggling to understand for a while so just worked around it. If you have a google apps script / web app and you want to call a function from inside the doGet() to return content or html, you may not always get what you want!

This is the script url with parameters:

https://script.google.com/macros/s/AKfycbxl3Ns1y0LP_DyXkkRMYKYGe_tOGPHNWX-3GtWEGrlcP6HhrIxu/exec?a=hello&b=world


You could just do this:

function doGet(e) {

return HtmlService.createHtmlOutput("<html><body><h1>Content returned from script</h1><p>" + e.parameter.a + " " + e.parameter.b + "</p></body></html>");

}

which will work. but very often your scripting will be much longer, and to keep the doGet() tidy you may want to have a separate function to call in the doGet()

For example, if you do this:

function doGet(e) {

doreturn(e.parameter.a,e.parameter.b);

}


function doreturn(a,b) {

return HtmlService.createHtmlOutput("<html><body><h1>Content returned from script</h1><p>" + a + " " + b + "</p></body></html>");

}

you will get a Google Apps Script page with this:

The script completed but did not return anything.

However, if you do this:

function doGet(e) {

return doreturn(e.parameter.a,e.parameter.b);

}


function doreturn(a,b) {

return HtmlService.createHtmlOutput("<html><body><h1>Content returned from script</h1><p>" + a + " " + b + "</p></body></html>");

}

then the web page WILL return your html output:

If you can't see the difference in the script, I added a "return" to the function call in doGet()