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
This commit is contained in:
Nick Burch 2010-12-13 05:39:39 +00:00
parent e8b9fa6725
commit 3d74ebf836
4 changed files with 34 additions and 1 deletions

View 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>

View 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 {

View 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());
}
}

Binary file not shown.