]> source.dussan.org Git - poi.git/commitdiff
Bug 53476 - Support Complex Name in formulas
authorYegor Kozlov <yegor@apache.org>
Wed, 27 Jun 2012 14:50:22 +0000 (14:50 +0000)
committerYegor Kozlov <yegor@apache.org>
Wed, 27 Jun 2012 14:50:22 +0000 (14:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1354550 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java

index c05e507ff64d44e85922112eb5041a22a11083a3..6dbea32ac5f67dc1cfbfa68504f7b4025f93e5b0 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.9-beta1" date="2012-??-??">
+          <action dev="poi-developers" type="add">53476 - Support Complex Name in formulas </action>
           <action dev="poi-developers" type="fix">53414 - properly update sheet dimensions when adding column </action>
           <action dev="poi-developers" type="add">Add File based constructor to OPCPackage, alongside existing String one (which constructed a File from the string internally)</action>
           <action dev="poi-developers" type="fix">53389 - Handle formatting General and @ formats even if a locale is prefixed to them</action>
index 4e45a76a393ec044a891cb78f02c60016ddf89f7..79676d6a07ae4b71fbf008b1cf5a49476b0e47bb 100644 (file)
@@ -637,10 +637,10 @@ public final class WorkbookEvaluator {
      * YK: Used by OperationEvaluationContext to resolve indirect names.
      */
        /*package*/ ValueEval evaluateNameFormula(Ptg[] ptgs, OperationEvaluationContext ec) {
-               if (ptgs.length > 1) {
-                       throw new RuntimeException("Complex name formulas not supported yet");
-               }
-               return getEvalForPtg(ptgs[0], ec);
+    if (ptgs.length == 1) {
+      return getEvalForPtg(ptgs[0], ec);
+    }
+         return evaluateFormula(ec, ptgs);
        }
 
        /**
index 8303e08600917f59ebf1492929af0e00b3832372..712335f85cdb2e4b779802f7fb56e376fa0497ea 100644 (file)
@@ -42,6 +42,9 @@ import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellValue;
 import org.apache.poi.ss.usermodel.ErrorConstants;
 import org.apache.poi.ss.usermodel.FormulaEvaluator;
+import org.apache.poi.ss.usermodel.Name;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 
 /**
@@ -235,4 +238,47 @@ public class TestWorkbookEvaluator extends TestCase {
                assertEquals(Cell.CELL_TYPE_ERROR, cv.getCellType());
                assertEquals(ErrorEval.CIRCULAR_REF_ERROR.getErrorCode(), cv.getErrorValue());
        }
+       
+
+  /**
+   * formulas with defined names.
+   */
+  public void testNamesInFormulas() {
+    Workbook wb = new HSSFWorkbook();
+    Sheet sheet = wb.createSheet("Sheet1");
+    
+    Name name1 = wb.createName();
+    name1.setNameName("aConstant");
+    name1.setRefersToFormula("3.14");
+
+    Name name2 = wb.createName();
+    name2.setNameName("aFormula");
+    name2.setRefersToFormula("SUM(Sheet1!$A$1:$A$3)");
+
+    Name name3 = wb.createName();
+    name3.setNameName("aSet");
+    name3.setRefersToFormula("Sheet1!$A$2:$A$4");
+
+    
+    Row row0 = sheet.createRow(0);
+    Row row1 = sheet.createRow(1);
+    Row row2 = sheet.createRow(2);
+    Row row3 = sheet.createRow(3);
+    row0.createCell(0).setCellValue(2);
+    row1.createCell(0).setCellValue(5);
+    row2.createCell(0).setCellValue(3);
+    row3.createCell(0).setCellValue(7);
+    
+    row0.createCell(2).setCellFormula("aConstant");
+    row1.createCell(2).setCellFormula("aFormula");
+    row2.createCell(2).setCellFormula("SUM(aSet)");
+    row3.createCell(2).setCellFormula("aConstant+aFormula+SUM(aSet)");
+
+    FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
+    assertEquals(3.14, fe.evaluate(row0.getCell(2)).getNumberValue());
+    assertEquals(10.0, fe.evaluate(row1.getCell(2)).getNumberValue());
+    assertEquals(15.0, fe.evaluate(row2.getCell(2)).getNumberValue());
+    assertEquals(28.14, fe.evaluate(row3.getCell(2)).getNumberValue());
+  }
+       
 }