diff --git a/crates/xlsx/src/sheet_write.rs b/crates/xlsx/src/sheet_write.rs index daac03daa..4c496e9ac 100644 --- a/crates/xlsx/src/sheet_write.rs +++ b/crates/xlsx/src/sheet_write.rs @@ -742,6 +742,11 @@ fn write_cell( Value::Number(n) => { let _ = write!(s, ">{}", num(*n)); } + // Text inside a spill or array range is a cached formula result: Excel writes it as `t="str"` + // and shows #VALUE! for a shared string there when it trusts the saved values. + Value::Text(t) if extra.is_some() => { + let _ = write!(s, " t=\"str\">{}", esc(&encode_xstring(t))); + } Value::Text(t) => { let i = out.sst.index(t); let _ = write!(s, " t=\"s\">{i}"); diff --git a/crates/xlsx/src/tests/array_export.rs b/crates/xlsx/src/tests/array_export.rs index 646bd1db3..b8a36924a 100644 --- a/crates/xlsx/src/tests/array_export.rs +++ b/crates/xlsx/src/tests/array_export.rs @@ -480,3 +480,24 @@ fn long_flat_chains_are_scalar_formulas() { assert_dynamic(&sheet, "B4", "B4"); assert_eq!(cell(&sheet, "B1").child("f").unwrap().text, refs); } + +#[test] +fn spilled_text_is_written_as_a_cached_string_not_a_shared_string() { + let mut wb = Workbook::new(); + let sh = wb.sheet_mut(0).unwrap(); + let mut anchor = Cell::formula(Formula::new("SORT({\"b\";\"a\";\"c\"})")); + anchor.value = Value::text("a"); + sh.cells.set(at("K4"), anchor); + sh.spill_ranges.insert(at("K4"), range("K4:K6")); + sh.spill.insert(at("K5"), Value::text("b")); + sh.spill.insert(at("K6"), Value::text("c")); + + let bytes = write_xlsx(&wb).unwrap(); + let mut pkg = Package::open(&bytes).unwrap(); + let sheet = pkg.read_xml("xl/worksheets/sheet1.xml").unwrap().unwrap(); + for (address, text) in [("K4", "a"), ("K5", "b"), ("K6", "c")] { + let c = cell(&sheet, address); + assert_eq!(c.attr("t"), Some("str"), "{address}"); + assert_eq!(c.child("v").unwrap().text, text, "{address}"); + } +}