From: Nick Burch Date: Tue, 11 Dec 2007 13:03:53 +0000 (+0000) Subject: Support for the Trim function, and a little enhancement to the formula evaluation... X-Git-Tag: REL_3_0_3_BETA1~253 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f6a7cb7483105b68b6c2f470adf4ca172ebefd1e;p=poi.git Support for the Trim function, and a little enhancement to the formula evaluation test git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@603233 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Trim.java b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Trim.java index 69b3ce6f55..5e9d91c7cc 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Trim.java +++ b/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Trim.java @@ -14,12 +14,62 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* - * Created on May 15, 2005 - * - */ package org.apache.poi.hssf.record.formula.functions; -public class Trim extends NotImplementedFunction { +import org.apache.poi.hssf.record.formula.eval.BlankEval; +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.StringEval; +import org.apache.poi.hssf.record.formula.eval.StringValueEval; +import org.apache.poi.hssf.record.formula.eval.ValueEval; + +/** + * An implementation of the TRIM function: + * Removes leading and trailing spaces from value if evaluated operand + * value is string. + * @author Manda Wilson < wilson at c bio dot msk cc dot org > + */ +public class Trim extends TextFunction { + /** + * Removes leading and trailing spaces from value if evaluated + * operand value is string. + * Returns StringEval only if evaluated operand is of type string + * (and is not blank or null) or number. If evaluated operand is + * of type string and is blank or null, or if evaluated operand is + * of type blank, returns BlankEval. Otherwise returns ErrorEval. + * + * @see org.apache.poi.hssf.record.formula.eval.Eval + */ + public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) { + Eval retval = ErrorEval.VALUE_INVALID; + String str = null; + + switch (operands.length) { + default: + break; + case 1: + ValueEval veval = singleOperandEvaluate(operands[0], srcCellRow, srcCellCol); + if (veval instanceof StringValueEval) { + StringValueEval sve = (StringValueEval) veval; + str = sve.getStringValue(); + if (str == null || str.trim().equals("")) { + return BlankEval.INSTANCE; + } + } + else if (veval instanceof NumberEval) { + NumberEval neval = (NumberEval) veval; + str = neval.getStringValue(); + } + else if (veval instanceof BlankEval) { + return BlankEval.INSTANCE; + } + } + + if (str != null) { + retval = new StringEval(str.trim()); + } + return retval; + } } diff --git a/src/scratchpad/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls b/src/scratchpad/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls index b2388f1d03..c6366ea1c4 100644 Binary files a/src/scratchpad/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls and b/src/scratchpad/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls differ diff --git a/src/scratchpad/testcases/org/apache/poi/hssf/record/formula/eval/TestEverything.java b/src/scratchpad/testcases/org/apache/poi/hssf/record/formula/eval/TestEverything.java index d1ab0c7076..9f65aabf26 100644 --- a/src/scratchpad/testcases/org/apache/poi/hssf/record/formula/eval/TestEverything.java +++ b/src/scratchpad/testcases/org/apache/poi/hssf/record/formula/eval/TestEverything.java @@ -30,20 +30,28 @@ public class TestEverything extends TestSuite { public static TestSuite suite() throws Exception { TestSuite suite = new TestSuite("Tests for OperationEval concrete implementation classes."); - suite.addTest(new GenericFormulaTestCase("D23")); - suite.addTest(new GenericFormulaTestCase("D27")); - suite.addTest(new GenericFormulaTestCase("D31")); - suite.addTest(new GenericFormulaTestCase("D35")); - suite.addTest(new GenericFormulaTestCase("D39")); - suite.addTest(new GenericFormulaTestCase("D43")); - suite.addTest(new GenericFormulaTestCase("D47")); - suite.addTest(new GenericFormulaTestCase("D51")); - suite.addTest(new GenericFormulaTestCase("D55")); - suite.addTest(new GenericFormulaTestCase("D59")); - suite.addTest(new GenericFormulaTestCase("D63")); - suite.addTest(new GenericFormulaTestCase("D67")); - suite.addTest(new GenericFormulaTestCase("D71")); - suite.addTest(new GenericFormulaTestCase("D75")); + suite.addTest(new GenericFormulaTestCase("D23")); // Add + suite.addTest(new GenericFormulaTestCase("D27")); // ConcatEval + suite.addTest(new GenericFormulaTestCase("D31")); // DivideEval + suite.addTest(new GenericFormulaTestCase("D35")); // EqualEval + suite.addTest(new GenericFormulaTestCase("D39")); // GreaterEqualEval + suite.addTest(new GenericFormulaTestCase("D43")); // GreaterThanEval + suite.addTest(new GenericFormulaTestCase("D47")); // LessEqualEval + suite.addTest(new GenericFormulaTestCase("D51")); // LessThanEval + suite.addTest(new GenericFormulaTestCase("D55")); // MultiplyEval + suite.addTest(new GenericFormulaTestCase("D59")); // NotEqualEval + suite.addTest(new GenericFormulaTestCase("D63")); // PowerEval + suite.addTest(new GenericFormulaTestCase("D67")); // SubtractEval + suite.addTest(new GenericFormulaTestCase("D71")); // UnaryMinusEval + suite.addTest(new GenericFormulaTestCase("D75")); // UnaryPlusEval + + suite.addTest(new GenericFormulaTestCase("D249")); // Concatenate + suite.addTest(new GenericFormulaTestCase("D741")); // Int + suite.addTest(new GenericFormulaTestCase("D1393")); // Trim + suite.addTest(new GenericFormulaTestCase("D1421")); // Upper + + // Add newly implemented formula functions here + return suite; } }