From 821e1640416c7c57fbcd702061fd48afb610cce7 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Tue, 31 Dec 2019 10:41:43 +0000 Subject: [PATCH] Bug 63700: Make D* functions work with numeric result column git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1872136 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/formula/functions/DStarRunner.java | 41 ++++++++++-------- test-data/spreadsheet/DGet.xls | Bin 54784 -> 55296 bytes 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/java/org/apache/poi/ss/formula/functions/DStarRunner.java b/src/java/org/apache/poi/ss/formula/functions/DStarRunner.java index b55ba808b0..295721b4dd 100644 --- a/src/java/org/apache/poi/ss/formula/functions/DStarRunner.java +++ b/src/java/org/apache/poi/ss/formula/functions/DStarRunner.java @@ -54,13 +54,13 @@ public final class DStarRunner implements Function3Arg { /** @see DSum */ DSUM(DSum::new), ; - + private final Supplier implSupplier; - private DStarAlgorithmEnum(Supplier implSupplier) { + DStarAlgorithmEnum(Supplier implSupplier) { this.implSupplier = implSupplier; } - + /** * @return a new function implementation instance */ @@ -94,7 +94,7 @@ public final class DStarRunner implements Function3Arg { } AreaEval db = (AreaEval)database; AreaEval cdb = (AreaEval)conditionDatabase; - + try { filterColumn = OperandResolver.getSingleValue(filterColumn, srcRowIndex, srcColumnIndex); } catch (EvaluationException e) { @@ -149,7 +149,7 @@ public final class DStarRunner implements Function3Arg { } /** - * + * * * @param nameValueEval Must not be a RefEval or AreaEval. Thus make sure resolveReference() is called on the value first! * @param db Database @@ -158,8 +158,17 @@ public final class DStarRunner implements Function3Arg { */ private static int getColumnForName(ValueEval nameValueEval, AreaEval db) throws EvaluationException { - String name = OperandResolver.coerceValueToString(nameValueEval); - return getColumnForString(db, name); + if (nameValueEval instanceof NumericValueEval) { + int columnNo = OperandResolver.coerceValueToInt(nameValueEval) - 1; + if (columnNo < 0 || columnNo >= db.getWidth()) { + return -1; + } + return columnNo; + } + else { + String name = OperandResolver.coerceValueToString(nameValueEval); + return getColumnForString(db, name); + } } /** @@ -169,10 +178,8 @@ public final class DStarRunner implements Function3Arg { * @param db Database. * @param name Column heading. * @return Corresponding column number. - * @throws EvaluationException If it's not possible to turn all headings into strings. */ - private static int getColumnForString(AreaEval db,String name) - throws EvaluationException { + private static int getColumnForString(AreaEval db,String name) { int resultColumn = -1; final int width = db.getWidth(); for(int column = 0; column < width; ++column) { @@ -216,10 +223,10 @@ public final class DStarRunner implements Function3Arg { // special column that accepts formulas. boolean columnCondition = true; ValueEval condition; - + // The condition to apply. condition = resolveReference(cdb, conditionRow, column); - + // If the condition is empty it matches. if(condition instanceof BlankEval) continue; @@ -229,7 +236,7 @@ public final class DStarRunner implements Function3Arg { if(!(targetHeader instanceof StringValueEval)) { throw new EvaluationException(ErrorEval.VALUE_INVALID); } - + if (getColumnForName(targetHeader, db) == -1) // No column found, it's again a special column that accepts formulas. columnCondition = false; @@ -269,7 +276,7 @@ public final class DStarRunner implements Function3Arg { throws EvaluationException { if(condition instanceof StringEval) { String conditionString = ((StringEval)condition).getStringValue(); - + if(conditionString.startsWith("<")) { // It's a XWu)HmBPn~_I2XUzLvsxDbNxb=rTithIh~inS+u3u;B~#M+O29fWh5n z$PD;Qqn9vd$mkj}0+xFbFl>f`TY@TpV8ArO!JyIa4fU8?9C%>ZjD;iKeub37lZ`+y zY;*@B0e@mo^GHrc)hS@6O@7vKHF3DjuN9B*9#x&dy$JB_>%iD`;JbO+XFR|1BJG@m z2qQuYqX%AuxWB@+4*_z6Fv*{_)>_oj`zV66_fcaF8^m2WHTTM>Lm(4VSq)A<1cZ>c zJl!r;_i!}UZys!R@f>clY6GM zrVMs&cA*Iev^bah?r9`$4bG`76{s%%yc4*Y19TPu-8el=%@r86aQkS`;G*hwoUWJTvY}_{JPU zuS32Z7+=-vks}vI3KJJDepI3xI4)PF%Vg`!QDMkyGbgAyCEuJWOB7u53JPtOFmb&{ z3GGUd1+$Nl@38o;*%!!rET?Z~pVIoAS{@$^kY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