From: Nick Burch Date: Mon, 5 Dec 2011 00:52:39 +0000 (+0000) Subject: Patch from Marcel May from bug #51875 - More XSSF formula new-line support X-Git-Tag: REL_3_8_BETA5~12 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=36f27c7385b67da5f1261c952982d8d09999068f;p=poi.git Patch from Marcel May from bug #51875 - More XSSF formula new-line support git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1210299 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index ffee4df0d6..6db3227e44 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51875 - More XSSF new-line in formula support POIFS EntryUtils.copyNodes(POFS,POIFS) now uses FilteringDirectoryNode, so can exclude from copying nodes not just directly under the root POIFS Helper FilteringDirectoryNode, which wraps a DirectoryEntry and allows certain parts to be ignored 52209 - fixed inserting multiple pictures in XSLF diff --git a/src/java/org/apache/poi/ss/formula/FormulaParser.java b/src/java/org/apache/poi/ss/formula/FormulaParser.java index f9f3ca0c77..3e2f07cc49 100644 --- a/src/java/org/apache/poi/ss/formula/FormulaParser.java +++ b/src/java/org/apache/poi/ss/formula/FormulaParser.java @@ -121,7 +121,9 @@ public final class FormulaParser { private ParseNode _rootNode; - private static char TAB = '\t'; + private final static char TAB = '\t'; // HSSF + XSSF + private final static char CR = '\r'; // Normally just XSSF + private final static char LF = '\n'; // Normally just XSSF /** * Lookahead Character. @@ -229,7 +231,7 @@ public final class FormulaParser { /** Recognize White Space */ private static boolean IsWhite( char c) { - return c ==' ' || c== TAB; + return c ==' ' || c== TAB || c == CR || c == LF; } /** Skip Over Leading White Space */ diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java index e5bec593ba..e9ba3c7bb1 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java @@ -34,8 +34,6 @@ import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.xssf.model.IndexedUDFFinder; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName; -import java.util.HashMap; - /** * Internal POI use only * @@ -147,26 +145,12 @@ public final class XSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E public Ptg[] getFormulaTokens(EvaluationCell evalCell) { XSSFCell cell = ((XSSFEvaluationCell)evalCell).getXSSFCell(); XSSFEvaluationWorkbook frBook = XSSFEvaluationWorkbook.create(_uBook); - String formulaText = cleanXSSFFormulaText(cell.getCellFormula()); - return FormulaParser.parse(formulaText, frBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet())); + return FormulaParser.parse(cell.getCellFormula(), 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 { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 4467dc46a4..d163221d52 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -623,7 +623,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { /** * Newlines are valid characters in a formula */ - public void test50440() throws Exception { + public void test50440And51875() throws Exception { Workbook wb = XSSFTestDataSamples.openSampleWorkbook("NewlineInFormulas.xlsx"); Sheet s = wb.getSheetAt(0); Cell c = s.getRow(0).getCell(0); @@ -636,6 +636,12 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals("SUM(\n1,2\n)", c.getCellFormula()); assertEquals(3.0, c.getNumericCellValue()); + + // For 51875 + Cell b3 = s.getRow(2).getCell(1); + formulaEvaluator.evaluateFormulaCell(b3); + assertEquals("B1+B2", b3.getCellFormula()); // The newline is lost for shared formulas + assertEquals(3.0, b3.getNumericCellValue()); } /** diff --git a/test-data/spreadsheet/NewlineInFormulas.xlsx b/test-data/spreadsheet/NewlineInFormulas.xlsx index 6a3ac949e9..18dec0aa5c 100644 Binary files a/test-data/spreadsheet/NewlineInFormulas.xlsx and b/test-data/spreadsheet/NewlineInFormulas.xlsx differ