Skip to content

XLSB import: open on the sheet the workbook was saved on - #4376

Open
Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/xlsb-active-sheet
Open

Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/xlsb-active-sheet

Conversation

@Hamza0101

@Hamza0101 Hamza0101 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4300

What was wrong

read_xlsb in crates/xlsx/src/xlsb.rs always made the first visible sheet active. A workbook Excel saved on its second sheet opened on the first, and gridcraft-cli convert … output.csv exported the first sheet by default. That's the issue's FIRST where Excel and the XLSX and XLS paths give SECOND.

What changed

  • workbook also reads the workbook's first book view (BrtBookView, record 158, from Microsoft's published [MS-XLSB] 2.4.304). It takes itabCur, the zero-based index of the active tab in the workbook's sheet list, which is the tab Excel opens on. The other fields are read past.
  • That index counts sheets the import skips (module, chart and macro sheets). The sheet loop records which imported sheet each list entry became, and the index is mapped through that.
  • With no book view, an index off the list, a skipped sheet, or a hidden tab, the first visible sheet is active, exactly as before.

QA report

Main vs branch, end to end (gridcraft-cli)

second.xlsb and first.xlsb are minimal XLSB packages I wrote in Python, following the record layout this crate's tests use. Each has the issue's two sheets, First and Second, with A1 holding the sheet's name in capitals. They differ only in the book view's active tab: Second and First. I didn't have Excel to save real .xlsb files. I ran the issue's commands, gridcraft-cli info <file> --json and gridcraft-cli convert <file> out.csv. main is a gridcraft-cli built from origin/main (abcfc191); the branch is this PR (fefdb623). The Excel column is the issue's result.

File main: activeSheet / CSV first field this PR Excel (issue)
second.xlsb (saved on Second) 0 / FIRST 1 / SECOND Second / SECOND
first.xlsb (saved on First, control) 0 / FIRST 0 / FIRST First
second.xlsb with --sheet Second (control) SECOND SECOND

Test

tests::xlsb_tests::xlsb_opens_on_the_sheet_it_was_saved_on covers:

  • the saved tab, 1 and 0;
  • the fallbacks: no book view, an index past the list, and a hidden tab;
  • a list starting with a module sheet the import skips, where tab 2 is the second imported sheet, tab 1 the first, and tab 0 (the module) falls back.

The other XLSB tests (values, visibility, epochs, damaged packages) still pass.

Negative control

With the saved tab ignored (on top of fefdb623, then restored with git checkout), the test fails at its first assertion: left "First", right "Second".

Gate (macOS arm64)

Step Result
cargo fmt --all -- --check clean
cargo clippy -p gridcraft-xlsx --all-targets -- -D warnings clean
cargo test -p gridcraft-xlsx -p gridcraft-engine 1004 (+1 ignored) + 120 + 7 + 375 passed, 0 failed

Not covered

  • Real Excel files: the .xlsb files are ones I wrote. The record follows the published specification, and the field layout matches the one documented there.
  • Several windows: only the first book view is read, as Excel opens on it. A workbook with several windows keeps the others' tabs unused, as before.
  • Selection within the sheet (BrtSel, the active cell) isn't imported. The issue concerns the active sheet.
  • cargo xtask ci: I didn't run the full cargo xtask ci (wasm, layers, the other crates).

Summary by CodeRabbit

  • Bug Fixes
    • XLSB workbooks now open with the saved active sheet selected when it is visible and available. If the saved selection is missing, invalid, hidden, or unavailable, the first visible sheet is selected instead. This makes sheet selection more consistent when importing workbooks with hidden sheets or sheets that cannot be imported.

The reader always made the first visible sheet active, so a workbook saved on its second sheet opened, and exported by default, on the first. It now reads the active tab from the workbook's first book view ([MS-XLSB] BrtBookView itabCur), mapped past sheets the import skips; with no book view, an index off the list or a hidden tab, it falls back to the first visible sheet as before. Fixes storytold#4300.
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The XLSB reader now reads the saved active-tab index and uses it to select the corresponding visible imported worksheet. If the index cannot select a visible imported worksheet, the reader falls back to the first visible worksheet or index 0.

