简体   繁体   中英

Doing multiple SUMIFS - Google Sheet Script Editor

I`m trying to perform SUMIFS in Google Sheet Script Editor

What I`m trying to do is...

I have this raw table.

在此处输入图片说明

I`m trying to create a report that looks like below.. with the matching values (from that raw table)

在此处输入图片说明

在此处输入图片说明

So I used to use SUMIFS(value) + SUMIFS(value) for this.

Is there way to do the same job in Script Editor?

There is a common issue regards aggregates

/**
 * @customfunction
 */
function BLAHBLAHFUNCTION(range) {
  var header = range.shift();
  var vendors = range.map(function(row){return row[1];});
  var r =  range.reduce(function(p, row) {
    if (!p.hasOwnProperty(row[0])) { 
      p[row[0]] = {};
      vendors.forEach(function(vendor){
        if (!p[row[0]].hasOwnProperty(vendor))
          p[row[0]][vendor] = { AB: 0, CD: 0 };
      });
    }
    p[row[0]][row[1]].AB += row[2] + row[3];
    p[row[0]][row[1]].CD += row[4] + row[5];
    return p;
  }, {});

  var res = [header.slice(0,2).concat('AB', 'CD')];
  for(var month in r){
    for(vendor in r[month]){
      res.push([month, vendor, r[month][vendor].AB, r[month][vendor].CD]);
    }
  }
  return res;
}

在此处输入图片说明

My example https://docs.google.com/spreadsheets/d/1yo627ZkT7zG7_Aj86Ww0iqUTd2o1xCAY_hDWk9DPsao/edit?usp=sharing

I wrote a test function to read ACTIVE sheet values and write different vendor reports in separate sheets . If you wanna know more about my code, let me know in comment.

function test() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var values = sheet.getDataRange().getValues();
  // remove header row values from array
  values.shift();

  // will be months present in data
  // { JAN: 1, FEB: 1 ... }
  var mos = {};
  // data structure
  // { A: {JAN: [value1, value2] }... }
  var ds = {};
  values.forEach(function(row) {
    var month = row.shift(); // 1st col
    var ven = row.shift(); // 2nd col
    mos[month] = 1; // set month on mos
    ds[ven] = ds[ven] || {};
    // set value on ds
    // example = ds.A.JAN = [ A+B, C+D ]
    ds[ven][month] = [parseInt(row[0]) + parseInt(row[1]), parseInt(row[2]) + parseInt(row[3])];
  });
  //  Logger.log(mos);
  var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
  // filter months to get months present in sheet data in order
  months = months.filter(function(m) {
    return mos[m] == 1;
  });
  // Logger.log(months);
  // result object, for each vendor create array of rows
  // { A: [ row, row... ]... }
  var rs = {};
  Object.keys(ds).forEach(function(v) {
    rs[v] = rs[v] || [];
    months.forEach(function(m) {
      if (ds[v][m]) {
        rs[v].push([m, ds[v][m][0], ds[v][m][1]]);
      } else {
        rs[v].push([m, 'N/A', 'N/A']);
      }
    });
  });
  // Logger.log(rs);
  // write rows
  Object.keys(rs).forEach(function(v) {
    var headers = [['Vendor', v, 'Report'].join(' '), 'Conversion (A+B)', 'Conversion (C+D)'];
    // insert headers into rows for writing
    rs[v].unshift(headers);
    try {
      ss.insertSheet(headers[0]);
    } catch (e) {}
    var sh = ss.getSheetByName(headers[0]);
    sh.clear();
    sh.getRange(1, 1, rs[v].length, 3).setValues(rs[v]);
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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