0

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]

2 Answers2

2

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]);
TheMaster
  • 45,448
  • 6
  • 62
  • 85
0

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]);
Cooper
  • 59,616
  • 6
  • 23
  • 54