From 3aec436a34da31b247a1449225c75f3157a1bf63 Mon Sep 17 00:00:00 2001 From: Vladislav Galas Date: Wed, 2 Jan 2019 20:45:52 +0000 Subject: [PATCH] Bug 62307: made Cell#getNumericCellValue() behavior consistent across HSSF/XSSF/SXSSF.\nAll three implementations throw ISE when trying to get numeric value from a boolean-valued cell, have it a formula set or not. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1850207 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/poi/xssf/usermodel/XSSFCell.java | 12 ++++++------ .../org/apache/poi/ss/usermodel/BaseTestCell.java | 9 ++++++++- 2 files changed, 14 insertions(+), 7 deletions(-) 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 a9490694c0..a1a66bf653 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -285,12 +285,10 @@ public final class XSSFCell implements Cell { */ @Override public double getNumericCellValue() { - CellType cellType = getCellType(); - switch(cellType) { + CellType valueType = isFormulaCell() ? getCachedFormulaResultType() : getCellType(); + switch(valueType) { case BLANK: return 0.0; - case FORMULA: - // fall-through case NUMERIC: if(_cell.isSetV()) { String v = _cell.getV(); @@ -305,8 +303,10 @@ public final class XSSFCell implements Cell { } else { return 0.0; } + case FORMULA: + throw new AssertionError(); default: - throw typeMismatch(CellType.NUMERIC, cellType, false); + throw typeMismatch(CellType.NUMERIC, valueType, false); } } @@ -1361,4 +1361,4 @@ public final class XSSFCell implements Cell { } } - \ No newline at end of file + diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index 304faaadaf..fb8f81b065 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -74,7 +74,7 @@ public abstract class BaseTestCell { assertEquals(CellType.BOOLEAN, cell.getCellType()); cell.setCellValue(true); assertTrue(cell.getBooleanCellValue()); - assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.STRING, + assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.STRING, CellType.BOOLEAN, CellType.FORMULA, CellType.ERROR); cell.setCellValue(factory.createRichTextString("Foo")); @@ -1133,4 +1133,11 @@ public abstract class BaseTestCell { assertEquals(CellType.FORMULA, cell.getCellType()); } } + + @Test + public void testGetNumericCellValueOnABlankCellReturnsZero() { + Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0); + assertEquals(CellType.BLANK, cell.getCellType()); + assertEquals(0, cell.getNumericCellValue(), 0); + } }