diff --git a/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java b/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java index cda98828e0..89b73d025d 100644 --- a/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java +++ b/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java @@ -28,19 +28,7 @@ import java.util.TreeSet; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException; import org.apache.poi.ss.formula.atp.AnalysisToolPak; -import org.apache.poi.ss.formula.eval.BlankEval; -import org.apache.poi.ss.formula.eval.BoolEval; -import org.apache.poi.ss.formula.eval.ErrorEval; -import org.apache.poi.ss.formula.eval.EvaluationException; -import org.apache.poi.ss.formula.eval.ExternalNameEval; -import org.apache.poi.ss.formula.eval.FunctionEval; -import org.apache.poi.ss.formula.eval.FunctionNameEval; -import org.apache.poi.ss.formula.eval.MissingArgEval; -import org.apache.poi.ss.formula.eval.NotImplementedException; -import org.apache.poi.ss.formula.eval.NumberEval; -import org.apache.poi.ss.formula.eval.OperandResolver; -import org.apache.poi.ss.formula.eval.StringEval; -import org.apache.poi.ss.formula.eval.ValueEval; +import org.apache.poi.ss.formula.eval.*; import org.apache.poi.ss.formula.function.FunctionMetadataRegistry; import org.apache.poi.ss.formula.functions.Choose; import org.apache.poi.ss.formula.functions.FreeRefFunction; @@ -84,12 +72,12 @@ public final class WorkbookEvaluator { private final IStabilityClassifier _stabilityClassifier; private final AggregatingUDFFinder _udfFinder; - private boolean _ignoreMissingWorkbooks = false; + private boolean _ignoreMissingWorkbooks; /** * whether print detailed messages about the next formula evaluation */ - private boolean dbgEvaluationOutputForNextEval = false; + private boolean dbgEvaluationOutputForNextEval; // special logger for formula evaluation output (because of possibly very large output) private final POILogger EVAL_LOG = POILogFactory.getLogger("POI.FormulaEval"); @@ -415,11 +403,10 @@ public final class WorkbookEvaluator { Stack stack = new Stack(); for (int i = 0, iSize = ptgs.length; i < iSize; i++) { - // since we don't know how to handle these yet :( Ptg ptg = ptgs[i]; if (dbgEvaluationOutputIndent > 0) { - EVAL_LOG.log(POILogger.INFO, dbgIndentStr + " * ptg " + i + ": " + ptg); + EVAL_LOG.log(POILogger.INFO, dbgIndentStr + " * ptg " + i + ": " + ptg + ", stack: " + stack); } if (ptg instanceof AttrPtg) { AttrPtg attrPtg = (AttrPtg) ptg; @@ -504,13 +491,17 @@ public final class WorkbookEvaluator { continue; } + if (ptg instanceof UnionPtg) { + ValueEval v2 = stack.pop(); + ValueEval v1 = stack.pop(); + stack.push(new RefListEval(v1, v2)); + continue; + } + ValueEval opResult; if (ptg instanceof OperationPtg) { OperationPtg optg = (OperationPtg) ptg; - if (optg instanceof UnionPtg) { continue; } - - int numops = optg.getNumberOfOperands(); ValueEval[] ops = new ValueEval[numops]; diff --git a/src/java/org/apache/poi/ss/formula/eval/RefListEval.java b/src/java/org/apache/poi/ss/formula/eval/RefListEval.java new file mode 100644 index 0000000000..ee884da78d --- /dev/null +++ b/src/java/org/apache/poi/ss/formula/eval/RefListEval.java @@ -0,0 +1,46 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.formula.eval; + +import java.util.ArrayList; +import java.util.List; + +/** + * Handling of a list of values, e.g. the 2nd argument in RANK(A1,(B1,B2,B3),1) + */ +public class RefListEval implements ValueEval { + private final List list = new ArrayList(); + + public RefListEval(ValueEval v1, ValueEval v2) { + add(v1); + add(v2); + } + + private void add(ValueEval v) { + // flatten multiple nested RefListEval + if(v instanceof RefListEval) { + list.addAll(((RefListEval)v).list); + } else { + list.add(v); + } + } + + public List getList() { + return list; + } +} diff --git a/src/java/org/apache/poi/ss/formula/functions/LogicalFunction.java b/src/java/org/apache/poi/ss/formula/functions/LogicalFunction.java index a0825774b3..1bf9cf5ca4 100644 --- a/src/java/org/apache/poi/ss/formula/functions/LogicalFunction.java +++ b/src/java/org/apache/poi/ss/formula/functions/LogicalFunction.java @@ -17,16 +17,7 @@ package org.apache.poi.ss.formula.functions; -import org.apache.poi.ss.formula.eval.AreaEval; -import org.apache.poi.ss.formula.eval.BlankEval; -import org.apache.poi.ss.formula.eval.BoolEval; -import org.apache.poi.ss.formula.eval.ErrorEval; -import org.apache.poi.ss.formula.eval.EvaluationException; -import org.apache.poi.ss.formula.eval.NumberEval; -import org.apache.poi.ss.formula.eval.OperandResolver; -import org.apache.poi.ss.formula.eval.RefEval; -import org.apache.poi.ss.formula.eval.StringEval; -import org.apache.poi.ss.formula.eval.ValueEval; +import org.apache.poi.ss.formula.eval.*; /** * Implementation of the various ISxxx Logical Functions, which @@ -131,7 +122,7 @@ public abstract class LogicalFunction extends Fixed1ArgFunction { public static final Function ISREF = new Fixed1ArgFunction() { public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) { - if (arg0 instanceof RefEval || arg0 instanceof AreaEval) { + if (arg0 instanceof RefEval || arg0 instanceof AreaEval || arg0 instanceof RefListEval) { return BoolEval.TRUE; } return BoolEval.FALSE; diff --git a/src/java/org/apache/poi/ss/formula/functions/Rank.java b/src/java/org/apache/poi/ss/formula/functions/Rank.java index c6ff86068d..c5e07506be 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Rank.java +++ b/src/java/org/apache/poi/ss/formula/functions/Rank.java @@ -19,13 +19,7 @@ package org.apache.poi.ss.formula.functions; -import org.apache.poi.ss.formula.eval.AreaEval; -import org.apache.poi.ss.formula.eval.ErrorEval; -import org.apache.poi.ss.formula.eval.EvaluationException; -import org.apache.poi.ss.formula.eval.NumberEval; -import org.apache.poi.ss.formula.eval.OperandResolver; -import org.apache.poi.ss.formula.eval.RefEval; -import org.apache.poi.ss.formula.eval.ValueEval; +import org.apache.poi.ss.formula.eval.*; /** @@ -45,51 +39,56 @@ import org.apache.poi.ss.formula.eval.ValueEval; public class Rank extends Var2or3ArgFunction { public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { - - AreaEval aeRange; - double result; try { ValueEval ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex); - result = OperandResolver.coerceValueToDouble(ve); + double result = OperandResolver.coerceValueToDouble(ve); if (Double.isNaN(result) || Double.isInfinite(result)) { throw new EvaluationException(ErrorEval.NUM_ERROR); } - aeRange = convertRangeArg(arg1); + + if(arg1 instanceof RefListEval) { + return eval(result, ((RefListEval)arg1), true); + } + + final AreaEval aeRange = convertRangeArg(arg1); + + return eval(result, aeRange, true); } catch (EvaluationException e) { return e.getErrorEval(); } - return eval(srcRowIndex, srcColumnIndex, result, aeRange, true); } public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) { - - AreaEval aeRange; - double result; - boolean order=false; try { ValueEval ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex); - result = OperandResolver.coerceValueToDouble(ve); + final double result = OperandResolver.coerceValueToDouble(ve); if (Double.isNaN(result) || Double.isInfinite(result)) { throw new EvaluationException(ErrorEval.NUM_ERROR); } - aeRange = convertRangeArg(arg1); - - ve = OperandResolver.getSingleValue(arg2, srcRowIndex, srcColumnIndex); - int order_value = OperandResolver.coerceValueToInt(ve); - if(order_value==0){ - order=true; - }else if(order_value==1){ - order=false; - }else throw new EvaluationException(ErrorEval.NUM_ERROR); - + + ve = OperandResolver.getSingleValue(arg2, srcRowIndex, srcColumnIndex); + int order_value = OperandResolver.coerceValueToInt(ve); + final boolean order; + if(order_value==0) { + order = true; + } else if(order_value==1) { + order = false; + } else { + throw new EvaluationException(ErrorEval.NUM_ERROR); + } + + if(arg1 instanceof RefListEval) { + return eval(result, ((RefListEval)arg1), order); + } + + final AreaEval aeRange = convertRangeArg(arg1); + return eval(result, aeRange, order); } catch (EvaluationException e) { return e.getErrorEval(); } - return eval(srcRowIndex, srcColumnIndex, result, aeRange, order); } - private static ValueEval eval(int srcRowIndex, int srcColumnIndex, double arg0, AreaEval aeRange, boolean descending_order) { - + private static ValueEval eval(double arg0, AreaEval aeRange, boolean descending_order) { int rank = 1; int height=aeRange.getHeight(); int width= aeRange.getWidth(); @@ -105,9 +104,30 @@ public class Rank extends Var2or3ArgFunction { } return new NumberEval(rank); } - - private static Double getValue(AreaEval aeRange, int relRowIndex, int relColIndex) { + private static ValueEval eval(double arg0, RefListEval aeRange, boolean descending_order) { + int rank = 1; + for(ValueEval ve : aeRange.getList()) { + if (ve instanceof RefEval) { + ve = ((RefEval) ve).getInnerValueEval(((RefEval) ve).getFirstSheetIndex()); + } + + final Double value; + if (ve instanceof NumberEval) { + value = ((NumberEval)ve).getNumberValue(); + } else { + continue; + } + + if(descending_order && value>arg0 || !descending_order && value