1 package org.simantics.spreadsheet.graph.formula;
3 import org.simantics.databoard.binding.mutable.Variant;
4 import org.simantics.spreadsheet.graph.CellFormulaFunction;
5 import org.simantics.spreadsheet.graph.CellValueVisitor;
6 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;
7 import org.simantics.spreadsheet.graph.SpreadsheetMatrix;
8 import org.simantics.spreadsheet.graph.parser.ast.AstArgList;
9 import org.simantics.spreadsheet.graph.parser.ast.AstValue;
11 public class AverageFormulaFunction implements CellFormulaFunction<Object> {
14 public Object evaluate(CellValueVisitor visitor, AstArgList args) {
15 if (args.values.size() == 0)
16 throw new IllegalStateException();
19 for(AstValue value : args.values){
20 Object res = value.accept(visitor);
21 if (res instanceof SpreadsheetMatrix) {
22 Object value2 = ((SpreadsheetMatrix) res).sumWithFormulaError();
23 if(value2 instanceof String) return value2;
25 sum += ((Number)value2).doubleValue();
26 count += ((SpreadsheetMatrix) res).countOfActualDoubleValues();
28 FormulaError2 err = FormulaError2.forObject(res);
29 if(err!=null) return err.getString();
31 if(res instanceof Variant){
32 Double dVal = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);
33 if(dVal==null) res = ((Variant)res).toString();
38 if(res instanceof String && !res.equals("")){
39 v = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);
41 else if(res instanceof Number)
42 v = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);
50 if(count==0.0) return FormulaError2.DIV0.getString();