From: Nick Burch Date: Mon, 13 Dec 2010 05:39:39 +0000 (+0000) Subject: Fix bug #50440 - Support evaluating formulas with newlines in them, which XSSF may... X-Git-Tag: REL_3_8_BETA1~99 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=143cab518a1d8509d63dce39a26a87d4ae2be018;p=poi.git 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 --- 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 0000000000..6a3ac949e9 Binary files /dev/null and b/test-data/spreadsheet/NewlineInFormulas.xlsx differ