Skip to content

xls, xlsb: quote sheet names in formulas - #780

Open
Alex-ley-scrub wants to merge 1 commit into
tafia:masterfrom
Alex-ley-scrub:fix/formula-sheet-names
Open

Alex-ley-scrub wants to merge 1 commit into
tafia:masterfrom
Alex-ley-scrub:fix/formula-sheet-names

Conversation

@Alex-ley-scrub

Copy link
Copy Markdown
Contributor

Summary

Fixed issue where xls and xlsb formulas wrote sheet names without quotes, so a reference to 'My Sheet'!A1 was read as My Sheet!A1. With sheets named Q3 SALES and SALES, a reference to the first read as a reference to the second.

A sheet name is now quoted when Excel would quote it, by a new quote_sheet_name() in utils.rs. Each reader quotes the names once, in the list of sheet names that its formulas use. Excel quotes a name that:

  • doesn't start with a letter or _, such as '2024';
  • has a character other than a letter, digit, _ or ., such as 'My Sheet', 'a-b' or '€uro';
  • is TRUE or FALSE;
  • could be read as an A1 reference, such as 'A1' or 'XFD1' but not XFE1, which is past the last column;
  • could be read as an R1C1 reference, such as 'R', 'C', 'RC' or 'R2C3'.

An apostrophe in a quoted name is doubled, as in 'Bob''s'.

The rules come from the formula text Excel for Mac wrote for each name. Excel's R1C1 check has some quirks: it quotes R1x but not R1. or R0. The new function quotes all names that start with R, C or RC followed by a digit, so it quotes a few names that Excel doesn't. A quoted name is still a valid reference.

Related issue

None. The same bug was reported in #713, which has since been closed. This follows its approach, and also quotes R2C3, RC and TRUE and leaves XFE1 unquoted, as Excel does.

Testing

tests/formula_sheet_names.xlsb and tests/formula_sheet_names.xls were created in Excel for Mac. They have 20 sheets named to cover each rule above, and Sheet1 column B refers to each one. Column A holds each sheet name, and column C holds the formula as Excel writes it, which the tests expect.

test_xlsb_formula_sheet_names and test_xls_formula_sheet_names check all 20 formulas. They fail without the fix.

Checklist

  • cargo test --all-features passes.
  • cargo fmt -- --check passes.
  • cargo clippy --all-targets --all-features -- -Dwarnings passes.
  • New or changed behavior is covered by a test.
  • Code and tests follow CONTRIBUTING.md.

Other information

In xlsb, defined names read their references through the same sheet list, so they're quoted too. The xls defined names are built separately and aren't changed here.

@jmcnamara

Copy link
Copy Markdown
Collaborator

One reason I passed on #713 the first time around was that the quote checking didn't look complete. But I didn't get around to pointing that out.

You seem to have done a better job. I also tackled the same problem in rust_xlsxwriter in the quote_sheet_name() function. You can copy that directly or use it for inspiration:

https://github.com/jmcnamara/rust_xlsxwriter/blob/main/src/utility.rs#L720-L827

It also has a largish test case that I generated from real Excel files:

https://github.com/jmcnamara/rust_xlsxwriter/blob/main/src/utility/tests.rs#L151

@jmcnamara jmcnamara added next release needs review The PR hasn't been reviewed yet. labels Nov 5, 2026
@Alex-ley-scrub

Copy link
Copy Markdown
Contributor Author

Thanks, that's a much more complete set of rules. I've switched to the rules from quote_sheet_name() and added its test cases. I kept the quoting of TRUE and FALSE, which Excel also quotes.

@jmcnamara

Copy link
Copy Markdown
Collaborator

I kept the quoting of TRUE and FALSE, which Excel also quotes.

@Alex-ley-scrub Thanks for pointing that out. I've added it to rust_xlsxwriter too.

Fixed issue where xls and xlsb formulas wrote sheet names without
quotes, so a reference to `'My Sheet'!A1` was read as `My Sheet!A1`.
With sheets named `Q3 SALES` and `SALES`, a reference to the first read
as a reference to the second.

Sheet names are now quoted with the rules from `quote_sheet_name()` in
rust_xlsxwriter, and also when the name is `TRUE` or `FALSE`, which
Excel quotes too. An apostrophe in a quoted name is doubled.

Co-authored-by: sjvrensburg <sjansevanrensburg@gmail.com>
@Alex-ley-scrub
Alex-ley-scrub force-pushed the fix/formula-sheet-names branch from b5830e9 to bcd1947 Compare November 14, 2026 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs review The PR hasn't been reviewed yet. next release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants