Skip to content

Preserve t="str" on empty-string formula results - #357

Merged
MathNya merged 1 commit into
MathNya:masterfrom
ben-schreiber:fix/empty-string-formula-result-type
Aug 25, 2026
Merged

Preserve t="str" on empty-string formula results#357
MathNya merged 1 commit into
MathNya:masterfrom
ben-schreiber:fix/empty-string-formula-result-type

Conversation

@ben-schreiber

Copy link
Copy Markdown
Contributor

Fixes #356.

Problem

When the reader encounters a formula cell whose cached result is an empty string — written by Excel/LibreOffice as a self-closing <v/> with t="str" — the value is dropped and the cell decays to Empty, losing its string type. On write the t="str" attribute disappears, so =IF(cond,"","x") evaluating to "" becomes an untyped/blank cell. Non-empty string results are unaffected.

Root cause: in Cell::set_attributes, the Event::Start arm dispatches <v> on the declared t type, but the Event::Empty arm (which is how a self-closing <v/> arrives) only handled <f/> and ignored <v/> entirely.

Fix

Handle <v/> in the Event::Empty arm, dispatching on the declared type the same way the Event::Start arm does, so an empty string result keeps t="str".

Repro (before this PR)

input.xlsx cell: <c r="A1" t="str"><f>IF(TRUE,"","x")</f><v/></c>

let book = umya_spreadsheet::reader::xlsx::read(Path::new("input.xlsx")).unwrap();
let cell = book.get_sheet(&0).unwrap().get_cell((1, 1)).unwrap();
assert_eq!(cell.get_data_type(), "str"); // was "", now "str"

After the fix the written cell keeps its type: <c r="A1" t="str"><f>IF(TRUE,"","x")</f><v></v></c>.

Tests

Added empty_string_formula_result_keeps_str_type covering the read path. Full cargo test --lib passes (114 tests).

The cell reader only handled a self-closing `<v/>` for formulas, so an
empty cached string result (written by Excel/LibreOffice as `<v/>`) was
dropped and the cell decayed to Empty, losing its `t="str"` type on write.
Handle `<v/>` in the `Event::Empty` arm, dispatching on the declared type
so an empty string result stays a string.

Bump version to 3.1.1.

Closes MathNya#356
@ben-schreiber
ben-schreiber force-pushed the fix/empty-string-formula-result-type branch from 3457482 to 90ad410 Compare August 24, 2026 10:54
@MathNya

MathNya commented Aug 25, 2026

Copy link
Copy Markdown
Owner

@ben-schreiber
Thank you for the PR.
Since there were no issues, I’ll go ahead and merge it.
There’s an error in Clippy, but I’ll take care of that.

@MathNya
MathNya merged commit e81bfc6 into MathNya:master Aug 25, 2026
1 of 5 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] empty string cells are rendered as empty without a type

2 participants