1 package org.simantics.spreadsheet.solver.formula;
3 import org.simantics.databoard.binding.mutable.Variant;
4 import org.simantics.spreadsheet.SpreadsheetMatrix;
5 import org.simantics.spreadsheet.Spreadsheets;
6 import org.simantics.spreadsheet.solver.formula.parser.ast.AstArgList;
7 import org.simantics.spreadsheet.solver.formula.parser.ast.AstValue;
9 public class AverageFormulaFunction implements CellFormulaFunction<Object> {
12 public Object evaluate(CellValueVisitor visitor, AstArgList args) {
13 if (args.values.size() == 0)
14 throw new IllegalStateException();
17 for(AstValue value : args.values){
18 Object res = value.accept(visitor);
19 if (res instanceof SpreadsheetMatrix) {
20 Object value2 = ((SpreadsheetMatrix) res).sumWithFormulaError();
21 if(value2 instanceof String) return value2;
23 sum += ((Number)value2).doubleValue();
24 count += ((SpreadsheetMatrix) res).countOfActualDoubleValues();
26 FormulaError2 err = FormulaError2.forObject(res);
27 if(err!=null) return err.getString();
29 if(res instanceof Variant){
30 Double dVal = Spreadsheets.asDoubleWhereEmptyStringIsZero(res);
31 if(dVal==null) res = ((Variant)res).toString();
36 if(res instanceof String && !res.equals("")){
37 v = Spreadsheets.asDoubleWhereEmptyStringIsZero(res);
39 else if(res instanceof Number)
40 v = Spreadsheets.asDoubleWhereEmptyStringIsZero(res);
48 if(count==0.0) return FormulaError2.DIV0.getString();