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