fix: read xf/@xfId so a cellXfs entry resolves against its own style - #355
Merged
MathNya merged 3 commits intoAug 10, 2026
Merged
Conversation
`CellFormat::set_attributes` read `numFmtId`, `fontId`, `fillId`, `borderId` and every `apply*` flag, but not `xfId`, even though the struct carries a `format_id` field for it and `write_to` writes it back out. `get_format_id()` therefore always returned 0, so `Stylesheet::make_style` paired every `cellXfs` entry with `cellStyleXfs[0]`. That default stand-in can veto the cell's own formatting: `get_style_by_cell_format` lets the referenced style turn a category off, so a workbook whose Normal style declares `applyNumberFormat="0"` — which Excel writes for plenty of templates — lost the number format on **every** cell. A percent cell read back its stored `0.25` instead of `25%`, and dates and currency went the same way. Reading it also stops a round trip from rewriting each `xfId` as 0. Signed-off-by: developer0hye <developer.0hye@gmail.com>
Current nightly clippy denies `nonstandard_macro_braces` for `format!`, so
`cargo clippy -- -D warnings` fails on master:
error: use of irregular braces for `format!` macro
--> src/writer/csv.rs:47:25
That aborts the build matrix before the other toolchains finish, so every
open pull request reads as red regardless of its own contents.
Switching to parentheses is the fix clippy suggests; rustfmt then wraps the
arguments, which the brace form had exempted it from. No behaviour change.
Signed-off-by: developer0hye <developer.0hye@gmail.com>
`get_format_id` and `get_number_format_id` are deprecated since 3.0.0 in favour of `format_id` and `number_format_id`, so the new tests compiled with four `deprecated` warnings. Use the current names. Signed-off-by: developer0hye <developer.0hye@gmail.com>
Owner
|
@developer0hye |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
CellFormat::set_attributesreadsnumFmtId,fontId,fillId,borderIdandall six
apply*flags, but notxfId— even though the struct already carries aformat_idfield for it andwrite_towrites it back out.get_format_id()therefore always returns 0, soStylesheet::make_stylepairsevery
cellXfsentry withcellStyleXfs[0]instead of the style it names:Why it matters
get_style_by_cell_formatlets that referenced style turn a whole formattingcategory off:
When
cellStyleXfs[0]carriesapplyNumberFormat="0"— which Excel writes forthe Normal style in many templates —
applyis false for every cell in theworkbook and no number format is ever attached. Percent, currency and date cells
all read back as the raw stored number.
Measured on such a workbook (cell holding
0.25undernumFmtId="9",xfId="6", wherecellStyleXfs[6]also declaresnumFmtId="9"):get_formatted_value()"0.25"None<numFmt numFmtId="9" formatCode="0%"/>"0.25"NoneapplyNumberFormat="0"removed fromcellStyleXfs[0]"25%"Some("0%")0 of its 294 cells resolved a number format. Adding the format definition changes
nothing — only the wrong default xf matters.
Reading the attribute also stops a round trip from rewriting every
xfIdas 0,which currently loses the cell-style association on save.
Tests
Three unit tests in
src/structs/cell_format.rs: a declaredxfIdis read from aself-closing element and from one with children, and an absent one stays 0.
cargo teston this branch: 109 + 113 + 4 + 79 pass.cargo clippy -- -D warningsandcargo testare green on 1.88.0, stable andnightly;
cargo +nightly fmt --all --checkis clean.Note
The second commit is unrelated to the
xfIdfix and is here only to get CIgreen. Current nightly clippy denies
nonstandard_macro_braces, socargo clippy -- -D warningsfails onmasteratsrc/writer/csv.rs:47:Because that job aborts the matrix, the other three toolchains are cancelled
before they finish and every open pull request reads as red. The commit
switches that one call to parentheses, which is what clippy suggests; rustfmt
then wraps the arguments, since the brace form had exempted it. Happy to split
it out if you would rather take it separately.
The third commit swaps the deprecated
get_format_id/get_number_format_idin the new tests for
format_id/number_format_id, so the suite compileswithout
deprecatedwarnings.