Skip to content

fix(xlsx): preserve saved Excel error cells - #2732

Open
Lubai (sunxiuguo) wants to merge 1 commit into
microsoft:mainfrom
sunxiuguo:fix-xlsx-error-cells-2728
Open

Lubai (sunxiuguo) wants to merge 1 commit into
microsoft:mainfrom
sunxiuguo:fix-xlsx-error-cells-2728

Conversation

@sunxiuguo

Copy link
Copy Markdown

Fixes #2728.

Saved Excel error cells currently become pandas NaN values, so the Markdown loses the distinction between an error and a blank cell. This restores the saved error text without evaluating formulas.

Changes

  • Reuse the public ExcelFile.book to locate nonempty error cells, including cached formula errors and error-valued headers.
  • Let pandas render the original column dtypes first, then restore only the corresponding HTML cells. Inserting error strings into object-cast columns before to_html() can otherwise change dates or round small nonzero floats to 0.0.
  • Keep the existing showZeroes repair, image hooks, borrowed stream, blank-cell behavior, and XLS path. No new dependency.
  • Add 22 synthetic regression cases covering all seven standard errors, legacy sheet views, duplicate error headers, sparse positions, cached formula errors, empty typed-error cells, and numeric/date rendering.

This is deliberately separate from literal NA-text handling in #2499, number-format/precision proposals, and formula calculation.

Validation

On current main 3cf47504 plus this patch, isolated Linux / Python 3.12:

  • hatch test --python 3.12: 1072 passed, 57 skipped (repository CI-mode skips; no service credentials; exiftool unavailable).
  • pytest tests/test_xlsx.py tests/test_html.py -q: 81 passed.
  • pre-commit run --all-files: passed, with the current Black 26.10.1 pin.
  • The 22 added cases against unchanged current-main code: 20 failed, 2 passed; the two passing controls are empty typed-error cells.
  • Separate automated review: 15 additional synthetic cases passed, plus an escaping probe.

Independent synthetic checks also covered header-only sheets, stream offsets, nonerror rendering, and escaped HTML-shaped cell values. These are automated checks, not Excel GUI acceptance or a multi-version/platform matrix.

Cost

This adds a second read-only worksheet scan. Sheets containing errors also need an HTML parse/serialization; sheets without errors retain the original HTML. A single 2,000×12 synthetic observation measured read-plus-HTML at 1.38→1.57 seconds without errors and 0.44→2.58 seconds with all data cells errors. These concurrent-run measurements are illustrative, not a performance guarantee.

@BichengWang Bicheng (BichengWang) left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed a65d8788befe29cf5a14a30ddb3e7e5b3d0f81a7. Restoring saved errors after to_html() keeps pandas' existing numeric/date formatting while recovering the error values and their cell positions. Reusing ExcelFile.book also preserves the data-only behavior for cached formula errors; this does not evaluate formulas.

I reproduced the regression against current main 21d9c1d01ecc12538dab19811fd247ef2f96ef60: the 22 added cases give 20 failures and two passing empty-error controls with the main-branch converter. At this PR head, GITHUB_ACTIONS=1 python -m pytest tests/test_xlsx.py tests/test_html.py -q -p no:cacheprovider passes all 81 tests. An additional 26 generated workbook checks passed: errors at rows 1/2/5/11 and columns 1/3/7 with trailing data, an error-only header, HTML-shaped ordinary text, and borrowed-stream lifetime.

No blocking correctness issue found in this diff. Validation was offline on macOS / Python 3.12.15 with pandas 3.0.6 and openpyxl 3.1.5; I did not run the full suite or platform matrix. The full extras could not be installed offline because Mammoth >=1.12.2 was absent from cache, and the pinned Black 26.10.1 was also unavailable offline.

@sunxiuguo

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

XLSX conversion loses Excel error values such as #DIV/0! and #REF!

2 participants