使用第二个数组过滤一个数组,并将结果显示在电子表格上

时间:2018-10-06 19:01:50

标签: arrays google-apps-script google-sheets array-filter

-----背景-----在Google表格/应用脚本中,我试图从一张工作表中移出一些交易数据,然后将其组织在另一张工作表中,并为每个客户帐户提供小计行(称为“单位”),底部总计。

我将事务整理成一个对象数组,作为键:值对(“交易”,键是标题,一些值是字符串,其他是数字),第二个单元中的单元名为“ unitList” ”。我想遍历unitList数组中的每个元素,并将具有匹配单元的每个事务拖到我的“ targetSheet”上,然后为每个单元添加小计行。

-----更新10/7/2018 3:49 PM EST -----

感谢大家的投入-我听取了您的建议,放弃了我正在使用的库,而是找到了更好的getRowsData和appendRowsData函数,这些函数直接放在我的代码项目中。 这解决了数组过滤器问题(已通过记录filterResults验证),但现在,当我调用appendRowsData()时,出现以下错误:

  

范围的坐标或尺寸无效。 (第73行,“展示交易”文件)

第73行是appendRowsData函数中的以下行。对于解决此问题的任何帮助将不胜感激。

var destinationRange = sheet.getRange(firstDataRowIndex, 1, objects.length, 9);

到目前为止,这是我的整个项目:

function displayTransactions() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Label each sheet
  var dashboard = ss.getSheetByName('Dashboard');
  var unitsSheet = ss.getSheetByName('Unit Ref Table');
  var transactionsSheet = ss.getSheetByName('Transactions Ref Sheet');
  var targetSheet = ss.getSheetByName('Target Sheet');

  // Returns true if the cell where cellData was read from is empty.
  // Arguments:
  //   - cellData: string
  function isCellEmpty(cellData) {
    return typeof(cellData) == "string" && cellData == "";
  }

  // Define function that converts arrays into JSON
  //   For every row of data in data, generates an object that contains the data.
  //   Names of object fields are defined in keys.
  // Arguments:
  //   - data: JavaScript 2d array
  //   - keys: Array of Strings that define the property names for the objects to create  
  function getObjects(data, keys) {
    var objects = [];
    for (var i = 0; i < data.length; ++i) {
      var object = {};
      var hasData = false;
      for (var j = 0; j < data[i].length; ++j) {
        var cellData = data[i][j];
        if (isCellEmpty(cellData)) {
          continue;
        }
        object[keys[j]] = cellData;
        hasData = true;
      }
      if (hasData) {
        objects.push(object);
      }
    }
    return objects;
  }

  // Define function that pulls spreadsheet data into arrays, then converts to JSON using getObjects function
  function getRowsData(sheet) {    
    var headersRange = sheet.getRange(1, 1, 1, sheet.getLastColumn());
    var headers = headersRange.getValues()[0];
    var dataRange = sheet.getRange(sheet.getFrozenRows()+1, 1, sheet.getLastRow(), sheet.getLastColumn());
    return getObjects(dataRange.getValues(), headers);
  }

  // Define function appendRowsData that uses getLastRow to fill 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

  function appendRowsData(sheet, objects) {
    var headersRange = sheet.getRange(7, 1, 1, 9);
    var firstDataRowIndex = sheet.getLastRow() + 1;
    var headers = 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, 1, objects.length, 9);
    destinationRange.setValues(data);
  }

  // Call getRowsData on transactions sheet
  var transactions = getRowsData(transactionsSheet);

  // Get array of units
  var unitList = unitsSheet.getRange("B2:B").getValues();

  // Iterate through the unitList and pull all transactions with matching unit into the target sheet
  for (var i=0; i < unitList.length; i++) {
    var subTotal = 0;
    var grandTotal = 0;
    var filterResults = transactions.filter(function(x) {
      return x['Unit'] == unitList[i];      
    })

    Logger.log(filterResults); // This brings the correct results!  

    // Display matching transactions
    appendRowsData(targetSheet, filterResults);

    // Grand total at the bottom when i=units.length

  }
}

0 个答案:

没有答案