Skip to content

Commit

Permalink
Merge pull request #84 from NOTtardy/support-for-getRichStringCellValue
Browse files Browse the repository at this point in the history
Support for Cell.getRichStringCellValue()
  • Loading branch information
monitorjbl authored Apr 23, 2017
2 parents 3c1746d + 4244232 commit b517261
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/java/com/monitorjbl/xlsx/impl/StreamingCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -386,11 +387,28 @@ public void setCellFormula(String formula) throws FormulaParseException {
}

/**
* Not supported
* Get the value of the cell as a XSSFRichTextString
* <p>
* For numeric cells we throw an exception. For blank cells we return an empty string.
* For formula cells we return the pre-calculated value if a string, otherwise an exception
* </p>
* @return the value of the cell as a XSSFRichTextString
*/
@Override
public RichTextString getRichStringCellValue() {
throw new NotSupportedException();
public XSSFRichTextString getRichStringCellValue() {
CellType cellType = getCellTypeEnum();
XSSFRichTextString rt;
switch (cellType) {
case BLANK:
rt = new XSSFRichTextString("");
break;
case STRING:
rt = new XSSFRichTextString(getStringCellValue());
break;
default:
throw new NotSupportedException();
}
return rt;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,41 @@ public void testTypes() throws Exception {
assertEquals(Cell.CELL_TYPE_STRING, row.get(0).getCellType());
assertEquals(Cell.CELL_TYPE_STRING, row.get(1).getCellType());
assertEquals("Type", row.get(0).getStringCellValue());
assertEquals("Type", row.get(0).getRichStringCellValue().getString());
assertEquals("Value", row.get(1).getStringCellValue());
assertEquals("Value", row.get(1).getRichStringCellValue().getString());

row = obj.get(1);
assertEquals(2, row.size());
assertEquals(Cell.CELL_TYPE_STRING, row.get(0).getCellType());
assertEquals(Cell.CELL_TYPE_STRING, row.get(1).getCellType());
assertEquals("string", row.get(0).getStringCellValue());
assertEquals("string", row.get(0).getRichStringCellValue().getString());
assertEquals("jib-jab", row.get(1).getStringCellValue());
assertEquals("jib-jab", row.get(1).getRichStringCellValue().getString());

row = obj.get(2);
assertEquals(2, row.size());
assertEquals(Cell.CELL_TYPE_STRING, row.get(0).getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, row.get(1).getCellType());
assertEquals("int", row.get(0).getStringCellValue());
assertEquals("int", row.get(0).getRichStringCellValue().getString());
assertEquals(10, row.get(1).getNumericCellValue(), 0);

row = obj.get(3);
assertEquals(2, row.size());
assertEquals(Cell.CELL_TYPE_STRING, row.get(0).getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, row.get(1).getCellType());
assertEquals("double", row.get(0).getStringCellValue());
assertEquals("double", row.get(0).getRichStringCellValue().getString());
assertEquals(3.14, row.get(1).getNumericCellValue(), 0);

row = obj.get(4);
assertEquals(2, row.size());
assertEquals(Cell.CELL_TYPE_STRING, row.get(0).getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, row.get(1).getCellType());
assertEquals("date", row.get(0).getStringCellValue());
assertEquals("date", row.get(0).getRichStringCellValue().getString());
assertEquals(df.parse("1/1/2014"), row.get(1).getDateCellValue());
assertTrue(DateUtil.isCellDateFormatted(row.get(1)));

Expand All @@ -106,19 +113,27 @@ public void testTypes() throws Exception {
assertEquals(Cell.CELL_TYPE_STRING, row.get(5).getCellType());
assertEquals(Cell.CELL_TYPE_STRING, row.get(6).getCellType());
assertEquals("long", row.get(0).getStringCellValue());
assertEquals("long", row.get(0).getRichStringCellValue().getString());
assertEquals("ass", row.get(1).getStringCellValue());
assertEquals("ass", row.get(1).getRichStringCellValue().getString());
assertEquals("row", row.get(2).getStringCellValue());
assertEquals("row", row.get(2).getRichStringCellValue().getString());
assertEquals("look", row.get(3).getStringCellValue());
assertEquals("look", row.get(3).getRichStringCellValue().getString());
assertEquals("at", row.get(4).getStringCellValue());
assertEquals("at", row.get(4).getRichStringCellValue().getString());
assertEquals("it", row.get(5).getStringCellValue());
assertEquals("it", row.get(5).getRichStringCellValue().getString());
assertEquals("go", row.get(6).getStringCellValue());
assertEquals("go", row.get(6).getRichStringCellValue().getString());

row = obj.get(6);
assertEquals(3, row.size());
assertEquals(Cell.CELL_TYPE_STRING, row.get(0).getCellType());
assertEquals(Cell.CELL_TYPE_BOOLEAN, row.get(1).getCellType());
assertEquals(Cell.CELL_TYPE_BOOLEAN, row.get(2).getCellType());
assertEquals("boolean", row.get(0).getStringCellValue());
assertEquals("boolean", row.get(0).getRichStringCellValue().getString());
assertEquals(true, row.get(1).getBooleanCellValue());
assertEquals(false, row.get(2).getBooleanCellValue());
}
Expand Down Expand Up @@ -230,9 +245,11 @@ public void testGaps() throws Exception {
assertEquals(Cell.CELL_TYPE_STRING, row.get(0).getCellType());
assertEquals(Cell.CELL_TYPE_STRING, row.get(1).getCellType());
assertEquals("Dat", row.get(0).getStringCellValue());
assertEquals("Dat", row.get(0).getRichStringCellValue().getString());
assertEquals(0, row.get(0).getColumnIndex());
assertEquals(0, row.get(0).getRowIndex());
assertEquals("gap", row.get(1).getStringCellValue());
assertEquals("gap", row.get(1).getRichStringCellValue().getString());
assertEquals(2, row.get(1).getColumnIndex());
assertEquals(0, row.get(1).getRowIndex());

Expand All @@ -241,9 +258,11 @@ public void testGaps() throws Exception {
assertEquals(Cell.CELL_TYPE_STRING, row.get(0).getCellType());
assertEquals(Cell.CELL_TYPE_STRING, row.get(1).getCellType());
assertEquals("guuurrrrrl", row.get(0).getStringCellValue());
assertEquals("guuurrrrrl", row.get(0).getRichStringCellValue().getString());
assertEquals(0, row.get(0).getColumnIndex());
assertEquals(6, row.get(0).getRowIndex());
assertEquals("!", row.get(1).getStringCellValue());
assertEquals("!", row.get(1).getRichStringCellValue().getString());
assertEquals(6, row.get(1).getColumnIndex());
assertEquals(6, row.get(1).getRowIndex());
}
Expand Down Expand Up @@ -273,6 +292,7 @@ public void testMultipleSheets_alpha() throws Exception {
row = obj.get(0);
assertEquals(1, row.size());
assertEquals("stuff", row.get(0).getStringCellValue());
assertEquals("stuff", row.get(0).getRichStringCellValue().getString());
}
}

Expand Down Expand Up @@ -301,6 +321,7 @@ public void testMultipleSheets_zulu() throws Exception {
row = obj.get(0);
assertEquals(1, row.size());
assertEquals("yeah", row.get(0).getStringCellValue());
assertEquals("yeah", row.get(0).getRichStringCellValue().getString());
}
}

Expand Down Expand Up @@ -329,6 +350,7 @@ public void testSheetName_zulu() throws Exception {
row = obj.get(0);
assertEquals(1, row.size());
assertEquals("yeah", row.get(0).getStringCellValue());
assertEquals("yeah", row.get(0).getRichStringCellValue().getString());
}
}

Expand Down Expand Up @@ -356,6 +378,7 @@ public void testSheetName_alpha() throws Exception {
row = obj.get(0);
assertEquals(1, row.size());
assertEquals("stuff", row.get(0).getStringCellValue());
assertEquals("stuff", row.get(0).getRichStringCellValue().getString());
}
}

