While working on bug 61148 I found that getCellType() and setCellErrorValue() in SXSSFCell are doing different things than the ones in XSSFCell, tried to fix this by mimicking functionality of XSSF in SXSSF as well.

Reformating some small things as well.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808874 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-09-19 12:47:13 +00:00
parent 7d8f31d4a4
commit 6bdce2bfd7
3 changed files with 84 additions and 28 deletions

View File

@ -124,14 +124,21 @@ public class SXSSFCell implements Cell {
ensureType(cellType);
}
private boolean isFormulaCell() {
return _value instanceof FormulaValue;
}
/**
* Return the cell type.
*
* @return the cell type
*/
@Override
public CellType getCellType()
{
public CellType getCellType() {
if (isFormulaCell()) {
return CellType.FORMULA;
}
return _value.getType();
}
@ -158,7 +165,7 @@ public class SXSSFCell implements Cell {
*/
@Override
public CellType getCachedFormulaResultType() {
if (_value.getType() != CellType.FORMULA) {
if (!isFormulaCell()) {
throw new IllegalStateException("Only formula cells have cached results");
}
@ -497,14 +504,19 @@ public class SXSSFCell implements Cell {
* @see org.apache.poi.ss.usermodel.FormulaError
*/
@Override
public void setCellErrorValue(byte value)
{
ensureType(CellType.ERROR);
if(_value.getType()==CellType.FORMULA)
public void setCellErrorValue(byte value) {
// for formulas, we want to keep the type and only have an ERROR as formula value
if(_value.getType()==CellType.FORMULA) {
// ensure that the type is "ERROR"
setFormulaType(CellType.ERROR);
// populate the value
((ErrorFormulaValue) _value).setPreEvaluatedValue(value);
else
} else {
ensureType(CellType.ERROR);
((ErrorValue) _value).setValue(value);
}
}
/**
* Get the value of the cell as a boolean.

View File

@ -20,6 +20,7 @@
package org.apache.poi.xssf.streaming;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
@ -71,6 +72,7 @@ public class TestSXSSFCell extends BaseTestXCell {
XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);
CTRst is = xCell.getCTCell().getIs();
assertNotNull(is);
XmlCursor c = is.newCursor();
c.toNextToken();
String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));

View File

@ -1014,4 +1014,46 @@ public abstract class BaseTestCell {
wb.close();
}
@Test
public void testSetErrorValue() throws Exception {
try (Workbook wb = _testDataProvider.createWorkbook()) {
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellFormula("A2");
cell.setCellErrorValue(FormulaError.NAME.getCode());
assertEquals(CellType.FORMULA, cell.getCellType());
assertEquals(CellType.ERROR, cell.getCachedFormulaResultType());
assertEquals("A2", cell.getCellFormula());
try {
cell.getNumericCellValue();
fail("Should catch exception here");
} catch (IllegalStateException e) {
// expected here
}
try {
cell.getStringCellValue();
fail("Should catch exception here");
} catch (IllegalStateException e) {
// expected here
}
try {
cell.getRichStringCellValue();
fail("Should catch exception here");
} catch (IllegalStateException e) {
// expected here
}
try {
cell.getDateCellValue();
fail("Should catch exception here");
} catch (IllegalStateException e) {
// expected here
}
assertEquals(FormulaError.NAME.getCode(), cell.getErrorCellValue());
assertNull(cell.getHyperlink());
}
}
}