【问题标题】:Parsing Data in Google Sheets From an Object从对象解析 Google 表格中的数据
【发布时间】:2020-02-19 23:48:31
【问题描述】:

我在 Google 表格文件的一列中有数千行数据,看起来像 [{"amountMax":49.99,"amountMin":49.99,"availability":"true","color":"Brown","currency":"USD","dateSeen":["2019-04-11T08:00:00Z"],"isSale":"false","offer":"Online only","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 表格中做到这一点。理想情况下,我想做一些事情,比如能够像我通常在 javascript 中那样检索数据属性,就像在这个链接中一样https://repl.it/@alexhoy/WetSlateblueDribbleware

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

例如,这里有一个 slugify 函数,它接受一个输入(单元格)并将其转换为一个 slug/句柄,而无需循环。在 Google 表格中,我可以调用 =slugify(b2) 并将该值转换为 slug 形式

/**
* Converts value to slug
* @customfunction
*/
function slugify(value) {
  /*
   * Convert the the vs in a range of cells into slugs.
   * @customfunction
   */
  let slug = '';

  slug = value.substring(0, 100).toLowerCase();
  slug = slug.replace(/[^\w\s-]/g, '');
  slug = slug.replace(/\s+/g, '-');
  Logger.log(slug);

  return slug;
}

我想做同样的事情,而不是循环解析上面的对象数据或声明一个值范围等等。

关于如何以如上所示的简单方式执行此操作的任何建议,无需声明活动电子表格、范围值和循环。

【问题讨论】:

  • 但是,在 script.google.com 中创建函数时,这似乎对我不起作用您应该在问题中显示该脚本函数

标签: javascript google-apps-script google-sheets text-parsing


【解决方案1】:

以下脚本将让您了解如何处理此任务。

假设:

  • 您问题中描述的 json 数据位于单元格 A2 中。
  • 最大值将被插入到单元格 D2 中
  • 货币将插入单元格 E2
  • 颜色将插入到单元格 F2 中

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

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


function so6031098604() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet()
  var content = JSON.parse(sheet.getRange("A2").getValue());

  // temp arrar to capture the data
  var temparray = [];
  temparray.push(content[0]["amountMax"]);
  temparray.push(content[0]["currency"]);
  temparray.push(content[0]["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);

}

【讨论】:

  • @TheMaster 注意到。我非常专注于 JSON,以至于我什至都没有想到。答案已修改。
  • 我希望有一个简单的解决方案,对于这么简单的任务来说似乎过于复杂了!
  • 答案已更新以删除“不必要的代码”。这很简单。我同意如果只有一行数据可能会更简单,但是你有“数千行”,并且需要创建一个循环。那么您认为哪个答案过于复杂?
  • 感谢您的回答,我想我正在寻找不涉及循环的东西。有点像一个内置的 excel 函数,你可以写成 =getAmountMax(A2) 然后向下拖动到适当的单元格
【解决方案2】:

您需要一个自定义函数,该函数将返回 JSON 数组中的某些字段。

在以下示例中,目标单元格可以是单个单元格或数组。

此示例不使用数组公式。使用带有自定义函数的数组公式的机制可能是您可以在此处研究的内容Custom SHEETNAME function not working in Arrayformula

注意:30 秒的配额适用于自定义函数的执行


/**
 * gets the MaxAmount, Current and Color from the data
 *
 * @param {cell reference or range} range The range to analyse.
 * @return amountMax,currency and  color
 * @customfunction
 */
function getJsonData(range) {

  //so6031098606
  // Test whether range is an array.
  if (range.map) {

  // if yes, then loop through the rows and build the row values
    var jsonLine = [];
    for (var i = 0; i < range.length; i++) {
      var jsonValues=[];
      var v = JSON.parse(range[i][0]);
      jsonValues.push(v.amountMax);
      jsonValues.push(v.currency);
      jsonValues.push(v.color);

      // aggregate the row values
      jsonLine.push(jsonValues); 
   } // end i

   return jsonLine;

 } else {

   // if no, then just return a single set of values
   var v = JSON.parse(range);
   var jsonValues = [];
   jsonValues.push(v.amountMax);
   jsonValues.push(v.currency);
   jsonValues.push(v.color);
   return [jsonValues];
 } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2016-08-02
    • 2020-08-10
    • 1970-01-01
    相关资源
    最近更新 更多