Skip to content

Commit

Permalink
Merge pull request #67 from waxxxd/master
Browse files Browse the repository at this point in the history
Basic implementation for getCell with MissingCellPolicy.
  • Loading branch information
monitorjbl authored Jan 26, 2017
2 parents 045de72 + 34fac11 commit a83c001
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/java/com/monitorjbl/xlsx/impl/StreamingRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ public void setRowNum(int rowNum) {
*/
@Override
public Cell getCell(int cellnum, MissingCellPolicy policy) {
throw new NotSupportedException();
StreamingCell cell = (StreamingCell) cellMap.get(cellnum);
if (policy == Row.CREATE_NULL_AS_BLANK) {
if (cell == null) return new StreamingCell(cellnum, rowIndex, false);

} else if (policy == Row.RETURN_BLANK_AS_NULL) {
if (cell.getCellType() == Cell.CELL_TYPE_BLANK) return null;
}
return cell;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -579,4 +580,30 @@ public void shouldIgnoreSpreadsheetDrawingRows() throws Exception {
}
}
}

@Test
public void testShouldReturnNullForMissingCellPolicy_RETURN_BLANK_AS_NULL() throws Exception {
try (
InputStream is = new FileInputStream(new File("src/test/resources/blank_cells.xlsx"));
StreamingReader reader = StreamingReader.builder()
.read(is);
) {
Row row = reader.iterator().next();
assertNotNull(row.getCell(0, Row.RETURN_BLANK_AS_NULL)); //Remain unchanged
assertNull(row.getCell(1, Row.RETURN_BLANK_AS_NULL));
}
}

@Test
public void testShouldReturnBlankForMissingCellPolicy_CREATE_NULL_AS_BLANK() throws Exception {
try (
InputStream is = new FileInputStream(new File("src/test/resources/null_cell.xlsx"));
StreamingReader reader = StreamingReader.builder()
.read(is);
) {
Row row = reader.iterator().next();
assertEquals("B1 is Null ->", row.getCell(0, Row.CREATE_NULL_AS_BLANK).getStringCellValue()); //Remain unchanged
assertNotNull(row.getCell(1, Row.CREATE_NULL_AS_BLANK));
}
}
}
Binary file added src/test/resources/null_cell.xlsx
Binary file not shown.

0 comments on commit a83c001

Please sign in to comment.