]> source.dussan.org Git - poi.git/commitdiff
Fix bug #50440 - Support evaluating formulas with newlines in them, which XSSF may...
authorNick Burch <nick@apache.org>
Mon, 13 Dec 2010 05:39:39 +0000 (05:39 +0000)
committerNick Burch <nick@apache.org>
Mon, 13 Dec 2010 05:39:39 +0000 (05:39 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1045021 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
test-data/spreadsheet/NewlineInFormulas.xlsx [new file with mode: 0644]

index 946e24ea7e8ae22c8a5025bbc5ebb85cd21712a9..05d4d10bd1939ea4d8475c25774ff5c1a12f4de2 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta1" date="2010-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">50440 - Support evaluating formulas with newlines in them, which XSSF may have (but HSSF may not)</action>
            <action dev="POI-DEVELOPERS" type="add">Added inline string support to XSSF EventModel</action>
            <action dev="POI-DEVELOPERS" type="fix">50246 - Properly position GutsRecord when reading HSSF workbooks</action>
            <action dev="POI-DEVELOPERS" type="add">48539 - Added implementation for MROUND(), VAR() and VARP()</action>
index 9535504506bb910eafdaa8737ee15c8de565cb80..e5bec593baa8c28e74fcfe2fca15b95daff5693d 100644 (file)
@@ -147,12 +147,26 @@ public final class XSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E
        public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
                XSSFCell cell = ((XSSFEvaluationCell)evalCell).getXSSFCell();
                XSSFEvaluationWorkbook frBook = XSSFEvaluationWorkbook.create(_uBook);
-               return FormulaParser.parse(cell.getCellFormula(), frBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
+               String formulaText = cleanXSSFFormulaText(cell.getCellFormula());
+               return FormulaParser.parse(formulaText, frBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
        }
 
     public UDFFinder getUDFFinder(){
         return _uBook.getUDFFinder();
     }
+    
+   /**
+    * XSSF allows certain extra textual characters in the formula that
+    *  HSSF does not. As these can't be composed down to HSSF-compatible
+    *  Ptgs, this method strips them out for us.
+    */
+   private String cleanXSSFFormulaText(String text) {
+      // Newlines are allowed in XSSF
+      text = text.replaceAll("\\n", "").replaceAll("\\r", "");
+      
+      // All done with cleaning
+      return text;
+   }
 
        private static final class Name implements EvaluationName {
 
index 3d9d69297e346b97e1c72b64356ac65d358d3dad..b45a5df6c8b2cd071f451b263518499e820da6aa 100644 (file)
@@ -602,4 +602,22 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
             }
         }
     }
+    
+    /**
+     * Newlines are valid characters in a formula
+     */
+    public void test50440() throws Exception {
+       Workbook wb = XSSFTestDataSamples.openSampleWorkbook("NewlineInFormulas.xlsx");
+       Sheet s = wb.getSheetAt(0);
+       Cell c = s.getRow(0).getCell(0);
+       
+       assertEquals("SUM(\n1,2\n)", c.getCellFormula());
+       assertEquals(3.0, c.getNumericCellValue());
+       
+       FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
+       formulaEvaluator.evaluateFormulaCell(c);
+       
+       assertEquals("SUM(\n1,2\n)", c.getCellFormula());
+       assertEquals(3.0, c.getNumericCellValue());
+    }
 }
diff --git a/test-data/spreadsheet/NewlineInFormulas.xlsx b/test-data/spreadsheet/NewlineInFormulas.xlsx
new file mode 100644 (file)
index 0000000..6a3ac94
Binary files /dev/null and b/test-data/spreadsheet/NewlineInFormulas.xlsx differ