From 4a42f6a765840cea6beeef02b18056b27091f5dc Mon Sep 17 00:00:00 2001 From: "Amol S. Deshmukh" Date: Sun, 26 Nov 2006 06:14:11 +0000 Subject: [PATCH] initial version git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@479290 13f79535-47bb-0310-9956-ffa450edef68 --- .../record/formula/functions/Islogical.java | 32 ++++++- .../record/formula/functions/Isnontext.java | 33 +++++++- .../record/formula/functions/Isnumber.java | 33 +++++++- .../hssf/record/formula/functions/Isref.java | 27 +++++- .../hssf/record/formula/functions/Istext.java | 33 +++++++- .../hssf/record/formula/functions/Left.java | 82 +++++++++++++++++- .../hssf/record/formula/functions/Right.java | 83 ++++++++++++++++++- 7 files changed, 305 insertions(+), 18 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Islogical.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Islogical.java index 1b2e57d7b6..8a4cac2e3e 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Islogical.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Islogical.java @@ -4,10 +4,38 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.RefEval; +import org.apache.poi.hssf.record.formula.eval.ValueEval; + /** - * @author + * @author Amol S. Deshmukh < amolweb at ya hoo dot com > * */ -public class Islogical extends NotImplementedFunction { +public class Islogical extends LogicalFunction { + public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) { + Eval retval = BoolEval.FALSE; + + switch (operands.length) { + default: + retval = ErrorEval.VALUE_INVALID; + break; + case 1: + Eval eval = operands[0]; + if (eval instanceof BoolEval) { + retval = BoolEval.TRUE; + } + else if (eval instanceof RefEval) { + Eval xlatedEval = xlateRefEval((RefEval) eval); + if (xlatedEval instanceof BoolEval) { + retval = BoolEval.TRUE; + } + } + } + + return retval; + } } diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isnontext.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isnontext.java index 71c9223543..643d6070d1 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isnontext.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isnontext.java @@ -4,10 +4,37 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.RefEval; +import org.apache.poi.hssf.record.formula.eval.StringEval; + /** - * @author + * @author Amol S. Deshmukh < amolweb at ya hoo dot com > * */ -public class Isnontext extends NotImplementedFunction { - +public class Isnontext extends LogicalFunction { + public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) { + Eval retval = BoolEval.TRUE; + + switch (operands.length) { + default: + retval = ErrorEval.VALUE_INVALID; + break; + case 1: + Eval eval = operands[0]; + if (eval instanceof StringEval) { + retval = BoolEval.FALSE; + } + else if (eval instanceof RefEval) { + Eval xlatedEval = xlateRefEval((RefEval) eval); + if (xlatedEval instanceof StringEval) { + retval = BoolEval.FALSE; + } + } + } + + return retval; + } } diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isnumber.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isnumber.java index 9337d5b5ea..5019165af9 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isnumber.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isnumber.java @@ -4,10 +4,37 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.record.formula.eval.RefEval; + /** - * @author + * @author Amol S. Deshmukh < amolweb at ya hoo dot com > * */ -public class Isnumber extends NotImplementedFunction { - +public class Isnumber extends LogicalFunction { + public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) { + Eval retval = BoolEval.FALSE; + + switch (operands.length) { + default: + retval = ErrorEval.VALUE_INVALID; + break; + case 1: + Eval eval = operands[0]; + if (eval instanceof NumberEval) { + retval = BoolEval.TRUE; + } + else if (eval instanceof RefEval) { + Eval xlatedEval = xlateRefEval((RefEval) eval); + if (xlatedEval instanceof NumberEval) { + retval = BoolEval.TRUE; + } + } + } + + return retval; + } } diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isref.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isref.java index 8053f5a69e..fd935ee545 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isref.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Isref.java @@ -4,10 +4,31 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.AreaEval; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.RefEval; + /** - * @author + * @author Amol S. Deshmukh < amolweb at ya hoo dot com > * */ -public class Isref extends NotImplementedFunction { - +public class Isref implements Function { + public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) { + Eval retval = BoolEval.FALSE; + + switch (operands.length) { + default: + retval = ErrorEval.VALUE_INVALID; + break; + case 1: + Eval eval = operands[0]; + if (eval instanceof RefEval || eval instanceof AreaEval) { + retval = BoolEval.TRUE; + } + } + + return retval; + } } diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Istext.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Istext.java index 5a571f09cb..1a9b158f19 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Istext.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Istext.java @@ -4,10 +4,37 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.RefEval; +import org.apache.poi.hssf.record.formula.eval.StringEval; + /** - * @author + * @author Amol S. Deshmukh < amolweb at ya hoo dot com > * */ -public class Istext extends NotImplementedFunction { - +public class Istext extends LogicalFunction { + public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) { + Eval retval = BoolEval.FALSE; + + switch (operands.length) { + default: + retval = ErrorEval.VALUE_INVALID; + break; + case 1: + Eval eval = operands[0]; + if (eval instanceof StringEval) { + retval = BoolEval.TRUE; + } + else if (eval instanceof RefEval) { + Eval xlatedEval = xlateRefEval((RefEval) eval); + if (xlatedEval instanceof StringEval) { + retval = BoolEval.TRUE; + } + } + } + + return retval; + } } diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Left.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Left.java index 482b609532..db37d26f8b 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Left.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Left.java @@ -4,10 +4,88 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.record.formula.eval.RefEval; +import org.apache.poi.hssf.record.formula.eval.StringEval; +import org.apache.poi.hssf.record.formula.eval.ValueEval; + /** - * @author + * @author Amol S. Deshmukh < amolweb at ya hoo dot com > * */ -public class Left extends NotImplementedFunction { +public class Left extends TextFunction { + public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) { + Eval retval = ErrorEval.VALUE_INVALID; + int index = 1; + switch (operands.length) { + default: + break; + case 2: + Eval indexEval = operands[1]; + index = evaluateAsInteger(indexEval); + if (index < 0) { + break; + } + case 1: + ValueEval veval = singleOperandEvaluate(operands[0], srcCellRow, srcCellCol); + String str = null; + if (veval instanceof StringEval) { + StringEval stringEval = (StringEval) veval; + str = stringEval.getStringValue(); + } + else if (veval instanceof BoolEval) { + BoolEval beval = (BoolEval) veval; + str = beval.getBooleanValue() ? "TRUE" : "FALSE"; + } + else if (veval instanceof NumberEval) { + NumberEval neval = (NumberEval) veval; + str = neval.getStringValue(); + } + if (null != str) { + str = str.substring(0, Math.min(str.length(), index)); + retval = new StringEval(str); + } + } + return retval; + } + + protected int evaluateAsInteger(Eval eval) { + int numval = -1; + if (eval instanceof NumberEval) { + NumberEval neval = (NumberEval) eval; + double d = neval.getNumberValue(); + numval = (int) d; + } + else if (eval instanceof StringEval) { + StringEval seval = (StringEval) eval; + String s = seval.getStringValue(); + try { + double d = Double.parseDouble(s); + numval = (int) d; + } + catch (Exception e) { + } + } + else if (eval instanceof BoolEval) { + BoolEval beval = (BoolEval) eval; + numval = beval.getBooleanValue() ? 1 : 0; + } + else if (eval instanceof RefEval) { + numval = evaluateAsInteger(xlateRefEval((RefEval) eval)); + } + return numval; + } + + protected Eval xlateRefEval(RefEval reval) { + Eval retval = reval.getInnerValueEval(); + + if (retval instanceof RefEval) { + retval = xlateRefEval((RefEval) retval); + } + return retval; + } } diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Right.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Right.java index 454e786b37..6e88111856 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Right.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Right.java @@ -4,10 +4,89 @@ */ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.BoolEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.record.formula.eval.RefEval; +import org.apache.poi.hssf.record.formula.eval.StringEval; +import org.apache.poi.hssf.record.formula.eval.ValueEval; + /** - * @author + * @author Amol S. Deshmukh < amolweb at ya hoo dot com > * */ -public class Right extends NotImplementedFunction { +public class Right extends TextFunction { + public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) { + Eval retval = ErrorEval.VALUE_INVALID; + int index = 1; + switch (operands.length) { + default: + break; + case 2: + Eval indexEval = operands[1]; + index = evaluateAsInteger(indexEval); + if (index < 0) { + break; + } + case 1: + ValueEval veval = singleOperandEvaluate(operands[0], srcCellRow, srcCellCol); + String str = null; + if (veval instanceof StringEval) { + StringEval stringEval = (StringEval) veval; + str = stringEval.getStringValue(); + } + else if (veval instanceof BoolEval) { + BoolEval beval = (BoolEval) veval; + str = beval.getBooleanValue() ? "TRUE" : "FALSE"; + } + else if (veval instanceof NumberEval) { + NumberEval neval = (NumberEval) veval; + str = neval.getStringValue(); + } + if (null != str) { + int strlen = str.length(); + str = str.substring(Math.max(0, strlen-index)); + retval = new StringEval(str); + } + } + return retval; + } + + protected int evaluateAsInteger(Eval eval) { + int numval = -1; + if (eval instanceof NumberEval) { + NumberEval neval = (NumberEval) eval; + double d = neval.getNumberValue(); + numval = (int) d; + } + else if (eval instanceof StringEval) { + StringEval seval = (StringEval) eval; + String s = seval.getStringValue(); + try { + double d = Double.parseDouble(s); + numval = (int) d; + } + catch (Exception e) { + } + } + else if (eval instanceof BoolEval) { + BoolEval beval = (BoolEval) eval; + numval = beval.getBooleanValue() ? 1 : 0; + } + else if (eval instanceof RefEval) { + numval = evaluateAsInteger(xlateRefEval((RefEval) eval)); + } + return numval; + } + + protected Eval xlateRefEval(RefEval reval) { + Eval retval = reval.getInnerValueEval(); + + if (retval instanceof RefEval) { + retval = xlateRefEval((RefEval) retval); + } + return retval; + } } -- 2.39.5