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); ensureType(cellType);
} }
private boolean isFormulaCell() {
return _value instanceof FormulaValue;
}
/** /**
* Return the cell type. * Return the cell type.
* *
* @return the cell type * @return the cell type
*/ */
@Override @Override
public CellType getCellType() public CellType getCellType() {
{ if (isFormulaCell()) {
return CellType.FORMULA;
}
return _value.getType(); return _value.getType();
} }
@ -158,7 +165,7 @@ public class SXSSFCell implements Cell {
*/ */
@Override @Override
public CellType getCachedFormulaResultType() { public CellType getCachedFormulaResultType() {
if (_value.getType() != CellType.FORMULA) { if (!isFormulaCell()) {
throw new IllegalStateException("Only formula cells have cached results"); throw new IllegalStateException("Only formula cells have cached results");
} }
@ -497,13 +504,18 @@ public class SXSSFCell implements Cell {
* @see org.apache.poi.ss.usermodel.FormulaError * @see org.apache.poi.ss.usermodel.FormulaError
*/ */
@Override @Override
public void setCellErrorValue(byte value) public void setCellErrorValue(byte value) {
{ // for formulas, we want to keep the type and only have an ERROR as formula value
ensureType(CellType.ERROR); if(_value.getType()==CellType.FORMULA) {
if(_value.getType()==CellType.FORMULA) // ensure that the type is "ERROR"
((ErrorFormulaValue)_value).setPreEvaluatedValue(value); setFormulaType(CellType.ERROR);
else
((ErrorValue)_value).setValue(value); // populate the value
((ErrorFormulaValue) _value).setPreEvaluatedValue(value);
} else {
ensureType(CellType.ERROR);
((ErrorValue) _value).setValue(value);
}
} }
/** /**
@ -827,13 +839,13 @@ public class SXSSFCell implements Cell {
{ {
if(_value.getType()!=CellType.STRING if(_value.getType()!=CellType.STRING
||((StringValue)_value).isRichText()) ||((StringValue)_value).isRichText())
_value=new PlainStringValue(); _value = new PlainStringValue();
} }
/*package*/ void ensureRichTextStringType() /*package*/ void ensureRichTextStringType()
{ {
if(_value.getType()!=CellType.STRING if(_value.getType()!=CellType.STRING
||!((StringValue)_value).isRichText()) ||!((StringValue)_value).isRichText())
_value=new RichTextValue(); _value = new RichTextValue();
} }
/*package*/ void ensureType(CellType type) /*package*/ void ensureType(CellType type)
{ {
@ -879,7 +891,7 @@ public class SXSSFCell implements Cell {
{ {
case NUMERIC: case NUMERIC:
{ {
_value=new NumericValue(); _value = new NumericValue();
break; break;
} }
case STRING: case STRING:
@ -895,12 +907,12 @@ public class SXSSFCell implements Cell {
} }
case FORMULA: case FORMULA:
{ {
_value=new NumericFormulaValue(); _value = new NumericFormulaValue();
break; break;
} }
case BLANK: case BLANK:
{ {
_value=new BlankValue(); _value = new BlankValue();
break; break;
} }
case BOOLEAN: case BOOLEAN:
@ -916,7 +928,7 @@ public class SXSSFCell implements Cell {
} }
case ERROR: case ERROR:
{ {
_value=new ErrorValue(); _value = new ErrorValue();
break; break;
} }
default: default:
@ -932,22 +944,22 @@ public class SXSSFCell implements Cell {
{ {
case NUMERIC: case NUMERIC:
{ {
_value=new NumericFormulaValue(); _value = new NumericFormulaValue();
break; break;
} }
case STRING: case STRING:
{ {
_value=new StringFormulaValue(); _value = new StringFormulaValue();
break; break;
} }
case BOOLEAN: case BOOLEAN:
{ {
_value=new BooleanFormulaValue(); _value = new BooleanFormulaValue();
break; break;
} }
case ERROR: case ERROR:
{ {
_value=new ErrorFormulaValue(); _value = new ErrorFormulaValue();
break; break;
} }
default: default:
@ -1041,12 +1053,12 @@ public class SXSSFCell implements Cell {
Property _next; Property _next;
public Property(Object value) public Property(Object value)
{ {
_value=value; _value = value;
} }
abstract int getType(); abstract int getType();
void setValue(Object value) void setValue(Object value)
{ {
_value=value; _value = value;
} }
Object getValue() Object getValue()
{ {
@ -1090,7 +1102,7 @@ public class SXSSFCell implements Cell {
} }
void setValue(double value) void setValue(double value)
{ {
_value=value; _value = value;
} }
double getValue() double getValue()
{ {
@ -1111,7 +1123,7 @@ public class SXSSFCell implements Cell {
String _value; String _value;
void setValue(String value) void setValue(String value)
{ {
_value=value; _value = value;
} }
String getValue() String getValue()
{ {
@ -1133,7 +1145,7 @@ public class SXSSFCell implements Cell {
} }
void setValue(RichTextString value) void setValue(RichTextString value)
{ {
_value=value; _value = value;
} }
RichTextString getValue() RichTextString getValue()
{ {
@ -1154,7 +1166,7 @@ public class SXSSFCell implements Cell {
} }
void setValue(String value) void setValue(String value)
{ {
_value=value; _value = value;
} }
String getValue() String getValue()
{ {
@ -1246,7 +1258,7 @@ public class SXSSFCell implements Cell {
} }
void setValue(boolean value) void setValue(boolean value)
{ {
_value=value; _value = value;
} }
boolean getValue() boolean getValue()
{ {
@ -1262,7 +1274,7 @@ public class SXSSFCell implements Cell {
} }
void setValue(byte value) void setValue(byte value)
{ {
_value=value; _value = value;
} }
byte getValue() byte getValue()
{ {

View File

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