Skip to content

Apply date and time number formats only to numeric cells - #1043

Merged
michelebastione merged 1 commit into
mini-software:masterfrom
pedrorossato:fix/text-cell-date-format-master
Nov 12, 2026
Merged

michelebastione merged 1 commit into
mini-software:masterfrom
pedrorossato:fix/text-cell-date-format-master

Conversation

@pedrorossato

@pedrorossato pedrorossato commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1041

Master counterpart of #1042.

ConvertValueByStyleFormat converted any value whose text parsed as a double, so text cells (t="s", t="str" and t="inlineStr") with a date or time number format came back as DateTime/TimeSpan, with a result that depended on the current culture. ConvertCellValue yields a double only for numeric cells, so the method now returns any other value unchanged and converts the double directly.

  • OpenXmlStyles.ConvertValueByStyleFormat: early return for values that are not a double. The double is used as is, instead of going through double.TryParse(value?.ToString(), ...). This covers the three callers: the row reader in OpenXmlReader, the cell reader in OpenXmlReader.Cells and OpenXmlMappingReader.
  • MiniExcelGithubIssuesTests.QueryReturnsTextCellsWithDateOrTimeFormatAsString: one row with a numeric cell and t="s", t="str" and t="inlineStr" cells sharing a date format, plus a numeric cell and a t="s" cell sharing a time format. The numeric cells still come back as DateTime/TimeSpan and the text cells as string. It fails without the fix.
  • SheetHelper.ReplaceSharedStringsXml: test helper mirroring ReplaceStylesXml.

Tested locally on net10.0 only, since this machine has no .NET 11 SDK: MiniExcel.OpenXml.Tests 545/545.

Summary by CodeRabbit

  • Bug Fixes
    • Date- and time-formatted numeric cells are now interpreted as dates or durations only when their values are numeric. Text cells that look like numbers remain text, and invalid date values are returned unchanged. This prevents text from being unintentionally converted and makes handling of date and time values more predictable when reading spreadsheets.

Text cells (t="s", "str" and "inlineStr") with a date or time number
format were converted to DateTime/TimeSpan whenever their text parsed as
a double, with a result that depended on the current culture. Excel
never applies a number format to text.

ConvertCellValue yields a double only for numeric cells, so
ConvertValueByStyleFormat now returns any other value unchanged and
converts the double directly instead of round-tripping it through
ToString and double.TryParse.

Fixes mini-software#1041
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Date and time style conversion now operates only on values already typed as double. Invalid OLE Automation dates and non-numeric values remain unchanged. A regression test covers numeric and text cells with date and time formats.

Changes: Cell style conversion

Layer / File(s) Summary
Numeric-only date and time conversion
src/MiniExcel.OpenXml/Styles/OpenXmlStyles.cs, tests/MiniExcel.OpenXml.Tests/Utils/SheetHelper.cs, tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs
The conversion method uses numeric values directly for date and time formats and leaves non-numeric values unchanged. The regression test checks date and time conversions alongside shared-string, string, and inline-string values. The test helper can replace the shared-strings ZIP entry with supplied XML.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

Suggested reviewers: michelebastione, virzak, gdols

Merge Risk: Minimal · up to 9c5e2

Text cells with date or time formats now stay strings instead of being converted by content and culture, while numeric cells still convert to DateTime or TimeSpan. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Apply date and time number formats only to numeric cells" matches the main change. The PR changes OpenXmlStyles.ConvertValueByStyleFormat so that only double values are converted under date…
Linked Issues check Passed Issue #1041 requires that date/time number formats convert only numeric cells, and that text cells (t="s", t="str", t="inlineStr") stay strings regardless of culture. The diff to OpenXmlStyles.Convert…
Out of Scope Changes check Passed The only production change is in OpenXmlStyles.ConvertValueByStyleFormat, which is the method named in #1041. The change is limited to the type check and the direct use of the double. The test additio…
Full details: Title check

Explanation

The title "Apply date and time number formats only to numeric cells" matches the main change. The PR changes OpenXmlStyles.ConvertValueByStyleFormat so that only double values are converted under date or time formats, and text cells (t="s", t="str", t="inlineStr") stay strings. The title is short, specific, and states the primary behavior change. The added regression test and the SheetHelper test utility support the same change.

Full details: Linked Issues check

Explanation

Issue #1041 requires that date/time number formats convert only numeric cells, and that text cells (t="s", t="str", t="inlineStr") stay strings regardless of culture. The diff to OpenXmlStyles.ConvertValueByStyleFormat adds an early return when value is not a double (if (value is not double number) return value;). Date and time conversion then uses that double directly, with no double.TryParse(value?.ToString(), ...) call. Per the issue, ConvertCellValue yields a double only for numeric cells, so text cells now bypass conversion. This covers all three callers named in the issue. The existing IsValidOADateTime guard is kept. The PR adds QueryReturnsTextCellsWithDateOrTimeFormatAsString, which covers text cells with date and time formats next to numeric cells. The author reports 545/545 passing on net10.0. Strongly typed DateTime property queries are not changed by this diff. I did not inspect the test file hunks or the callers directly; the test-coverage claim relies on the PR summary.

Full details: Out of Scope Changes check

Explanation

The only production change is in OpenXmlStyles.ConvertValueByStyleFormat, which is the method named in #1041. The change is limited to the type check and the direct use of the double. The test addition (QueryReturnsTextCellsWithDateOrTimeFormatAsString) and the SheetHelper.ReplaceSharedStringsXml helper directly support the regression test for this bug. No unrelated changes were identified in the reviewed evidence.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c25f14d3-e7f6-48bb-bbe9-f503c98ba605
Commits

Reviewing files that changed from the base of the PR and between 7247760 and 9c5e247.

Files selected for processing: 3

  • src/MiniExcel.OpenXml/Styles/OpenXmlStyles.cs
  • tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs
  • tests/MiniExcel.OpenXml.Tests/Utils/SheetHelper.cs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@michelebastione
michelebastione merged commit 430674c into mini-software:master Nov 12, 2026
4 checks passed
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.

[BUG] - Text cells with a date or time number format are read as DateTime/TimeSpan

2 participants