Skip to content

fix(sheet): store error cells produced by the error string converter - #1318

Open
Sean-Walker0 wants to merge 2 commits into
apache:mainfrom
Sean-Walker0:fix/error-cell-write
Open

Sean-Walker0 wants to merge 2 commits into
apache:mainfrom
Sean-Walker0:fix/error-cell-write

Conversation

@Sean-Walker0

Copy link
Copy Markdown
Contributor

Purpose of the pull request

StringErrorConverter is registered for both directions (putAllConverter), and its write side produces an ERROR-typed WriteCellData — but the fillValue switch in the write executor had no ERROR case, so every write through the explicitly configured converter failed:

ExcelWriteDataConvertException: Not supported data:#DIV/0! return type:ERROR at row:1

A registered converter whose output type the executor cannot store is unusable by construction: @ExcelProperty(converter = StringErrorConverter.class) reads fine (the read side decodes error cells to #NAME?-style text) but can never write.

No issue exists for this yet; found while auditing the converter/executor contract.

What's changed?

fillValue gains an ERROR case: the error text is mapped to its POI code via FormulaError.forString(...) and stored through Cell.setCellErrorValue(byte) — the exact inverse of the read side, which surfaces error cells as the same #NAME?-style text (ErrorEval.getText, #1093). An unrecognized error text fails the conversion loudly, like any other conversion failure.

Checklist

  • I have read the Contributor Guide.
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Verifying this change

ErrorCellWriteTest:

  • stringErrorConverterWritesAnErrorCell — red before (Not supported data:#DIV/0! return type:ERROR), green after: the cell is a real POI error cell and #DIV/0! round-trips through ErrorEval.getText(getErrorCellValue());

  • plainStringFieldStillWrites — pins ordinary string writes, green before and after.

  • ./mvnw clean package -B -Dmaven.test.skip=false -pl fesod-common,fesod-shaded,fesod-sheet: 1814 tests, 0 failures, 0 errors.

  • ./mvnw -pl fesod-sheet spotless:check: clean.

Notes

@Sean-Walker0
Sean-Walker0 force-pushed the fix/error-cell-write branch 2 times, most recently from 3d994b1 to 4c74f23 Compare November 9, 2026 23:38

@Mikkey-f Mikkey-f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Sean-Walker0 — the core fix is right, and it closes a real gap: StringErrorConverter is registered by default (DefaultConverterLoader, putAllConverter), but the executor rejected the ERROR type it produces, so the converter was read-only in practice.

Verification (Windows 11, JDK 17, 4c74f233)

  • Green: ErrorCellWriteTest 2/2; full fesod-common 60 + fesod-sheet 1,819 tests, 0 failures/errors; spotless:check clean.
  • Red: with only AbstractExcelWriteExecutor.java reverted, stringErrorConverterWritesAnErrorCell fails with ExcelWriteDataConvertException: Not supported data:#DIV/0! return type:ERRORat row:1 — the test pins the gap precisely.
  • Probes: #N/A round-trips to a real ERROR cell in both XLSX and XLS (getCellType() == ERROR, ErrorEval.getText(...) == "#N/A"), so the fix is not XLSX-only.

One issue I think should be fixed before merge: CSV silently loses the value

AbstractExcelWriteExecutor is shared by the CSV writer (ExcelBuilderImpl → ExcelWriteAddExecutor with CsvWorkbook/CsvSheet). On the CSV path, CsvCell.setCellErrorValue(byte) stores the code in numberValue and leaves stringValue null, while CsvSheet.buildCellValue() does:

case STRING:
case ERROR:
    return csvCell.getStringCellValue();

So writing an ErrorModel to CSV produces the header row followed by an empty data cell (probe), i.e. the error text is silently dropped. Before this change the CSV write failed (here via the pre-existing #97 CsvWorkbook.hashCode/CsvSheet.hashCode recursion in the failure path), so the new behavior is silent loss rather than a supported write. Two minimal fixes:

  • set stringValue = ErrorEval.getText(value) in CsvCell.setCellErrorValue(byte), so the existing ERROR branch serializes the text, or
  • render ErrorEval.getText(csvCell.getErrorCellValue()) in CsvSheet.buildCellValue.

A CSV case in ErrorCellWriteTest would pin whichever you choose.

Non-blocking

  • Invalid text (e.g. "not-an-error") throws POI's IllegalArgumentException: Unknown error code: not-an-error, without the row/column context the other write failures carry. Consider wrapping it in ExcelWriteDataConvertException, or adding a test to document the failure mode.
  • The new test covers XLSX only; XLS works with the same code (probe above), so a parameterized XLS case would be cheap. CSV needs the handling above either way.

@Sean-Walker0

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough verification and for catching the CSV gap — pushed as a follow-up commit (rebased onto current main):

  1. CSV fix: took your first option — CsvCell.setCellErrorValue(byte) now also stores ErrorEval.getText(value) in stringValue, so the existing ERROR branch serializes the text. New test stringErrorConverterWritesErrorTextOnCsv pins it (writes #DIV/0!, asserts the CSV line carries the text, quoted by the default MINIMAL mode).
  2. XLS case: added stringErrorConverterWritesAnErrorCellOnXls — a real ERROR cell whose ErrorEval.getText(...) round-trips #N/A, matching your probe.
  3. Non-blocking exception-context point: left as-is for this PR — POI's IllegalArgumentException already fails the conversion loudly; wrapping it in ExcelWriteDataConvertException would touch the shared fillValue error path for all types, which felt out of scope here. Happy to do it as a follow-up if you prefer.

ErrorCellWriteTest 4/4 green; full suite 1821 tests, 0 failures; spotless clean.

@Sean-Walker0
Sean-Walker0 force-pushed the fix/error-cell-write branch 2 times, most recently from 0eb835a to f65fc58 Compare November 11, 2026 11:22
@Sean-Walker0

Copy link
Copy Markdown
Contributor Author

Follow-up also folded in (amended into the CSV commit, sorry for the squash): the non-blocking point is now addressed too — FormulaError.forString failures are wrapped in ExcelWriteDataConvertException with the row context, pinned by unknownErrorTextFailsWithContext. ErrorCellWriteTest 5/5 green, spotless clean.

@Mikkey-f Mikkey-f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Sean-Walker0 — both follow-ups check out on my side.

I re-verified on f65fc581:

  • CsvCell.setCellErrorValue now keeps ErrorEval.getText(value) next to the code, so the CSV ERROR branch writes #DIV/0! instead of an empty field. That is the first option from my earlier review, and the new CSV test pins it.
  • Wrapping FormulaError.forString's IllegalArgumentException in ExcelWriteDataConvertException gives the failure the row context it was missing; the test asserts both the exception type and that the offending value is in the message.

Verification:

  • Targeted ErrorCellWriteTest: 5/5 (XLSX, XLS, CSV, unknown-value failure, plain-string regression).
  • Red side: reverting the two product files to 4c74f233 reproduces exactly the two gaps — CSV expected "#DIV/0!" but was empty, and an unknown value threw POI's IllegalArgumentException instead of ExcelWriteDataConvertException.
  • Full suite: fesod-common 60 + fesod-sheet 1,822, 0 failures / 0 errors / 0 skipped; spotless passes; CI is 14/14 green.

One non-blocking nit: the new tests mix imports and fully qualified names (java.io.FileInputStream, java.util.List, org.apache.poi.ss.usermodel.Cell, etc.) even though the class already imports several of them — importing the rest would read a bit better. Not a blocker.

LGTM from my side.

StringErrorConverter is registered for both directions, and its write
side produces an ERROR-typed WriteCellData - but the fillValue switch
in the write executor had no ERROR case, so every write through the
explicitly configured converter failed with 'Not supported data: ...
return type:ERROR'. A registered converter whose output the executor
cannot store is unusable by construction.

Map the error text to its POI code with FormulaError.forString and
store it via Cell.setCellErrorValue, symmetric with the read side
that decodes error cells to the same '#NAME?' style text.

Verified with ErrorCellWriteTest (the explicit-converter write throws
before the change and produces a real error cell round-tripping
'#DIV/0!' after; a plain string field write is pinned on both sides)
plus the full fesod-sheet suite (1814 tests) and spotless:check.
CsvCell.setCellErrorValue stored only the numeric code, while
CsvSheet.buildCellValue serializes ERROR cells through the string
value - so writing through StringErrorConverter to CSV produced an
empty field instead of the '#NAME?' style text. Keep the
ErrorEval.getText rendering in stringValue alongside the code.

Also pins the error-cell write on XLS (real ERROR cell round-trip)
and CSV (error text line) in ErrorCellWriteTest.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants