--- /dev/null
+package org.simantics.spreadsheet.graph.formula;\r
+\r
+import org.simantics.spreadsheet.graph.CellFormulaFunction;\r
+import org.simantics.spreadsheet.graph.CellValueVisitor;\r
+import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;\r
+import org.simantics.spreadsheet.graph.SpreadsheetMatrix;\r
+import org.simantics.spreadsheet.graph.parser.ast.AstArgList;\r
+import org.simantics.spreadsheet.graph.parser.ast.AstRange;\r
+\r
+public class CountifFormulaFunction implements CellFormulaFunction<Integer> {\r
+\r
+ @Override\r
+ public Integer evaluate(CellValueVisitor visitor, AstArgList args) {\r
+ if (args.values.size() != 2) throw new IllegalStateException();\r
+ \r
+ AstRange range = (AstRange) args.values.get(0);\r
+ SpreadsheetMatrix matrix = (SpreadsheetMatrix) range.accept(visitor);\r
+ Object crit = null;\r
+ try {\r
+ crit = args.values.get(1).accept(visitor);\r
+ } catch (IllegalStateException e){\r
+ return 0;\r
+ }\r
+ \r
+ String criteria = SpreadsheetGraphUtils.asString(crit);\r
+ int result = 0;\r
+ for (Object value : matrix.values) {\r
+ if (SpreadsheetGraphUtils.matchCriteria(value, criteria))\r
+ result++;\r
+ }\r
+ return Integer.valueOf(result);\r
+ }\r
+}\r