]> source.dussan.org Git - poi.git/commitdiff
Support for the Trim function, and a little enhancement to the formula evaluation...
authorNick Burch <nick@apache.org>
Tue, 11 Dec 2007 13:03:53 +0000 (13:03 +0000)
committerNick Burch <nick@apache.org>
Tue, 11 Dec 2007 13:03:53 +0000 (13:03 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@603233 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Trim.java
src/scratchpad/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls
src/scratchpad/testcases/org/apache/poi/hssf/record/formula/eval/TestEverything.java

index 69b3ce6f55c51fba79e1c0062bb1d9e9b1b5ae54..5e9d91c7cc7682eb40c6a5f0715d5697dd4c83d6 100644 (file)
 * 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 &lt; wilson at c bio dot msk cc dot org &gt;
+ */
+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;
+    }
 }
index b2388f1d032309609e1e4b14c3510545a76aee86..c6366ea1c4314965ccf43d14699a2abf31e419bc 100644 (file)
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
index d1ab0c7076b7d06ff98ab5f79edfb77da1b9bcd4..9f65aabf26506de2ecf00378a301fc98054c9dbe 100644 (file)
@@ -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;
     }
 }