Index: src/java/org/apache/poi/ss/formula/FormulaParser.java =================================================================== --- src/java/org/apache/poi/ss/formula/FormulaParser.java (revision 1174350) +++ src/java/org/apache/poi/ss/formula/FormulaParser.java (working copy) @@ -121,7 +121,7 @@ private ParseNode _rootNode; - private static char TAB = '\t'; + private final static char TAB = '\t'; /** * Lookahead Character. @@ -229,7 +229,7 @@ /** Recognize White Space */ private static boolean IsWhite( char c) { - return c ==' ' || c== TAB; + return c ==' ' || c== TAB || c == '\r' || c == '\n'; } /** Skip Over Leading White Space */ Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java =================================================================== --- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java (revision 1174350) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java (working copy) @@ -34,8 +34,6 @@ 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 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 { Index: src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java =================================================================== --- src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java (revision 1174350) +++ src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java (working copy) @@ -622,7 +622,7 @@ /** * 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); @@ -631,10 +631,16 @@ assertEquals(3.0, c.getNumericCellValue()); FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator(); - formulaEvaluator.evaluateFormulaCell(c); + formulaEvaluator.evaluateFormulaCell(c); 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()); } /**