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();
}
*/
@Override
public CellType getCachedFormulaResultType() {
- if (_value.getType() != CellType.FORMULA) {
+ if (!isFormulaCell()) {
throw new IllegalStateException("Only formula cells have cached results");
}
* @see org.apache.poi.ss.usermodel.FormulaError
*/
@Override
- public void setCellErrorValue(byte value)
- {
- ensureType(CellType.ERROR);
- if(_value.getType()==CellType.FORMULA)
- ((ErrorFormulaValue)_value).setPreEvaluatedValue(value);
- else
- ((ErrorValue)_value).setValue(value);
+ 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 {
+ ensureType(CellType.ERROR);
+ ((ErrorValue) _value).setValue(value);
+ }
}
/**
{
if(_value.getType()!=CellType.STRING
||((StringValue)_value).isRichText())
- _value=new PlainStringValue();
+ _value = new PlainStringValue();
}
/*package*/ void ensureRichTextStringType()
{
if(_value.getType()!=CellType.STRING
||!((StringValue)_value).isRichText())
- _value=new RichTextValue();
+ _value = new RichTextValue();
}
/*package*/ void ensureType(CellType type)
{
{
case NUMERIC:
{
- _value=new NumericValue();
+ _value = new NumericValue();
break;
}
case STRING:
}
case FORMULA:
{
- _value=new NumericFormulaValue();
+ _value = new NumericFormulaValue();
break;
}
case BLANK:
{
- _value=new BlankValue();
+ _value = new BlankValue();
break;
}
case BOOLEAN:
}
case ERROR:
{
- _value=new ErrorValue();
+ _value = new ErrorValue();
break;
}
default:
{
case NUMERIC:
{
- _value=new NumericFormulaValue();
+ _value = new NumericFormulaValue();
break;
}
case STRING:
{
- _value=new StringFormulaValue();
+ _value = new StringFormulaValue();
break;
}
case BOOLEAN:
{
- _value=new BooleanFormulaValue();
+ _value = new BooleanFormulaValue();
break;
}
case ERROR:
{
- _value=new ErrorFormulaValue();
+ _value = new ErrorFormulaValue();
break;
}
default:
Property _next;
public Property(Object value)
{
- _value=value;
+ _value = value;
}
abstract int getType();
void setValue(Object value)
{
- _value=value;
+ _value = value;
}
Object getValue()
{
}
void setValue(double value)
{
- _value=value;
+ _value = value;
}
double getValue()
{
String _value;
void setValue(String value)
{
- _value=value;
+ _value = value;
}
String getValue()
{
}
void setValue(RichTextString value)
{
- _value=value;
+ _value = value;
}
RichTextString getValue()
{
}
void setValue(String value)
{
- _value=value;
+ _value = value;
}
String getValue()
{
}
void setValue(boolean value)
{
- _value=value;
+ _value = value;
}
boolean getValue()
{
}
void setValue(byte value)
{
- _value=value;
+ _value = value;
}
byte getValue()
{
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());
+ }
+ }
}