]> source.dussan.org Git - poi.git/commitdiff
Patch from Conor from bug #57747 - Add ISERR() function
authorNick Burch <nick@apache.org>
Fri, 24 Apr 2015 01:47:20 +0000 (01:47 +0000)
committerNick Burch <nick@apache.org>
Fri, 24 Apr 2015 01:47:20 +0000 (01:47 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1675741 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/formula/eval/FunctionEval.java
src/java/org/apache/poi/ss/formula/functions/LogicalFunction.java
src/testcases/org/apache/poi/ss/formula/functions/TestLogicalFunction.java [new file with mode: 0644]

index 315b769a478e43f306858055f7e6dc36861f0ee8..cf88b37a62dd628c854aafdef789e3476c0cba91 100644 (file)
@@ -154,6 +154,7 @@ public final class FunctionEval {
 
         retval[124] = TextFunction.FIND;
 
+        retval[126] = LogicalFunction.ISERR;
         retval[127] = LogicalFunction.ISTEXT;
         retval[128] = LogicalFunction.ISNUMBER;
         retval[129] = LogicalFunction.ISBLANK;
index ef18dab6454fd0e0d26383bcfd8998d5353cdf11..c4b3e6718752fda9737495be2f403e5ceec3676f 100644 (file)
@@ -90,6 +90,27 @@ public abstract class LogicalFunction extends Fixed1ArgFunction {
                }
        };
 
+    /**
+     * Implementation of Excel <tt>ISERR()</tt> function.<p/>
+     *
+     * <b>Syntax</b>:<br/>
+     * <b>ISERR</b>(<b>value</b>)<p/>
+     *
+     * <b>value</b>  The value to be tested<p/>
+     *
+     * Returns the logical value <tt>TRUE</tt> if value refers to any error value except
+     * <tt>'#N/A'</tt>; otherwise, it returns <tt>FALSE</tt>.
+     */
+    public static final Function ISERR = new LogicalFunction() {
+        @Override
+        protected boolean evaluate(ValueEval arg) {
+            if (arg instanceof ErrorEval) {
+                return arg != ErrorEval.NA;
+            }
+            return false;
+        }
+    };
+
        /**
         * Implementation for Excel ISNA() function.<p/>
         *
diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestLogicalFunction.java b/src/testcases/org/apache/poi/ss/formula/functions/TestLogicalFunction.java
new file mode 100644 (file)
index 0000000..faafdac
--- /dev/null
@@ -0,0 +1,88 @@
+package org.apache.poi.ss.formula.functions;\r
+\r
+import java.io.IOException;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
+import org.apache.poi.ss.usermodel.Cell;\r
+import org.apache.poi.ss.usermodel.CellValue;\r
+import org.apache.poi.ss.usermodel.FormulaEvaluator;\r
+import org.apache.poi.ss.usermodel.Row;\r
+import org.apache.poi.ss.usermodel.Sheet;\r
+import org.apache.poi.ss.usermodel.Workbook;\r
+import org.apache.poi.ss.util.CellReference;\r
+\r
+/**\r
+ * LogicalFunction unit tests.\r
+ */\r
+public class TestLogicalFunction extends TestCase {\r
+\r
+    private FormulaEvaluator evaluator;\r
+    private Row row3;\r
+    private Cell cell1;\r
+    private Cell cell2;\r
+\r
+    @Override\r
+    public void setUp() throws IOException {\r
+        Workbook wb = new HSSFWorkbook();\r
+        try {\r
+            buildWorkbook(wb);\r
+        } finally {\r
+            wb.close();\r
+        }\r
+    }\r
+\r
+    private void buildWorkbook(Workbook wb) {\r
+        Sheet sh = wb.createSheet();\r
+        Row row1 = sh.createRow(0);\r
+        Row row2 = sh.createRow(1);\r
+        row3 = sh.createRow(2);\r
+\r
+        row1.createCell(0, Cell.CELL_TYPE_NUMERIC);\r
+        row1.createCell(1, Cell.CELL_TYPE_NUMERIC);\r
+\r
+        row2.createCell(0, Cell.CELL_TYPE_NUMERIC);\r
+        row2.createCell(1, Cell.CELL_TYPE_NUMERIC);\r
+\r
+        row3.createCell(0);\r
+        row3.createCell(1);\r
+\r
+        CellReference a1 = new CellReference("A1");\r
+        CellReference a2 = new CellReference("A2");\r
+        CellReference b1 = new CellReference("B1");\r
+        CellReference b2 = new CellReference("B2");\r
+\r
+        sh.getRow(a1.getRow()).getCell(a1.getCol()).setCellValue(35);\r
+        sh.getRow(a2.getRow()).getCell(a2.getCol()).setCellValue(0);\r
+        sh.getRow(b1.getRow()).getCell(b1.getCol()).setCellFormula("A1/A2");\r
+        sh.getRow(b2.getRow()).getCell(b2.getCol()).setCellFormula("NA()");\r
+\r
+        evaluator = wb.getCreationHelper().createFormulaEvaluator();\r
+    }\r
+\r
+    public void testIsErr() {\r
+        cell1 = row3.createCell(0);\r
+        cell1.setCellFormula("ISERR(B1)"); // produces #DIV/0!\r
+        cell2 = row3.createCell(1);\r
+        cell2.setCellFormula("ISERR(B2)"); // produces #N/A\r
+\r
+        CellValue cell1Value = evaluator.evaluate(cell1);\r
+        CellValue cell2Value = evaluator.evaluate(cell2);\r
+\r
+        assertEquals(true, cell1Value.getBooleanValue());\r
+        assertEquals(false, cell2Value.getBooleanValue());\r
+    }\r
+\r
+    public void testIsError() {\r
+        cell1 = row3.createCell(0);\r
+        cell1.setCellFormula("ISERROR(B1)"); // produces #DIV/0!\r
+        cell2 = row3.createCell(1);\r
+        cell2.setCellFormula("ISERROR(B2)"); // produces #N/A\r
+\r
+        CellValue cell1Value = evaluator.evaluate(cell1);\r
+        CellValue cell2Value = evaluator.evaluate(cell2);\r
+\r
+        assertEquals(true, cell1Value.getBooleanValue());\r
+        assertEquals(true, cell2Value.getBooleanValue());\r
+    }\r
+}\r