将countif函数向下扩展到Google表格中的一列

问题描述 投票:0回答:1

我正在尝试使用数组将以下公式扩展到一列中,据我所知,它不适用于countif。我需要此作为列中的除数,让分子成功地向下扩展到该列。这是在每行中都能正常运行的函数:

=arrayformula(countifs(unique(if('All Data'!$B$2:$B=$A2,'All Data'!$A$2:$A,)),">="&edate(F$2,0),unique(if('All Data'!$B$2:$B=$A2,'All Data'!$A$2:$A,)),"<="&edate(F$2,1)))

我正在努力避免在整个工作表中复制公式,因为它相当大,并且希望它作为一个数组工作,在这里我可以得到第2行中的a2,第3行中的a3,第4行中的a4的计数等等,所以我尝试了这个公式,该公式不起作用:

=arrayformula(countifs(unique(if('All Data'!$B$2:$B=$A2:$A,'All Data'!$A$2:$A,)),">="&edate(F$2,0),unique(if('All Data'!$B$2:$B=$A2:$A,'All Data'!$A$2:$A,)),"<="&edate(F$2,1)))

是否可以使用变通办法为我扩展此功能?

谢谢!

google-sheets google-sheets-formula countif
1个回答
1
投票

您可以使用Apps脚本custom function完成此操作。为此,请按照下列步骤操作:

  • 在电子表格中,选择工具>脚本编辑器打开绑定到文件的脚本。
  • 在脚本编辑器中复制此功能,并保存项目:
function AVERAGE_STUDENTS(classes, monthIndex) {
  const sheet = SpreadsheetApp.getActive().getSheetByName("All Data");
  const data = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn()).getValues();
  const monthRows = data.filter(row => row[0].getMonth() + 1 == monthIndex);
  return classes.map(classy => {
    const classRows = monthRows.filter(row => row[1] == classy[0]);
    const totalStudents = classRows.length;
    const uniqueDates = new Set(classRows.map(element => JSON.stringify([element[0], element[1]]))).size;
    return totalStudents / uniqueDates;
  });
}
  • 现在,如果您返回电子表格,则可以像使用任何内置功能一样使用此功能。您只需为类提供适当的范围(在这种情况下为A3:A18)和月份索引(对于4月,则为4),如下所示:

enter image description here

注意:

  • 也可以修改此函数,以便仅在C1处调用该函数并自动填充所有月份。

参考:

© www.soinside.com 2019 - 2024. All rights reserved.