Expand Down Expand Up @@ -396,6 +419,7 @@ public void testIteration() throws Exception {
for(Row r : reader) {
assertEquals(i, r.getCell(0).getNumericCellValue(), 0);
assertEquals("#" + i, r.getCell(1).getStringCellValue());
assertEquals("#" + i, r.getCell(1).getRichStringCellValue().getString());
i++;
}
}
Expand All @@ -417,6 +441,7 @@ public void testLeadingZeroes() throws Exception {
Row r2 = iter.next();
assertEquals(2, r2.getCell(0).getNumericCellValue(), 0);
assertEquals("0002", r2.getCell(0).getStringCellValue());
assertEquals("0002", r2.getCell(0).getRichStringCellValue().getString());
assertEquals(Cell.CELL_TYPE_STRING, r2.getCell(0).getCellType());
}
}
Expand Down Expand Up @@ -477,6 +502,7 @@ public void testBlankNumerics() throws Exception {
try(StreamingReader reader = StreamingReader.builder().read(f)) {
Row row = reader.iterator().next();
assertThat(row.getCell(1).getStringCellValue(), equalTo(""));
assertThat(row.getCell(1).getRichStringCellValue().getString(), equalTo(""));
assertThat(row.getCell(1).getDateCellValue(), is(nullValue()));
assertThat(row.getCell(1).getNumericCellValue(), equalTo(0.0));
}
Expand Down Expand Up @@ -513,6 +539,7 @@ public void testEncryption() throws Exception {
for(Row r : reader) {
for(Cell c : r) {
assertEquals("Demo", c.getStringCellValue());
assertEquals("Demo", c.getRichStringCellValue().getString());
break OUTER;
}
}
Expand All @@ -528,6 +555,7 @@ public void testStringCellValue() throws Exception {
for(Row r : reader) {
if(r.getRowNum() == 1) {
assertEquals("", r.getCell(1).getStringCellValue());
assertEquals("", r.getCell(1).getRichStringCellValue().getString());
}
}
}
Expand Down Expand Up @@ -558,7 +586,9 @@ public void testInlineCells() throws Exception {
) {
Row row = reader.iterator().next();
assertEquals("First inline cell", row.getCell(0).getStringCellValue());
assertEquals("First inline cell", row.getCell(0).getRichStringCellValue().getString());
assertEquals("Second inline cell", row.getCell(1).getStringCellValue());
assertEquals("Second inline cell", row.getCell(1).getRichStringCellValue().getString());
}
}

Expand Down Expand Up @@ -603,6 +633,7 @@ public void testShouldReturnBlankForMissingCellPolicy_CREATE_NULL_AS_BLANK() thr
) {
Row row = reader.iterator().next();
assertEquals("B1 is Null ->", row.getCell(0, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); //Remain unchanged
assertEquals("B1 is Null ->", row.getCell(0, Row.CREATE_NULL_AS_BLANK).getRichStringCellValue().getString()); //Remain unchanged
assertThat(row.getCell(1), is(nullValue()));
assertNotNull(row.getCell(1, Row.CREATE_NULL_AS_BLANK));
}
Expand Down

0 comments on commit b517261

Please sign in to comment.