https://bz.apache.org/bugzilla/show_bug.cgi?id=62846

            Bug ID: 62846
           Summary: FormulaParseException thrown for row containing empty
                    formula cells
           Product: POI
           Version: 4.0.0-FINAL
          Hardware: PC
            Status: NEW
          Severity: normal
          Priority: P2
         Component: XSSF
          Assignee: [email protected]
          Reporter: [email protected]
  Target Milestone: ---

I'm reading workbooks that may have been downloaded from Google sheets, so
there are errors where a formula doesn't get converted. for each cell in the
range I'm looking at, I want to display "ERROR" for cells with invalid
formulas.

I'm using lambda, so the code was essentially:
inRow.forEach(inCell -> {
  if (CellType.FORMULA == inCell.getCellType()) {
    try {
      evaluator.evaluateInCell(inCell);
    } catch (Exception ex) {
      inCell.setCellType(CellType.STRING);
      inCell.setCellValue("ERROR");
    }
  }
});

I was getting FormulaParseException thrown WITHOUT being caught!

The problem was that the value looked at by the evaluator was itself an error
message that a FormulaParseException had been thrown. With more evaluation, I
determined that the cells for which the exception was thrown were empty, and
that a single FormulaParseException was thrown to my code as part of the row
evaluation, probably dealing with the forEach invocation.

My code is now:

try {
  inRow.forEach(inCell -> {
    if (CellType.FORMULA == inCell.getCellType()) {
      String raw = ((XSSFCell) inCell).getRawValue();
      if (raw.isEmpty()) {
        inCell.setCellType(CellType.STRING);
        inCell.setCellValue("");
      } else {
        try {
          evaluator.evaluateInCell(inCell);
        } catch (Exception ex) {
          inCell.setCellType(CellType.STRING);
          inCell.setCellValue("ERROR");
        }
      }
  });
} catch (FormulaParseException) {
  // whatever....
}

I think the empty cells should be silently ignored.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to