简体   繁体   English

在谷歌脚本中将数据从对象推送到电子表格

[英]pushing data from an object to a spreadsheet in google scripts

I have grabbed data from my source spreadsheet and placed it into an array of objects that contain name : value pairs for each row in the spreadsheet . 我从源代码电子表中获取数据并将其放入一个对象array ,这些对象包含spreadsheetrow namevalue对。

The name comes from the header row in my spreadsheet , and the values come from each following row. 该名称来自我的spreadsheet的标题row ,值来自后面的每一行。

I am then looping through the name and value pairs in the objects and modifying them before they go into the target spreadsheet. 然后我循环遍历对象中的名称和值对,并在它们进入目标电子表格之前修改它们。

This is what the data looks like in my debugger: 这是我的调试器中的数据:

objectData[29] | Array | [{id:1001, name:"John", cats:"3"}, {id:1002, name:"Shelley", cats:"9"}...]

I would like to take the data in this object and place it in the target spreadsheet with the condition that the object names match the column names in the spreadsheet, and the object values are placed on a new row underneath the matching columns. 我想获取此对象中的数据并将其放在目标电子表格中,条件是对象名称与电子表格中的列名称匹配,对象值放在匹配列下面的新行上。

I'm really new to scripting and am having a horrendous time figuring out how to do this. 我真的很喜欢编写脚本,而且我有一个可怕的时间来弄清楚如何做到这一点。

This and much more available at https://gist.github.com/mhawksey/1442370 这可以通过https://gist.github.com/mhawksey/1442370获得

// setRowsData fills in one row of data per object defined in the objects Array.
// For every Column, it checks if data objects define a value for it.
// Arguments:
//   - sheet: the Sheet Object where the data will be written
//   - objects: an Array of Objects, each of which contains data for a row
//   - optHeadersRange: a Range of cells where the column headers are defined. This
//     defaults to the entire first row in sheet.
//   - optFirstDataRowIndex: index of the first row where data should be written. This
//     defaults to the row immediately below the headers.
function setRowsData(sheet, objects, optHeadersRange, optFirstDataRowIndex) {
  var headersRange = optHeadersRange || sheet.getRange(1, 1, 1, sheet.getMaxColumns());
  var firstDataRowIndex = optFirstDataRowIndex || headersRange.getRowIndex() + 1;
  var headers = normalizeHeaders(headersRange.getValues()[0]);

  var data = [];
  for (var i = 0; i < objects.length; ++i) {
    var values = []
    for (j = 0; j < headers.length; ++j) {
      var header = headers[j];
      values.push(header.length > 0 && objects[i][header] ? objects[i][header] : "");
    }
    data.push(values);
  }
  var destinationRange = sheet.getRange(firstDataRowIndex, headersRange.getColumnIndex(), 
                                        objects.length, headers.length);
  destinationRange.setValues(data);
}

Edit: I noticed a function used above was not included 编辑:我注意到上面使用的函数没有包括在内

// Returns an Array of normalized Strings.
// Arguments:
//   - headers: Array of Strings to normalize
function normalizeHeaders(headers) {
  var keys = [];
  for (var i = 0; i < headers.length; ++i) {
    var key = normalizeHeader(headers[i]);
    if (key.length > 0) {
      keys.push(key);
    }
  }
  return keys;
}

// Normalizes a string, by removing all alphanumeric characters and using mixed case
// to separate words. The output will always start with a lower case letter.
// This function is designed to produce JavaScript object property names.
// Arguments:
//   - header: string to normalize
// Examples:
//   "First Name" -> "firstName"
//   "Market Cap (millions) -> "marketCapMillions
//   "1 number at the beginning is ignored" -> "numberAtTheBeginningIsIgnored"
function normalizeHeader(header) {
  var key = "";
  var upperCase = false;
  for (var i = 0; i < header.length; ++i) {
    var letter = header[i];
    if (letter == " " && key.length > 0) {
      upperCase = true;
      continue;
    }
    //if (!isAlnum(letter)) {
    //  continue;
    //}
    if (key.length == 0 && isDigit(letter)) {
      continue; // first character must be a letter
    }
    if (upperCase) {
      upperCase = false;
      key += letter.toUpperCase();
    } else {
      key += letter.toLowerCase();
    }
  }
  return key;
}

the link at the top of answer includes code for procedures that you have already accomplished. 答案顶部的链接包括您已完成的程序的代码。 Seeing how others attain the same goal is often very enlightening. 看到其他人如何达到同一目标通常是非常有启发性的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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