poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fannin...@apache.org
Subject svn commit: r1830685 - in /poi/trunk/src: java/org/apache/poi/ss/formula/eval/ java/org/apache/poi/ss/formula/functions/ testcases/org/apache/poi/ss/formula/functions/
Date Tue, 01 May 2018 14:47:30 GMT
Author: fanningpj
Date: Tue May  1 14:47:29 2018
New Revision: 1830685

URL: http://svn.apache.org/viewvc?rev=1830685&view=rev
Log:
[githib-107] add areas function support. Thanks to Inji Hanbin. This closes #107

Added:
    poi/trunk/src/java/org/apache/poi/ss/formula/functions/Areas.java   (with props)
    poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestAreas.java   (with props)
Modified:
    poi/trunk/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java
    poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/AllIndividualFunctionEvaluationTests.java

Modified: poi/trunk/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java?rev=1830685&r1=1830684&r2=1830685&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java Tue May  1 14:47:29
2018
@@ -139,6 +139,7 @@ public final class FunctionEval {
         retval[73] = CalendarFieldFunction.SECOND;
         retval[74] = new Now();
         // 75: AREAS
+        retval[75] = new Areas();
         retval[76] = new Rows();
         retval[77] = new Columns();
         retval[FunctionID.OFFSET] = new Offset(); //nominally 78

Added: poi/trunk/src/java/org/apache/poi/ss/formula/functions/Areas.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/Areas.java?rev=1830685&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/Areas.java (added)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/Areas.java Tue May  1 14:47:29
2018
@@ -0,0 +1,35 @@
+package org.apache.poi.ss.formula.functions;
+
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.RefListEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
+import org.apache.poi.ss.formula.ptg.NumberPtg;
+
+/**
+ * Returns the number of areas in a reference. An area is a range of contiguous cells or
a single cell.
+ *
+ * @author Loopbing (loopbing@gmail.com)
+ */
+public final class Areas implements Function {
+
+    @Override
+    public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
+        if (args.length == 0) {
+            return ErrorEval.VALUE_INVALID;
+        }
+        try {
+            ValueEval valueEval = args[0];
+            int result = 1;
+            if (valueEval instanceof RefListEval) {
+                RefListEval refListEval = (RefListEval) valueEval;
+                result = refListEval.getList().size();
+            }
+            NumberEval numberEval = new NumberEval(new NumberPtg(result));
+            return numberEval;
+        } catch (Exception e) {
+            return ErrorEval.VALUE_INVALID;
+        }
+
+    }
+}

Propchange: poi/trunk/src/java/org/apache/poi/ss/formula/functions/Areas.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/AllIndividualFunctionEvaluationTests.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/AllIndividualFunctionEvaluationTests.java?rev=1830685&r1=1830684&r2=1830685&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/AllIndividualFunctionEvaluationTests.java
(original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/AllIndividualFunctionEvaluationTests.java
Tue May  1 14:47:29 2018
@@ -57,6 +57,7 @@ import org.junit.runners.Suite;
     TestValue.class,
     TestXYNumericFunction.class,
     TestAddress.class,
+    TestAreas.class,
     TestClean.class
 })
 public class AllIndividualFunctionEvaluationTests {

Added: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestAreas.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestAreas.java?rev=1830685&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestAreas.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestAreas.java Tue May  1
14:47:29 2018
@@ -0,0 +1,42 @@
+package org.apache.poi.ss.formula.functions;
+
+import junit.framework.TestCase;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.CellValue;
+
+public final class TestAreas extends TestCase {
+
+    public void testAreas() {
+        HSSFWorkbook wb = new HSSFWorkbook();
+        HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
+        HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+
+        String formulaText = "AREAS(B1)";
+        confirmResult(fe, cell, formulaText,1.0);
+
+        formulaText = "AREAS(B2:D4)";
+        confirmResult(fe, cell, formulaText,1.0);
+
+        formulaText = "AREAS((B2:D4,E5,F6:I9))";
+        confirmResult(fe, cell, formulaText,3.0);
+
+        formulaText = "AREAS((B2:D4,E5,C3,E4))";
+        confirmResult(fe, cell, formulaText,4.0);
+
+        formulaText = "AREAS((I9))";
+        confirmResult(fe, cell, formulaText,1.0);
+    }
+
+    private static void confirmResult(HSSFFormulaEvaluator fe, HSSFCell cell, String formulaText,Double
expectedResult) {
+        cell.setCellFormula(formulaText);
+        fe.notifyUpdateCell(cell);
+        CellValue result = fe.evaluate(cell);
+        assertEquals(result.getCellTypeEnum(), CellType.NUMERIC);
+        assertEquals(expectedResult, result.getNumberValue());
+    }
+}

Propchange: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestAreas.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message