0
votes

I just create a doGet function in my web app,

function doPost(e){
  var userName = e.parameter.userName;

  return ContentService.createTextOutput(userName);

}

and I published it to the public, anyone could execute it the url is "https://script.google.com/a/slt.org.au/macros/s/AKfycby2pFGHc3qWaxnD4WGTLMEAPMUocohzH_-OsUPxwqi8kmWfRZs8/exec"

and I add "?userName=Leon" at the end

I type the url "https://script.google.com/a/slt.org.au/macros/s/AKfycby2pFGHc3qWaxnD4WGTLMEAPMUocohzH_-OsUPxwqi8kmWfRZs8/exec?userName=Leon" at the end, and I only want it to just return my username, but it returns so many things "{"parameter":{"userName":"Leon"},"contextPath":"","contentLength":-1,"queryString":"userName=Leon","parameters":{"userName":["Leon"]}}"

I tried both parameter and parameters, doPost(e) and doGet(e), all did not work.. Could someone to help me that why these cannot work?

2

2 Answers

2
votes

Change doPost -> doGet

function doGet(e){
  var userName = e.parameter.userName;
  return ContentService.createTextOutput(userName);
}

GOAL

0
votes
  1. Here's a simple example of getting querystring from a doGet:
  2. Don't forget to deploy as a webapp.
  3. I keep the latest parameters in a sheet name Params.

Codes.gs

function getParams() 
{ 
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Params');
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var s='<table>';
  for(var i=0;i<vA.length;i++)
  {
     s+=Utilities.formatString('<tr><td>%s</td><td>%s</td></tr>',vA[i][0],vA[i][1]);
  }
  s+='</table>';
  sh.clearContents();
  return s;
}

function showTheThisDialog()
{
  var ui=HtmlService.createHtmlOutputFromFile('thethisfunc');
  SpreadsheetApp.getUi().showModelessDialog(ui, 'The This Func');
}

function doGet(e)
{
  var sh=SpreadsheetApp.getActive().getSheetByName('Params');
  var ui=HtmlService.createHtmlOutputFromFile('thethisfunc');
  var data=e.parameter;
  //Logger.log(e.parameter);
  for(key in data)
  {
    var A=[key + ' is a ',data[key]];
    sh.appendRow(A);
  }
  return ui.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

thethisfunc.html

<!DOCTYPE html>
<html>
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
    $(function() {
        google.script.run
          .withSuccessHandler(dispParams)
          .getParams();
      });
    function dispParams(hl)
    {
      console.log(hl);
      document.getElementById('mybox').innerHTML=hl;
    }
    console.log('My Code');
    </script>
  </head>
  <body>
    <div id="mybox"></div>
  </body>
</html>

Here's my command line:

https://script.google.com/macros/s/ID/exec?President=Donald&Donald=Duck