From 4669ec9f5dad0bcd81c8a2b41439be2e283f0fd9 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sat, 26 Sep 2009 10:41:01 +0000 Subject: [PATCH] fixed XSSFCell.getStringCellValue() to properly handle cached formula results git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@819106 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/xssf/usermodel/XSSFCell.java | 16 +++-- .../poi/xssf/usermodel/TestXSSFCell.java | 61 ++++++++++++++++++ test-data/spreadsheet/47889.xlsx | Bin 0 -> 3555 bytes 4 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 test-data/spreadsheet/47889.xlsx diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index d44bc902ad..446790656c 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,6 +33,7 @@ + 47889 - fixed XSSFCell.getStringCellValue() to properly handle cached formula results 47747 - fixed logic for locating shared formula records diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index e76f22a97b..aee0405b2f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -249,6 +249,9 @@ public final class XSSFCell implements Cell { } else { rt = new XSSFRichTextString(""); } + } else if (_cell.getT() == STCellType.STR) { + //cached formula value + rt = new XSSFRichTextString(_cell.isSetV() ? _cell.getV() : ""); } else { if (_cell.isSetV()) { int idx = Integer.parseInt(_cell.getV()); @@ -411,12 +414,15 @@ public final class XSSFCell implements Cell { /** * Return the cell's style. * - * @return the cell's style. Always not-null. Default cell style has zero index and can be obtained as - * workbook.getCellStyleAt(0) + * @return the cell's style. */ public XSSFCellStyle getCellStyle() { - long idx = _cell.isSetS() ? _cell.getS() : 0; - return _stylesSource.getStyleAt((int)idx); + XSSFCellStyle style = null; + if(_stylesSource.getNumCellStyles() > 0){ + long idx = _cell.isSetS() ? _cell.getS() : 0; + style = _stylesSource.getStyleAt((int)idx); + } + return style; } /** @@ -629,7 +635,7 @@ public final class XSSFCell implements Cell { private void setBlank(){ CTCell blank = CTCell.Factory.newInstance(); blank.setR(_cell.getR()); - blank.setS(_cell.getS()); + if(_cell.isSetS()) blank.setS(_cell.getS()); _cell.set(blank); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java index 194a983e25..e097bfd140 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -21,6 +21,7 @@ import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.model.SharedStringsTable; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; /** * @author Yegor Kozlov @@ -105,4 +106,64 @@ public final class TestXSSFCell extends BaseTestCell { assertEquals(0, sst.getCount()); assertEquals(XSSFCell.CELL_TYPE_BLANK, cell_1.getCellType()); } + + public void testFormulaString() { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFCell cell = wb.createSheet().createRow(0).createCell(0); + CTCell ctCell = cell.getCTCell(); //low-level bean holding cell's xml + + cell.setCellFormula("A2"); + assertEquals(XSSFCell.CELL_TYPE_FORMULA, cell.getCellType()); + //the value is not set and cell's type='N' which means blank + assertEquals(STCellType.N, ctCell.getT()); + + //set cached formula value + cell.setCellValue("t='str'"); + //we are still of 'formula' type + assertEquals(XSSFCell.CELL_TYPE_FORMULA, cell.getCellType()); + //cached formula value is set and cell's type='STR' + assertEquals(STCellType.STR, ctCell.getT()); + assertEquals("t='str'", cell.getStringCellValue()); + + //now remove the formula, the cached formula result remains + cell.setCellFormula(null); + assertEquals(XSSFCell.CELL_TYPE_STRING, cell.getCellType()); + assertEquals(STCellType.STR, ctCell.getT()); + //the line below failed prior to fix of Bug #47889 + assertEquals("t='str'", cell.getStringCellValue()); + + //revert to a blank cell + cell.setCellValue((String)null); + assertEquals(XSSFCell.CELL_TYPE_BLANK, cell.getCellType()); + assertEquals(STCellType.N, ctCell.getT()); + assertEquals("", cell.getStringCellValue()); + } + + /** + * Bug 47889: problems when calling XSSFCell.getStringCellValue() on a workbook created in Gnumeric + */ + public void test47889() { + XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("47889.xlsx"); + XSSFSheet sh = wb.getSheetAt(0); + + XSSFCell cell; + + //try a string cell + cell = sh.getRow(0).getCell(0); + assertEquals(XSSFCell.CELL_TYPE_STRING, cell.getCellType()); + assertEquals("a", cell.getStringCellValue()); + assertEquals("a", cell.toString()); + //Gnumeric produces spreadsheets without styles + //make sure we return null for that instead of throwing OutOfBounds + assertEquals(null, cell.getCellStyle()); + + //try a numeric cell + cell = sh.getRow(1).getCell(0); + assertEquals(XSSFCell.CELL_TYPE_NUMERIC, cell.getCellType()); + assertEquals(1.0, cell.getNumericCellValue()); + assertEquals("1.0", cell.toString()); + //Gnumeric produces spreadsheets without styles + //make sure we return null for that instead of throwing OutOfBounds + assertEquals(null, cell.getCellStyle()); + } } diff --git a/test-data/spreadsheet/47889.xlsx b/test-data/spreadsheet/47889.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..d4b23abc2163c18e5fb154dc2393b8d6708a4324 GIT binary patch literal 3555 zcmeHKcT`h(7ETC+8iEu-K#^vlgepy`11JFm5s)I06`}|TNJ8&jASfaww4sAEL5wsf zR8awi&;rs&klqH7Py{x?Wo6u%IlKGcp0oFzciubiyx)EA-tW8jem_H95H&l1j(n7) z$(;urGA{DHr=x_2v#SHj4v9pgB=#>-FweV=F(%8-gOV&e9XI~Axh<))(IueK7i4() zuEmSWZKpWKXD%P#sbp?1P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