-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
-\r
-class SumifFormulaFunction implements CellFormulaFunction<Object> {\r
-\r
- @Override\r
- public Object evaluate(CellValueVisitor visitor, AstArgList args) {\r
- if (args.values.size() == 2) {\r
- Object test = args.values.get(0).accept(visitor);\r
- Object criteria = null;\r
- try {\r
- criteria = args.values.get(1).accept(visitor);\r
- } catch (IllegalStateException e){\r
- return 0;\r
- }\r
- FormulaError2 error = FormulaError2.forObject(criteria);\r
- if(error!=null) return error.getString();\r
- \r
- if (test instanceof SpreadsheetMatrix) {\r
- double sum = 0.0;\r
- SpreadsheetMatrix tm = (SpreadsheetMatrix) test;\r
- for (int i = 0; i < tm.values.length; i++) {\r
- if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) {\r
- double d = SpreadsheetGraphUtils.asNumber(tm.values[i]);\r
- if (Double.isFinite(d))\r
- sum += d;\r
- }\r
- }\r
- return sum;\r
- } else {\r
- Double d = SpreadsheetGraphUtils.asNumber(test);\r
- return d;\r
- }\r
- }\r
-\r
- if (args.values.size() == 3) {\r
- Object test = args.values.get(0).accept(visitor);\r
- Object criteria = null;\r
- try {\r
- criteria = args.values.get(1).accept(visitor);\r
- } catch (IllegalStateException e){\r
- return 0;\r
- }\r
- FormulaError2 error = FormulaError2.forObject(criteria);\r
- if(error!=null) return error.getString();\r
- \r
- Object range = args.values.get(2).accept(visitor);\r
-\r
- if (test instanceof SpreadsheetMatrix) {\r
- Double sum = 0.0;\r
- SpreadsheetMatrix tm = (SpreadsheetMatrix) test;\r
- SpreadsheetMatrix rm = (SpreadsheetMatrix) range;\r
- for (int i = 0; i < tm.values.length; i++) {\r
- if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) {\r
- double d = SpreadsheetGraphUtils.asNumber(rm.values[i]);\r
- if (Double.isFinite(d))\r
- sum += d;\r
- }\r
- }\r
- return sum;\r
- } else {\r
- Double d = SpreadsheetGraphUtils.asNumber(test);\r
- return d;\r
- }\r
-\r
- }\r
- throw new IllegalStateException();\r
- }\r
+package org.simantics.spreadsheet.graph.formula;
+
+import org.simantics.spreadsheet.graph.CellFormulaFunction;
+import org.simantics.spreadsheet.graph.CellValueVisitor;
+import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;
+import org.simantics.spreadsheet.graph.SpreadsheetMatrix;
+import org.simantics.spreadsheet.graph.parser.ast.AstArgList;
+
+class SumifFormulaFunction implements CellFormulaFunction<Object> {
+
+ @Override
+ public Object evaluate(CellValueVisitor visitor, AstArgList args) {
+ if (args.values.size() == 2) {
+ Object test = args.values.get(0).accept(visitor);
+ Object criteria = null;
+ try {
+ criteria = args.values.get(1).accept(visitor);
+ } catch (IllegalStateException e){
+ return 0;
+ }
+ FormulaError2 error = FormulaError2.forObject(criteria);
+ if(error!=null) return error.getString();
+
+ if (test instanceof SpreadsheetMatrix) {
+ double sum = 0.0;
+ SpreadsheetMatrix tm = (SpreadsheetMatrix) test;
+ for (int i = 0; i < tm.values.length; i++) {
+ if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) {
+ double d = SpreadsheetGraphUtils.asNumber(tm.values[i]);
+ if (Double.isFinite(d))
+ sum += d;
+ }
+ }
+ return sum;
+ } else {
+ Double d = SpreadsheetGraphUtils.asNumber(test);
+ return d;
+ }
+ }
+
+ if (args.values.size() == 3) {
+ Object test = args.values.get(0).accept(visitor);
+ Object criteria = null;
+ try {
+ criteria = args.values.get(1).accept(visitor);
+ } catch (IllegalStateException e){
+ return 0;
+ }
+ FormulaError2 error = FormulaError2.forObject(criteria);
+ if(error!=null) return error.getString();
+
+ Object range = args.values.get(2).accept(visitor);
+
+ if (test instanceof SpreadsheetMatrix) {
+ Double sum = 0.0;
+ SpreadsheetMatrix tm = (SpreadsheetMatrix) test;
+ SpreadsheetMatrix rm = (SpreadsheetMatrix) range;
+ for (int i = 0; i < tm.values.length; i++) {
+ if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) {
+ double d = SpreadsheetGraphUtils.asNumber(rm.values[i]);
+ if (Double.isFinite(d))
+ sum += d;
+ }
+ }
+ return sum;
+ } else {
+ Double d = SpreadsheetGraphUtils.asNumber(test);
+ return d;
+ }
+
+ }
+ throw new IllegalStateException();
+ }
}
\ No newline at end of file