mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-30 09:43:14 -06:00
XLSX: keep the cache of a kept PivotTable that names it only by cacheId
This commit is contained in:
@@ -379,14 +379,29 @@ pub fn write_workbook(wb: &Workbook, wb_rels: &mut Rels, out: &mut Out) -> Workb
|
||||
let (live, dropped): (Vec<&PreservedSheet>, Vec<&PreservedSheet>) = p.sheets.iter().partition(|s| chart_sheet_usable(wb, p, s));
|
||||
let root_of = |s: &PreservedSheet| p.rels.iter().find(|r| r.id == s.rel_id);
|
||||
// Pivot caches no kept PivotTable of a sheet uses any more (their sheets were deleted).
|
||||
let used_caches: HashSet<String> = wb
|
||||
.sheets
|
||||
.iter()
|
||||
.filter_map(|s| s.preserved.as_deref())
|
||||
.flat_map(|q| q.parts.iter().flat_map(|part| part.rels.iter()))
|
||||
let sheet_parts = || wb.sheets.iter().filter_map(|s| s.preserved.as_deref()).flat_map(|q| q.parts.iter());
|
||||
let mut used_caches: HashSet<String> = sheet_parts()
|
||||
.flat_map(|part| part.rels.iter())
|
||||
.filter(|r| !r.external && is_kind(r, &["pivotCacheDefinition"]))
|
||||
.map(|r| normalize(&r.target))
|
||||
.collect();
|
||||
// A PivotTable can name its cache only by the workbook's `cacheId`.
|
||||
let cache_of_id: HashMap<u32, String> = p
|
||||
.elements
|
||||
.iter()
|
||||
.filter(|e| e.slot == "pivotCache")
|
||||
.filter_map(|e| {
|
||||
let x = xml::parse(e.xml.as_bytes()).ok()?;
|
||||
let rel = p.rels.iter().find(|r| Some(r.id.as_str()) == x.attr("id"))?;
|
||||
Some((x.attr_u32("cacheId")?, normalize(&rel.target)))
|
||||
})
|
||||
.collect();
|
||||
used_caches.extend(
|
||||
sheet_parts()
|
||||
.filter(|part| part.content_type == CT_PIVOT_TABLE)
|
||||
.filter_map(|part| xml::parse(&part.data).ok()?.attr_u32("cacheId"))
|
||||
.filter_map(|id| cache_of_id.get(&id).cloned()),
|
||||
);
|
||||
let unused_caches: Vec<&PreservedRel> =
|
||||
p.rels.iter().filter(|r| !r.external && is_kind(r, &["pivotCacheDefinition"]) && !used_caches.contains(&normalize(&r.target))).collect();
|
||||
let mut dropped_ids: HashSet<&str> = dropped.iter().map(|s| s.rel_id.as_str()).collect();
|
||||
|
||||
@@ -1419,6 +1419,18 @@ fn pivot_tables_naming_their_cache_by_id_go_with_a_dropped_cache() {
|
||||
assert!(written_pivot_caches(&u).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pivot_tables_naming_their_cache_by_id_keep_it() {
|
||||
let (wb, _) = read_xlsx(&conn_pivot_package(ConnPkg { table_rels: false, ..CONN_PKG })).unwrap();
|
||||
let u = Unpacked::new(&write_xlsx(&wb).unwrap());
|
||||
u.assert_valid();
|
||||
for sheet in ["Report", "More"] {
|
||||
assert_eq!(written_pivot_tables(&u, sheet).len(), 1, "{sheet}");
|
||||
}
|
||||
let caches = written_pivot_caches(&u);
|
||||
assert_eq!(caches.iter().map(|c| c.0.as_str()).collect::<Vec<_>>(), ["3", "4"], "{caches:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connection_pivot_tables_need_their_connections() {
|
||||
// No connections part: the caches name connections that aren't there.
|
||||
|
||||
Reference in New Issue
Block a user