mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-20 21:10:03 -06:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f94cb8f7c0 |
@@ -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("<externalReference/><externalReference r:id="), "{wb_xml}");
|
||||
let (reopened, rep2) = read_xlsx(&saved).unwrap();
|
||||
assert!(!rep2.warnings.iter().any(|w| w.contains("dropped")), "{:?}", rep2.warnings);
|
||||
assert_eq!(reopened.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 saved2 = write_xlsx(&reopened).unwrap();
|
||||
let mut zip = zip::ZipArchive::new(std::io::Cursor::new(saved2)).unwrap();
|
||||
let read_zip = |z: &mut zip::ZipArchive<std::io::Cursor<Vec<u8>>>, 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("<externalReference ").skip(1).filter_map(|s| s.split("r:id=\"").nth(1).and_then(|t| t.split('"').next())).collect();
|
||||
assert_eq!(ref_ids.len(), 2, "{wb_xml}");
|
||||
let wb_rels = read_zip(&mut zip, "xl/_rels/workbook.xml.rels");
|
||||
let second_target = wb_rels
|
||||
.split("<Relationship ")
|
||||
.find(|r| r.contains(&format!("Id=\"{}\"", ref_ids[1])))
|
||||
.and_then(|r| r.split("Target=\"").nth(1))
|
||||
.and_then(|s| s.split('"').next())
|
||||
.expect("second externalReference r:id should resolve in workbook.xml.rels")
|
||||
.trim_start_matches('/')
|
||||
.to_string();
|
||||
let second_part = if second_target.starts_with("xl/") { second_target } else { format!("xl/{second_target}") };
|
||||
let second_xml = read_zip(&mut zip, &second_part);
|
||||
assert!(second_xml.contains("<sheetName val=\"SheetA\"/>"), "{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}"
|
||||
);
|
||||
}
|
||||
|
||||
+7
-12
@@ -212,30 +212,25 @@ pub(crate) fn is_xlsb(bytes: &[u8]) -> bool {
|
||||
}
|
||||
|
||||
fn preserve_external_links(links: &[(String, Option<craft_xlsx::ExternalLink>)]) -> Option<Preserved> {
|
||||
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!("<externalReferences xmlns:r=\"{NS_REL}\">");
|
||||
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("<externalReference/>");
|
||||
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 <externalReferences> 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}<externalLink xmlns=\"{NS_MAIN}\"/>"), 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, "<externalReference r:id=\"{rid}\"/>");
|
||||
}
|
||||
if !has_any {
|
||||
return None;
|
||||
}
|
||||
refs_xml.push_str("</externalReferences>");
|
||||
p.elements.push(PreservedXml { slot: "externalReferences".into(), xml: refs_xml });
|
||||
Some(p)
|
||||
|
||||
Reference in New Issue
Block a user