From 606fa7a37e204d70f861bc0fde2475f40648572d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 20 Nov 2026 15:29:55 -0500 Subject: [PATCH] xlsx: emit placeholder externalLink part and r:id for missing XLSB link slots (#4598) --- crates/xlsx/src/tests/xlsb_tests.rs | 39 ++++++++++++++++++++++++++--- crates/xlsx/src/xlsb.rs | 19 ++++++-------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/crates/xlsx/src/tests/xlsb_tests.rs b/crates/xlsx/src/tests/xlsb_tests.rs index ad48fd824..6fa5efaeb 100644 --- a/crates/xlsx/src/tests/xlsb_tests.rs +++ b/crates/xlsx/src/tests/xlsb_tests.rs @@ -668,8 +668,39 @@ fn xlsb_external_link_ordinals_preserve_missing_slots_on_xlsx_save() { assert_eq!(wb.sheets[0].cell(CellRef::new(0, 1)).and_then(|c| c.formula.as_ref()).map(|f| f.text.as_str()), Some("[2]SheetA!$A$1")); let saved = write_xlsx(&wb).unwrap(); - let mut zip = zip::ZipArchive::new(std::io::Cursor::new(saved)).unwrap(); - let mut wb_xml = String::new(); - std::io::Read::read_to_string(&mut zip.by_name("xl/workbook.xml").unwrap(), &mut wb_xml).unwrap(); - assert!(wb_xml.contains(">>, name: &str| { + let mut s = String::new(); + std::io::Read::read_to_string(&mut z.by_name(name).unwrap(), &mut s).unwrap(); + s + }; + let wb_xml = read_zip(&mut zip, "xl/workbook.xml"); + let ref_ids: Vec<&str> = + wb_xml.split(""), "{second_xml}"); + let (dir, file) = second_part.rsplit_once('/').unwrap(); + let second_rels = read_zip(&mut zip, &format!("{dir}/_rels/{file}.rels")); + assert!( + second_rels.contains("externalLinkPath") + && second_rels.contains("Target=\"SecondBook.xlsx\"") + && second_rels.contains("TargetMode=\"External\""), + "{second_rels}" + ); } diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 042043169..89b788bb9 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -212,30 +212,25 @@ pub(crate) fn is_xlsb(bytes: &[u8]) -> bool { } fn preserve_external_links(links: &[(String, Option)]) -> Option { - if links.is_empty() { + if links.is_empty() || links.iter().all(|(_, l)| l.is_none()) { return None; } const CT_EXT_LINK: &str = "application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml"; let mut p = Preserved::default(); let mut refs_xml = format!(""); - let mut has_any = false; for (i, (_, link)) in links.iter().enumerate() { - let Some(link) = link else { - // Preserve the 1-based `[n]` ordinal for later links when an earlier slot is missing. - refs_xml.push_str(""); - continue; - }; - has_any = true; let rid = format!("rId{}", i + 1); let part_name = format!("xl/externalLinks/externalLink{}.xml", i + 1); - let (xml, rels) = external_link_part_xml(link); + // Missing slots still need a valid r:id and part so ECMA-376 and + // XLSX reopen keep the 1-based `[n]` ordinals for later links. + let (xml, rels) = match link { + Some(link) => external_link_part_xml(link), + None => (format!("{XML_DECL}"), Vec::new()), + }; p.rels.push(PreservedRel { id: rid.clone(), rel_type: format!("{NS_REL}/externalLink"), target: part_name.clone(), external: false }); p.parts.push(PreservedPart { name: part_name, content_type: CT_EXT_LINK.into(), data: xml.into_bytes().into(), rels }); let _ = write!(refs_xml, ""); } - if !has_any { - return None; - } refs_xml.push_str(""); p.elements.push(PreservedXml { slot: "externalReferences".into(), xml: refs_xml }); Some(p)