How to use, "e.parameters" as a array in doGet

2,106 views
Skip to first unread message

Carlos Bello

unread,
Mar 22, 2020, 7:00:13 PM3/22/20
to Google Apps Script Community
Hi. I am trying to send three data through the web and then insert it into a spreadsheet. The insert part in the spreadsheet works fine. The problem is that the Do Get function, when accessed from the web browser, does not detect the 3 values ​​as values ​​of an array but as a single meaningless string.
When I use the debug everything works great and the three values ​​are written correctly.
the code is the following.


// This method will be called first or hits first  
function doGet(e){
  Logger.log("--- doGet ---");
 
 var tag = "", value = "",Temperatura="",Humedad="",Presion="";
 
  try {
 
    // this helps during debuggin
    if (e == null){e={}; e.parameters = {tag:"test",value:[24.2,69.3,970.5]};}
 
    tag = e.parameters.tag;
    value = e.parameters.value;
    Logger.log(e);
    Temperatura=e.parameters.value[0];
    Humedad=e.parameters.value[1];
    Presion=e.parameters.value[2];
    
    // save the data to spreadsheet
    save_data(Temperatura,Humedad,Presion);

    return ContentService.createTextOutput("Wrote:\n  tag: " + tag + "\n  value: " + value);
 
  } catch(error) { 
    Logger.log(error);    
    return ContentService.createTextOutput("upsi...." + error.message
                                            + "\n" + new Date() 
                                            + "\nvalor " + value);
  }  
}
 
// Method to save given data to a sheet
function save_data(A , B , C){
  Logger.log("--- save_data ---"); 

  try {..
....

Alan Wells

unread,
Mar 22, 2020, 7:39:19 PM3/22/20
to Google Apps Script Community
If there are commas in the string then you can convert the string to an array.

value = e.parameters.value;
value = value.split(",");//Convert comma separated string to an array

Temperatura = values[0];

Carlos Bello

unread,
Mar 22, 2020, 7:59:14 PM3/22/20
to Google Apps Script Community
Thanks Alan.
The string does have commas. This code is executed in Google app Script and returns the result ... "value.split is not a function". Does this mean that Google app Script has a reduced instruction set?

[20-03-22 20:57:31:704 ART] --- doGet ---
[20-03-22 20:57:31:707 ART] {parameters={value=[24.2, 69.3, 970.5], tag=test}}
[20-03-22 20:57:31:711 ART] TypeError: value.split is not a function

...
    tag = e.parameters.tag;
    value = e.parameters.value;
    Logger.log(e);
    value=value.split(",");
    Temperatura=e.parameters.value[0];
    Humedad=e.parameters.value[1];
    Presion=e.parameters.value[2];
    
    // save the data to spreadsheet
...

Alan Wells

unread,
Mar 22, 2020, 8:13:55 PM3/22/20
to Google Apps Script Community
check the data type of value

Logger.log("typeof value: " + typeof value)

It might not be a string.
If it's not a string, then you'll need to do something else to it first.

you could try:
value = value.toString();
//Then convert to array

Carlos Bello

unread,
Mar 23, 2020, 9:59:51 AM3/23/20
to Google Apps Script Community
Alan, it worked.
I copy the final code below.
Thank You.

function doGet(e){
  Logger.log("--- doGet ---");
 
  var tag = "", value = "",Temperatura="",Humedad="",Presion="";
  var k,subCadena;
 
  try {
 
    // this helps during debuggin
    if (e == null){e={}; e.parameters = {tag:"test",value:["11.1","99.1","770.7"]};}
 
    tag = e.parameters.tag;
    value = e.parameters.value;
    value = value.toString();
 
    k=value.indexOf(",");
    Temperatura=value.substr(0,k);
    subCadena=value.substr(k+1);
    k=value.indexOf(",");
    Humedad=subCadena.substr(0,k);
    Presion=subCadena.substr(k+1);

    // save the data to spreadsheet
    save_data(Temperatura,Humedad,Presion);
    return ContentService.createTextOutput("Wrote:\n  tag: " + tag + "\n  value: " + value);
  } catch(error) { 
    Logger.log(error);    
    return ContentService.createTextOutput("upsi...
...

Alan Wells

unread,
Mar 23, 2020, 10:07:33 AM3/23/20
to Google Apps Script Community
 Glad you got it working.
Reply all
Reply to author
Forward
0 new messages