在 Google 表格中向下扩展 countif 函数

Expanding a countif function down a column in Google Sheets

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

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

我试图避免在整个 sheet 中复制公式,因为它相当大并且更喜欢它作为一个数组工作,它为我提供第 2 行中的 a2,第 2 行中的 a3 的计数第 3 行,第 4 行中的 a4,等等,所以我尝试了这个公式,但它不起作用:

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

有没有我可以用来为我扩展的解决方法?

谢谢!

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

  • 在您的电子表格中,select工具 > 脚本编辑器打开绑定到您的文件的脚本。
  • 在脚本编辑器中复制此函数,并保存项目:
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)提供适当的范围,如下所示:

注:

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

参考: