Skip to content

Commit

Permalink
Merge pull request #128 from cmuchinsky/master
Browse files Browse the repository at this point in the history
Ignore formula tag that isn't within a cell
  • Loading branch information
monitorjbl authored Jan 12, 2018
2 parents 033e9c1 + 4e06a3c commit 3a06f17
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ && isSpreadsheetTag(event.asStartElement().getName())) {
}
}
} else if("f".equals(tagLocalName)) {
currentCell.setType("str");
if (currentCell != null) {
currentCell.setType("str");
}
}

// Clear contents cache
Expand All @@ -207,9 +209,12 @@ && isSpreadsheetTag(event.asEndElement().getName())) {
currentRowNum++;
} else if("c".equals(tagLocalName)) {
currentRow.getCellMap().put(currentCell.getColumnIndex(), currentCell);
currentCell = null;
currentColNum++;
} else if("f".equals(tagLocalName)) {
currentCell.setFormula(lastContents);
if (currentCell != null) {
currentCell.setFormula(lastContents);
}
}

}
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
Expand Down Expand Up @@ -671,4 +672,25 @@ public void testShouldHandleBlankSSTReference() throws Exception {
}
}
}

// The last cell on this sheet should be a NUMERIC but there is a lingering "f"
// tag that was getting attached to the last cell causing it to be a FORUMLA.
@Test
public void testForumulaOutsideCellIgnored() throws Exception {
try(
InputStream is = new FileInputStream(new File("src/test/resources/formula_outside_cell.xlsx"));
Workbook wb = StreamingReader.builder().open(is);
) {
Iterator<Row> rows = wb.getSheetAt(0).iterator();
Cell cell = null;
while(rows.hasNext()) {
Iterator<Cell> cells = rows.next().iterator();
while(cells.hasNext()) {
cell = cells.next();
}
}
assertNotNull(cell);
assertThat(cell.getCellTypeEnum(), is(CellType.NUMERIC));
}
}
}
Binary file added src/test/resources/formula_outside_cell.xlsx
Binary file not shown.

0 comments on commit 3a06f17

Please sign in to comment.