简体   繁体   中英

POSTing a array from android to Google apps script

I am trying to send an array to google script to put into google sheets.

What I have for the google script:

  function insert(e, sheet) {
 
  //var scannedData = e.parameter.sOrder;
  var scannedData = JSON.parse(e.parameter.sOrder);
  var orderLocation = e.parameter.sLocation;
  var d = new Date();
  var ctime =  d.toLocaleString();
  
  sheet.appendRow([scannedData, orderLocation, ctime]);
  
  return ContentService
  .createTextOutput("Success")
  .setMimeType(ContentService.MimeType.JAVASCRIPT);  

  
  
}

the results it gives me is:

[Ljava.lang.Object;@5c0b25d1    Shipping    25/07/2020, 22:32:21

what it should give me is:

0152502243  Shipping    24/07/2020, 18:20:37

my code on my apps side:

 postDataArray = new JSONArray(Arrays.asList(finalData));
            postDataParams.put("sOrder", postDataArray);

            postDataParams.put("sLocation",orderLocation);
            postDataParams.put("sheetName",sheetName);


            Log.e("params",postDataParams.toString());

finalData is a String[] that consists of 2 entries. "Location" "Data"

if i send finalData[0] as a control then it picks up the first entry, but it gives me this error instead:

[Ljava.lang.Object;@5c0b25d1

The google script needs to take either an array straight or convert a string into an array, and I am stuck on this conversion.

so google script must take the array

finalData = {"Location","Data"}

and convert it into:

[Location] 
[Data]

When sending and receiving structured data, it is preferable to send and receive as json.

Sheet#appendRow accepts a single argument of type array. This array should not be a nested array. Try

sheet.appendRow(scannedData);

or

sheet.appendRow([...scannedData, orderLocation, ctime]);

or

sheet.appendRow(scannedData.concat([orderLocation, ctime]);

Assuming a doPost(e)

doPost(e) {
  ...
  var scannedData = e.parameter.sOrder;
  var arr ="{"+ scannedData+"}";
  var orderLocation = e.parameter.sLocation;
  var d = new Date();
  var ctime =  d.toLocaleString();
  var ss=SpreadsheetApp.openById('ssid')
  var sheet=ss.getActiveSheet();
  sheet.appendRow([arr,ctime]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM