Skip to content

Commit 3a06f17

Browse files
authored
Merge pull request #128 from cmuchinsky/master
Ignore formula tag that isn't within a cell
2 parents 033e9c1 + 4e06a3c commit 3a06f17

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/main/java/com/monitorjbl/xlsx/impl/StreamingSheetReader.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ && isSpreadsheetTag(event.asStartElement().getName())) {
189189
}
190190
}
191191
} else if("f".equals(tagLocalName)) {
192-
currentCell.setType("str");
192+
if (currentCell != null) {
193+
currentCell.setType("str");
194+
}
193195
}
194196

195197
// Clear contents cache
@@ -207,9 +209,12 @@ && isSpreadsheetTag(event.asEndElement().getName())) {
207209
currentRowNum++;
208210
} else if("c".equals(tagLocalName)) {
209211
currentRow.getCellMap().put(currentCell.getColumnIndex(), currentCell);
212+
currentCell = null;
210213
currentColNum++;
211214
} else if("f".equals(tagLocalName)) {
212-
currentCell.setFormula(lastContents);
215+
if (currentCell != null) {
216+
currentCell.setFormula(lastContents);
217+
}
213218
}
214219

215220
}

src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.apache.poi.openxml4j.opc.OPCPackage;
55
import org.apache.poi.openxml4j.opc.PackageAccess;
66
import org.apache.poi.ss.usermodel.Cell;
7+
import org.apache.poi.ss.usermodel.CellType;
78
import org.apache.poi.ss.usermodel.DateUtil;
89
import org.apache.poi.ss.usermodel.Row;
910
import org.apache.poi.ss.usermodel.Workbook;
@@ -671,4 +672,25 @@ public void testShouldHandleBlankSSTReference() throws Exception {
671672
}
672673
}
673674
}
675+
676+
// The last cell on this sheet should be a NUMERIC but there is a lingering "f"
677+
// tag that was getting attached to the last cell causing it to be a FORUMLA.
678+
@Test
679+
public void testForumulaOutsideCellIgnored() throws Exception {
680+
try(
681+
InputStream is = new FileInputStream(new File("src/test/resources/formula_outside_cell.xlsx"));
682+
Workbook wb = StreamingReader.builder().open(is);
683+
) {
684+
Iterator<Row> rows = wb.getSheetAt(0).iterator();
685+
Cell cell = null;
686+
while(rows.hasNext()) {
687+
Iterator<Cell> cells = rows.next().iterator();
688+
while(cells.hasNext()) {
689+
cell = cells.next();
690+
}
691+
}
692+
assertNotNull(cell);
693+
assertThat(cell.getCellTypeEnum(), is(CellType.NUMERIC));
694+
}
695+
}
674696
}
19.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)