Skip to content

xlsx: keep a table's filter criteria when the sheet AutoFilter coincides with it - #1531

Merged
aiongg merged 1 commit into
storytold:mainfrom
midasmatterhorn:fix/r4-table-autofilter-criteria
Oct 22, 2026
Merged

aiongg merged 1 commit into
storytold:mainfrom
midasmatterhorn:fix/r4-table-autofilter-criteria

Conversation

@midasmatterhorn

Copy link
Copy Markdown
Contributor

The engine's live filter state is always sheet.autofilter — apply_filter (data.rs:585), filter_values, clear_filter, reapply and cmd/slicer.rs all read and write it, and the auto-filter dropdown of a table header installs one over the table's own range (ui-egui/src/grid.rs:2749 runs data.filter {"range": <the table's range>}) — while Table::filter is only ever filled by the XLSX reader. Writing then suppressed the sheet <autoFilter> whenever any table merely intersected it (sheet_write.rs:342) and wrote the table part from Table::filter, which is empty: a filtered table saved with its rows still hidden="1" and no filter criteria anywhere. On reopen there is no filter to clear, the dropdown shows no selection, data.clearFilter restores nothing, and Excel shows an inactive filter over mysteriously hidden rows. A slicer's selection is discarded by the same path (a slicer is front-end over the same filter state).

Fix: where the sheet filter's range coincides with a table's (contains, header row, filter button) and the table has no criteria of its own, fold the sheet criteria into the table part — the stored colId offsets are relative to the range's first column and both ranges start at the same column, so they carry over verbatim. A sheet filter that only partially overlaps a table still has no valid home (an <autoFilter> beside the table's own would be invalid XML) and keeps being suppressed; the suppression test is narrowed from intersects to that case so partial filters are written as before.

Regression test: a_table_filter_criteria_survive_a_round_trip — on main the table part carries no criteria after a save (the expect("…kept on save") fires); with the fold the criteria round trip, the hidden row stays hidden, and the sheet keeps no duplicate autoFilter. Full gridcraft-xlsx lib suite green (174 passed).

…des with it

The engine's live filter state is always sheet.autofilter — apply_filter,
filter_values, clear_filter, reapply and the slicer command all read and
write it, and a table's header dropdown installs one over the table's own
range — while Table::filter is only ever filled by the reader. Writing
suppressed the sheet <autoFilter> whenever any table merely intersected
it and wrote the table part from Table::filter, which was empty: a
filtered table saved with its rows still hidden and no criteria anywhere,
so on reopen there was no filter to clear and Excel showed an inactive
filter over mysteriously hidden rows. A slicer's selection was lost the
same way.

Fold the coinciding sheet filter's criteria into the table part (colId
offsets are relative to the range's first column, and both ranges start
at the same column, so they carry over verbatim); a filter that only
partially covers a table still has nowhere valid to go and keeps being
suppressed.

Regression test: a_table_filter_criteria_survive_a_round_trip — on main
the table part carries no criteria after a save; with the fold they round
trip, the hidden row stays, and the sheet keeps no duplicate autoFilter.
@MichaelMuClaude

Copy link
Copy Markdown

Verified against main (applies clean):

  • Isolated just the new test and ran it alone against unpatched main: fails exactly as described — "the table's filter criteria were kept on save" panics, confirming the criteria are lost.
  • Applied the full patch: cargo test -p gridcraft-xlsx --lib → 175/175 passed.
  • cargo clippy -p gridcraft-xlsx --lib --tests -- -D warnings clean.
  • Nit, same kind as on engine: sort and remove duplicates move the rows' notes and hyperlinks #1535: cargo fmt --check -p gridcraft-xlsx isn't clean — three spots in the new test (roundtrip.rs around 1182, 1202, 1213) are split across multiple lines where this repo's rustfmt.toml (max_width = 150) would collapse them. Production code (sheet_write.rs) itself is fmt-clean; only the new test needs a pass. cargo xtask ci would catch this as submitted.
  • The fix's logic checks out: folded only pulls the sheet filter into a table when the table's range fully contains the filter's range and the table actually has a header row and filter button (so the colId offsets genuinely line up), and the suppression condition at the bottom was correctly narrowed from intersects (too broad — wrongly ate partial overlaps too) to the same contains_range && header_row && filter_button check, so a partially-overlapping sheet filter still gets its own <autoFilter> as before.

Correct fix for a real silent-data-loss bug; just needs a cargo fmt pass before merge.

aiongg added a commit to aiongg/gridcraft that referenced this pull request Oct 22, 2026
…hen both start at the same column

A sheet AutoFilter that starts right of the table's first column is not folded
(its colId offsets would hit the wrong column); the sheet autoFilter stays
suppressed for any table it intersects, as before. Test added.
@aiongg
aiongg merged commit 604d85e into storytold:main Oct 22, 2026
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.

3 participants