@Override
public void setCellValue(RichTextString value)
{
- XSSFRichTextString xvalue = (XSSFRichTextString)value;
-
- if (xvalue != null && xvalue.getString() != null) {
- ensureRichTextStringType();
-
- if (xvalue.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
+ if (value != null && value.getString() != null) {
+ if (value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
}
+ ensureRichTextStringType();
+
if(_value instanceof RichTextStringFormulaValue) {
- ((RichTextStringFormulaValue) _value).setPreEvaluatedValue(xvalue);
+ ((RichTextStringFormulaValue) _value).setPreEvaluatedValue(value);
} else {
- ((RichTextValue) _value).setValue(xvalue);
+ ((RichTextValue) _value).setValue(value);
}
} else {
setCellType(CellType.BLANK);
public void setCellValue(String value)
{
if (value != null) {
- ensureTypeOrFormulaType(CellType.STRING);
-
if (value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
}
-
- if(_value.getType()==CellType.FORMULA)
- if(_value instanceof NumericFormulaValue) {
- ((NumericFormulaValue) _value).setPreEvaluatedValue(Double.parseDouble(value));
- } else if(_value instanceof RichTextStringFormulaValue) {
- ((RichTextStringFormulaValue) _value).setPreEvaluatedValue(new XSSFRichTextString(value));
- } else {
- ((StringFormulaValue) _value).setPreEvaluatedValue(value);
- }
- else
- ((PlainStringValue)_value).setValue(value);
+
+ ensureTypeOrFormulaType(CellType.STRING);
+
+ if(_value.getType() == CellType.FORMULA) {
+ ((StringFormulaValue) _value).setPreEvaluatedValue(value);
+ } else {
+ ((PlainStringValue) _value).setValue(value);
+ }
} else {
setCellType(CellType.BLANK);
}
boolean result = cell.getBooleanCellValue();
assertFalse(result);
}
+
+ @Test
+ public void setStringCellValue_ifThrows_shallNotChangeCell() {
+ Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
+
+ final double value = 2.78;
+ cell.setCellValue(value);
+ assertEquals(CellType.NUMERIC, cell.getCellType());
+
+ int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1;
+ String badStringValue = new String(new byte[badLength]);
+
+ try {
+ cell.setCellValue(badStringValue);
+ } catch (IllegalArgumentException e) {
+ // no-op, expected to throw but we need to assert something more
+ }
+
+ assertEquals(CellType.NUMERIC, cell.getCellType());
+ assertEquals(value, cell.getNumericCellValue(), 0);
+ }
+
+ @Test
+ public void setStringCellValueWithRichTextString_ifThrows_shallNotChangeCell() {
+ Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
+
+ final double value = 2.78;
+ cell.setCellValue(value);
+ assertEquals(CellType.NUMERIC, cell.getCellType());
+
+ int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1;
+ RichTextString badStringValue = cell.getSheet().getWorkbook().getCreationHelper().
+ createRichTextString(new String(new byte[badLength]));
+
+ try {
+ cell.setCellValue(badStringValue);
+ } catch (IllegalArgumentException e) {
+ // no-op, expected to throw but we need to assert something more
+ }
+
+ assertEquals(CellType.NUMERIC, cell.getCellType());
+ assertEquals(value, cell.getNumericCellValue(), 0);
+ }
}