1 package org.simantics.spreadsheet.graph.formula;
3 import org.simantics.spreadsheet.graph.CellFormulaFunction;
4 import org.simantics.spreadsheet.graph.CellValueVisitor;
5 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;
6 import org.simantics.spreadsheet.graph.SpreadsheetMatrix;
7 import org.simantics.spreadsheet.graph.parser.ast.AstArgList;
9 class SumifFormulaFunction implements CellFormulaFunction<Object> {
12 public Object evaluate(CellValueVisitor visitor, AstArgList args) {
13 if (args.values.size() == 2) {
14 Object test = args.values.get(0).accept(visitor);
15 Object criteria = null;
17 criteria = args.values.get(1).accept(visitor);
18 } catch (IllegalStateException e){
21 FormulaError2 error = FormulaError2.forObject(criteria);
22 if(error!=null) return error.getString();
24 if (test instanceof SpreadsheetMatrix) {
26 SpreadsheetMatrix tm = (SpreadsheetMatrix) test;
27 for (int i = 0; i < tm.values.length; i++) {
28 if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) {
29 double d = SpreadsheetGraphUtils.asNumber(tm.values[i]);
30 if (Double.isFinite(d))
36 Double d = SpreadsheetGraphUtils.asNumber(test);
41 if (args.values.size() == 3) {
42 Object test = args.values.get(0).accept(visitor);
43 Object criteria = null;
45 criteria = args.values.get(1).accept(visitor);
46 } catch (IllegalStateException e){
49 FormulaError2 error = FormulaError2.forObject(criteria);
50 if(error!=null) return error.getString();
52 Object range = args.values.get(2).accept(visitor);
54 if (test instanceof SpreadsheetMatrix) {
56 SpreadsheetMatrix tm = (SpreadsheetMatrix) test;
57 SpreadsheetMatrix rm = (SpreadsheetMatrix) range;
58 for (int i = 0; i < tm.values.length; i++) {
59 if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) {
60 double d = SpreadsheetGraphUtils.asNumber(rm.values[i]);
61 if (Double.isFinite(d))
67 Double d = SpreadsheetGraphUtils.asNumber(test);
72 throw new IllegalStateException();