Fixed bug in conversion to/from text cells

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@711694 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-11-05 20:46:00 +00:00
parent 5da098c36a
commit 0e80e842aa
2 changed files with 117 additions and 70 deletions

View File

@ -43,7 +43,6 @@ import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.ObjRecord; import org.apache.poi.hssf.record.ObjRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.RecordBase; import org.apache.poi.hssf.record.RecordBase;
import org.apache.poi.hssf.record.StringRecord;
import org.apache.poi.hssf.record.SubRecord; import org.apache.poi.hssf.record.SubRecord;
import org.apache.poi.hssf.record.TextObjectRecord; import org.apache.poi.hssf.record.TextObjectRecord;
import org.apache.poi.hssf.record.UnicodeString; import org.apache.poi.hssf.record.UnicodeString;
@ -332,38 +331,23 @@ public final class HSSFCell {
break; break;
case CELL_TYPE_STRING : case CELL_TYPE_STRING :
LabelSSTRecord lrec = null; LabelSSTRecord lrec;
if (cellType != this.cellType) if (cellType == this.cellType) {
{ lrec = (LabelSSTRecord) record;
} else {
lrec = new LabelSSTRecord(); lrec = new LabelSSTRecord();
}
else
{
lrec = ( LabelSSTRecord ) record;
}
lrec.setColumn(col); lrec.setColumn(col);
lrec.setRow(row); lrec.setRow(row);
lrec.setXFIndex(styleIndex); lrec.setXFIndex(styleIndex);
if (setValue)
{
if ((getStringCellValue() != null)
&& (!getStringCellValue().equals("")))
{
int sst = 0;
UnicodeString str = getRichStringCellValue().getUnicodeString();
//jmh if (encoding == ENCODING_COMPRESSED_UNICODE)
//jmh {
// jmh str.setCompressedUnicode();
// jmh } else if (encoding == ENCODING_UTF_16)
// jmh {
// jmh str.setUncompressedUnicode();
// jmh }
sst = book.getWorkbook().addSSTString(str);
lrec.setSSTIndex(sst);
getRichStringCellValue().setUnicodeString(book.getWorkbook().getSSTString(sst));
} }
if (setValue) {
String str = convertCellValueToString();
int sstIndex = book.getWorkbook().addSSTString(new UnicodeString(str));
lrec.setSSTIndex(sstIndex);
UnicodeString us = book.getWorkbook().getSSTString(sstIndex);
stringValue = new HSSFRichTextString();
stringValue.setUnicodeString(us);
} }
record = lrec; record = lrec;
break; break;
@ -778,7 +762,9 @@ public final class HSSFCell {
case CELL_TYPE_BOOLEAN: case CELL_TYPE_BOOLEAN:
return (( BoolErrRecord ) record).getBooleanValue(); return (( BoolErrRecord ) record).getBooleanValue();
case CELL_TYPE_STRING: case CELL_TYPE_STRING:
return Boolean.valueOf(((StringRecord)record).getString()).booleanValue(); int sstIndex = ((LabelSSTRecord)record).getSSTIndex();
String text = book.getWorkbook().getSSTString(sstIndex).getString();
return Boolean.valueOf(text).booleanValue();
case CELL_TYPE_NUMERIC: case CELL_TYPE_NUMERIC:
return ((NumberRecord)record).getValue() != 0; return ((NumberRecord)record).getValue() != 0;
@ -792,6 +778,26 @@ public final class HSSFCell {
} }
throw new RuntimeException("Unexpected cell type (" + cellType + ")"); throw new RuntimeException("Unexpected cell type (" + cellType + ")");
} }
private String convertCellValueToString() {
switch (cellType) {
case CELL_TYPE_BLANK:
return "";
case CELL_TYPE_BOOLEAN:
return ((BoolErrRecord) record).getBooleanValue() ? "TRUE" : "FALSE";
case CELL_TYPE_STRING:
int sstIndex = ((LabelSSTRecord)record).getSSTIndex();
return book.getWorkbook().getSSTString(sstIndex).getString();
case CELL_TYPE_NUMERIC:
return String.valueOf(((NumberRecord)record).getValue());
case CELL_TYPE_ERROR:
return HSSFErrorConstants.getText(((BoolErrRecord) record).getErrorValue());
case CELL_TYPE_FORMULA:
// should really evaluate, but HSSFCell can't call HSSFFormulaEvaluator
return "";
}
throw new RuntimeException("Unexpected cell type (" + cellType + ")");
}
/** /**
* get the value of the cell as a boolean. For strings, numbers, and errors, we throw an exception. * get the value of the cell as a boolean. For strings, numbers, and errors, we throw an exception.

View File

@ -28,8 +28,8 @@ import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
/** /**
* Tests various functionity having to do with HSSFCell. For instance support for * Tests various functionality having to do with {@link HSSFCell}. For instance support for
* paticular datatypes, etc. * particular datatypes, etc.
* @author Andrew C. Oliver (andy at superlinksoftware dot com) * @author Andrew C. Oliver (andy at superlinksoftware dot com)
* @author Dan Sherman (dsherman at isisph.com) * @author Dan Sherman (dsherman at isisph.com)
* @author Alex Jacoby (ajacoby at gmail.com) * @author Alex Jacoby (ajacoby at gmail.com)
@ -349,7 +349,7 @@ public final class TestHSSFCell extends TestCase {
* Test to ensure we can only assign cell styles that belong * Test to ensure we can only assign cell styles that belong
* to our workbook, and not those from other workbooks. * to our workbook, and not those from other workbooks.
*/ */
public void testCellStyleWorkbookMatch() throws Exception { public void testCellStyleWorkbookMatch() {
HSSFWorkbook wbA = new HSSFWorkbook(); HSSFWorkbook wbA = new HSSFWorkbook();
HSSFWorkbook wbB = new HSSFWorkbook(); HSSFWorkbook wbB = new HSSFWorkbook();
@ -361,11 +361,11 @@ public final class TestHSSFCell extends TestCase {
try { try {
styA.verifyBelongsToWorkbook(wbB); styA.verifyBelongsToWorkbook(wbB);
fail(); fail();
} catch(IllegalArgumentException e) {} } catch (IllegalArgumentException e) {}
try { try {
styB.verifyBelongsToWorkbook(wbA); styB.verifyBelongsToWorkbook(wbA);
fail(); fail();
} catch(IllegalArgumentException e) {} } catch (IllegalArgumentException e) {}
HSSFCell cellA = wbA.createSheet().createRow(0).createCell(0); HSSFCell cellA = wbA.createSheet().createRow(0).createCell(0);
HSSFCell cellB = wbB.createSheet().createRow(0).createCell(0); HSSFCell cellB = wbB.createSheet().createRow(0).createCell(0);
@ -375,11 +375,52 @@ public final class TestHSSFCell extends TestCase {
try { try {
cellA.setCellStyle(styB); cellA.setCellStyle(styB);
fail(); fail();
} catch(IllegalArgumentException e) {} } catch (IllegalArgumentException e) {}
try { try {
cellB.setCellStyle(styA); cellB.setCellStyle(styA);
fail(); fail();
} catch(IllegalArgumentException e) {} } catch (IllegalArgumentException e) {}
}
public void testChangeTypeStringToBool() {
HSSFCell cell = new HSSFWorkbook().createSheet("Sheet1").createRow(0).createCell(0);
cell.setCellValue(new HSSFRichTextString("TRUE"));
assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCellType());
try {
cell.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
} catch (ClassCastException e) {
throw new AssertionFailedError(
"Identified bug in conversion of cell from text to boolean");
}
assertEquals(HSSFCell.CELL_TYPE_BOOLEAN, cell.getCellType());
assertEquals(true, cell.getBooleanCellValue());
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
assertEquals("TRUE", cell.getRichStringCellValue().getString());
// 'false' text to bool and back
cell.setCellValue(new HSSFRichTextString("FALSE"));
cell.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
assertEquals(HSSFCell.CELL_TYPE_BOOLEAN, cell.getCellType());
assertEquals(false, cell.getBooleanCellValue());
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
assertEquals("FALSE", cell.getRichStringCellValue().getString());
}
public void testChangeTypeBoolToString() {
HSSFCell cell = new HSSFWorkbook().createSheet("Sheet1").createRow(0).createCell(0);
cell.setCellValue(true);
try {
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
} catch (IllegalStateException e) {
if (e.getMessage().equals("Cannot get a text value from a boolean cell")) {
throw new AssertionFailedError(
"Identified bug in conversion of cell from boolean to text");
}
throw e;
}
assertEquals("TRUE", cell.getRichStringCellValue().getString());
} }
} }