From 143cab518a1d8509d63dce39a26a87d4ae2be018 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 13 Dec 2010 05:39:39 +0000 Subject: [PATCH] Fix bug #50440 - Support evaluating formulas with newlines in them, which XSSF may have (but HSSF may not) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1045021 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../usermodel/XSSFEvaluationWorkbook.java | 16 +++++++++++++++- .../poi/xssf/usermodel/TestXSSFBugs.java | 18 ++++++++++++++++++ test-data/spreadsheet/NewlineInFormulas.xlsx | Bin 0 -> 8703 bytes 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test-data/spreadsheet/NewlineInFormulas.xlsx diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 946e24ea7e..05d4d10bd1 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 50440 - Support evaluating formulas with newlines in them, which XSSF may have (but HSSF may not) Added inline string support to XSSF EventModel 50246 - Properly position GutsRecord when reading HSSF workbooks 48539 - Added implementation for MROUND(), VAR() and VARP() 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 9535504506..e5bec593ba 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java @@ -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 { 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 3d9d69297e..b45a5df6c8 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -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 index 0000000000000000000000000000000000000000..6a3ac949e9f0416d9986f3e0a5c5c18fb13ada42 GIT binary patch literal 8703 zcmeHMbyQSq*Po%gB!@2PMnOuth8ntI28Ql#q*Gc#5Tqo$q=+<*AP54|DWOP8N=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