将一个数组从安卓端POST到Google Apps Script。

huangapple go评论90阅读模式
英文:

POSTing a array from android to Google apps script

问题

以下是翻译好的内容:

我正在尝试将一个数组发送到 Google 脚本,以便放入 Google 表格中。

我为 Google 脚本所拥有的内容:

function insert(e, sheet) {
  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);
}

它给我返回的结果是:

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

而实际上它应该给我返回:

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

我应用端的代码:

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

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

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

其中,finalData 是一个包含 2 个条目的 String[]

如果我将 finalData[0] 作为控制参数发送,那么它可以获取到第一个条目,但是它却返回以下错误信息:

[Ljava.lang.Object;@5c0b25d1

Google 脚本需要接受一个数组或将一个字符串转换为数组,并且我在这个转换过程中遇到了问题。

因此,Google 脚本必须接受数组:

finalData = ["Location", "Data"];

并将其转换为:

[Location]
[Data]
英文:

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]

答案1

得分: 2

当发送和接收结构化数据时,最好将其作为 JSON 进行发送接收

Sheet#appendRow 接受一个类型为数组的单一参数。该数组不应为嵌套数组。尝试使用以下方法:

sheet.appendRow(scannedData);

或者

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

或者

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

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]);

答案2

得分: 0

假设有一个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]);
}
英文:

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]);

huangapple
  • 本文由 发表于 2020年7月25日 06:15:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63082131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定