<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>
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 {
}
}
}
+
+ /**
+ * 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());
+ }
}