从对象解析Google表格中的数据

问题描述 投票:0回答:1

我在Google表格文件的某列中有成千上万的数据行,看起来像是

[{“” amountMax“:49.99,” amountMin“:49.99,” availability“:” true“,” color“:” Brown“,” currency“:” USD“,” dateSeen“:[” 2019-04- 11T08:00:00Z“],” isSale“:” false“,” offer“:”仅在线“,” sourceURLs“:[” https://www.walmart.com/ip/SadoTech-Model-CXR-Wireless-Doorbell-1-Remote-Button-2-Plugin-Receivers-Operating-500-feet-Range-50-Chimes-Batteries-Required-Receivers-Beige-Fixed-C/463989633“]}]]

我希望能够返回最大值,货币,颜色属性。如何在Google表格中执行此操作。理想情况下,我希望像在此链接中https://repl.it/@alexhoy/WetSlateblueDribbleware

一样,能够像我通常在javascript中那样检索数据属性

但是在script.google.com中创建函数时,这似乎对我不起作用

有什么建议吗?

javascript google-sheets text-parsing
1个回答
0
投票

以下脚本将为您提供有关如何完成此任务的想法。

它假定:

  • 您的问题中描述的json数据位于单元格A2中。
    • json以方括号开头和结尾。
    • 这些已被删除,因此可以使用常规的JSON.parse命令。
  • 最大值将插入到单元格D2中
  • 货币将插入到单元格E2中
  • 颜色将插入到单元格F2中

脚本使用临时数组捕获值,然后将其分配给2d数组。

如果您有很多行数据,那么您将需要创建一个循环。我建议您逐步构建arraydata,并仅在循环结束时更新目标范围。这将为您提供最有效的结果。


function so6031098602() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet()

  // get the json data
  var json = sheet.getRange("A2").getValue();

  // remove the opening and closing square brackets
  var json = json.substring(1);
  var json = json.substring(0, json.length - 1);

  // parse the json  
  var content = JSON.parse(json); 

  // temp arrar to capture the data
  var temparray = [];
  temparray.push(content["amountMax"]);
  temparray.push(content["currency"]);
  temparray.push(content["color"]);


  // second array to accept the row data
  var arraydata =[];
  arraydata.push(temparray)

  // define the target range
  var targetrange = sheet.getRange(2, 4, 1, 3);
  // update with the arraydata
  targetrange.setValues(arraydata);

}
© www.soinside.com 2019 - 2024. All rights reserved.