Changes: XLSB active-sheet selection

Layer / File(s) Summary
Read the saved active-tab index
crates/xlsx/src/xlsb.rs
The workbook parser returns sheet metadata and the active-tab index from the first BrtBookView record.
Map and select the imported worksheet
crates/xlsx/src/xlsb.rs, crates/xlsx/src/tests/xlsb_tests.rs
XLSB import maps workbook sheet positions to imported worksheets and selects the saved tab when it is visible. Tests cover valid selection and fallback when the view is absent, the index is out of range, the sheet is hidden, or the entry refers to a skipped module sheet.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: harrisonoconnorhover, aiongg

Merge Risk: Minimal · up to fefdb

XLSB files now open on the sheet they were saved on, and the previous first-visible-sheet behavior is kept as a fallback. No merge-blocking risk was found.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Issue #4300 requires XLSB import to open on the saved active sheet, so info --json and default convert export that sheet. The diff in crates/xlsx/src/xlsb.rs does this logic. workbook reads `i… Confirm the BrtBookView field widths in [MS-XLSB] 2.4.304 against the code's six u32 reads followed by itabCur. Also verify the change with a real Excel-saved .xlsb file saved on Second, showing info --json active sheet 1 and default …
Passed Passed checks: 3 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "XLSB import: open on the sheet the workbook was saved on" matches the main change. The PR reads itabCur from the first BrtBookView record and selects the saved tab when it maps to a visible…
Out of Scope Changes check Passed All changes in the whole-PR diff (merge base abcfc191 to head fefdb623) serve the #4300 objective. The xlsb.rs changes add the book view read, the sheet-list-to-imported-sheet mapping, and the a…
Full details: Title check

Explanation

The title "XLSB import: open on the sheet the workbook was saved on" matches the main change. The PR reads itabCur from the first BrtBookView record and selects the saved tab when it maps to a visible imported worksheet. Otherwise it falls back to the first visible sheet. The title is specific, short, and states the user-facing effect that issue #4300 asks for. It does not need to list the helper or test changes.

Full details: Out of Scope Changes check

Explanation

All changes in the whole-PR diff (merge base abcfc191 to head fefdb623) serve the #4300 objective. The xlsb.rs changes add the book view read, the sheet-list-to-imported-sheet mapping, and the active-sheet selection with fallback. The xlsb_tests.rs changes add helpers and a regression test for the same behavior. No unrelated files or behavior changes appear in the reviewed summary and diff.

Full details: Linked Issues check

Explanation

Issue #4300 requires XLSB import to open on the saved active sheet, so info --json and default convert export that sheet. The diff in crates/xlsx/src/xlsb.rs does this logic. workbook reads itabCur from the first BrtBookView (record 158). read_xlsb maps that sheet-list index to the imported sheet through a per-entry imported vector. It keeps the saved tab only when that sheet is imported and visible. Otherwise it uses the first visible sheet, as before. The test xlsb_opens_on_the_sheet_it_was_saved_on covers the saved tab, the fallbacks, and a skipped module sheet. Two points remain unverified. First, the code reads six u32 fields before itabCur. The PR states the layout follows [MS-XLSB] 2.4.304, but I could not check the field widths in the specification. If iTabRatio or itabFirst are narrower than 4 bytes, the offset is wrong. Second, all checks use workbooks the author generated. No Excel-saved XLSB file was tested, and the issue reproduces with Excel-saved files.

Resolution

Confirm the BrtBookView field widths in [MS-XLSB] 2.4.304 against the code's six u32 reads followed by itabCur. Also verify the change with a real Excel-saved .xlsb file saved on Second, showing info --json active sheet 1 and default CSV SECOND.

Finishing Touches

Generate unit tests (beta)

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

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between abcfc19 and fefdb62.

Files selected for processing: 2

  • crates/xlsx/src/tests/xlsb_tests.rs
  • crates/xlsx/src/xlsb.rs

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

emrahoksak pushed a commit to emrahoksak/girdcraft- that referenced this pull request Nov 18, 2026

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.

XLSB import ignores the saved active sheet and exports the first sheet by default

1 participant