【问题标题】:How to find the sum of certain column values ​by conditions from another one?如何通过条件从另一个列中找到某些列值的总和?
【发布时间】:2022-01-02 14:23:14
【问题描述】:

有 2 列。第一个具有日期格式 DD.MM.YYYY 的值,第二个具有整数值。如何根据匹配第一列月份的条件产生第二列数字的总和?例如,我对匹配 MONTH () = 11 的值的总和感兴趣?

【问题讨论】:

标签: google-apps-script google-sheets google-sheets-formula


【解决方案1】:

每月总和

function myfunk() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0');
  const vs = sh.getRange(2,1,sh.getLastRow() - 1, 2).getValues();
  let sums = {pA:[]};
  vs.forEach((r => {
    let p = r[0].split('.').filter((e,i) => i > 0).reduce((a,c,idx) => {
      if(idx > 0) a += '.';
      a += c;
      return a;
    });
    if(!sums.hasOwnProperty(p)) {
      sums[p]= r[1];
      sums.pA.push(p);
    } else {
      sums[p] += r[1]
    }
  }));
  let html = '<style> td,th{border: 1px solid black;}</style><table><tr><th>Month</th><th>Sum</th></tr>';
  sums.pA.forEach(p => {
    html += `<tr><td>${p}</td><td>${sums[p]}</td></tr>`
  });
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html),'Monthly Sums')
}

数据:

求和对话框:

为 osh 添加了每月总和

function myfunk() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0');
  const osh = ss.getSheetByName('Sheet1');
  osh.clear();
  const vs = sh.getRange(2,1,sh.getLastRow() - 1, 2).getValues();
  let sums = {pA:[]};
  vs.forEach((r => {
    let p = r[0].split('.').filter((e,i) => i > 0).reduce((a,c,idx) => {
      if(idx > 0) a += '.';
      a += c;
      return a;
    });
    if(!sums.hasOwnProperty(p)) {
      sums[p]= r[1];
      sums.pA.push(p);
    } else {
      sums[p] += r[1]
    }
  }));
  let html = '<style> td,th{border: 1px solid black;}</style><table><tr><th>Month</th><th>Sum</th></tr>';
  let arr = [['Month','Sums']];
  sums.pA.forEach(p => {
    html += `<tr><td>${p}</td><td>${sums[p]}</td></tr>`
    arr.push([`${p}`,`${sums[p]}`])
  });
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html),'Monthly Sums');
  osh.getRange(1,1,arr.length,arr[0].length).setValues(arr);
}

【讨论】:

  • 工作正常,谢谢。但我需要将收到的 arr 值放入新的 Range。你能帮帮我吗?我不擅长理解使用数组的细节
  • 将每月总和输出添加到 osh
  • 返回错误:TypeError: r[0].split is not a function
  • 和上次一样的代码。反正那是3天前的事了,我都忘记了。所以你可以自己调试。
【解决方案2】:

假设您的原始数据位于名为 Sheet1 的工作表中,您在 A1 和 B1 中有标题,并且您发布的数据集在 A2:B14 中,单元格 A15:B 中没有任何内容。在新工作表中,在单元格 A1 中输入此公式:

=ArrayFormula(QUERY({DATE(YEAR(Sheet1!A2:A),MONTH(Sheet1!A2:A),1),B2:B},"Select Col1, SUM(Col2) WHERE Col2 Is Not Null GROUP BY Col1 LABEL Col1 'MONTH', SUM(Col2) 'TOTAL' FORMAT Col1 'mmm yyyy'"))

QUERY 将生成两个标题(如果您愿意,可以在QUERYLABEL 部分更改)以及数据集中所有月份/年份的所有结果。

QUERY 作用于一个虚拟数组,该数组首先根据原始日期的月份和年份将 Sheet1!A2:A 中的所有日期转换为新的DATEs,但所有日期都落在该月的第一天。这允许将SUMs 分组到QUERY 中。然后QUERYFORMAT 部分将这些标准化日期转换为仅显示月份和年份部分。

调整工作表名称和引用范围以适合实际数据的工作表名称和数据范围。

注意(在得知 OP 的语言环境是俄罗斯之后):

此版本适用于您的语言环境...

=ArrayFormula(QUERY({DATE(YEAR(Sheet1!A2:A); MONTH(Sheet1!A2:A);1) \ Sheet1!B2:B}; "Select Col1, SUM(Col2) WHERE Col2 Is Not Null GROUP BY Col1 LABEL Col1 'MONTH', SUM(Col2) 'TOTAL' FORMAT Col1 'mmm yyyy'"))

【讨论】:

  • 非常感谢您快速详细的回答。但是粘贴该公式后出现语法错误。 Mb 我首先需要将日期数据转换为“mmm yyyy”格式?
  • 在将“Sheet2”重命名为“Erik Help”之后,我更新了电子表格中您所在地区(俄罗斯)的公式。新的公式版本; =ArrayFormula(QUERY({DATE(YEAR(Sheet1!A2:A);MONTH(Sheet1!A2:A);1)\Sheet1!B2:B};"Select Col1, SUM(Col2) WHERE Col2 Is Not Null GROUP BY Col1 LABEL Col1 'MONTH', SUM(Col2) 'TOTAL' FORMAT Col1 'mmm yyyy'"))
  • 太过分了!现在可以了
  • 不客气。
猜你喜欢
  • 2016-08-08
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 2022-12-09
  • 2021-02-11
  • 2021-09-28
  • 2016-03-22
  • 2018-04-21
相关资源
最近更新 更多