From 496aa28ed30f27b01492783d4e0d3b1f0099bc34 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Wed, 28 Oct 2026 12:03:33 -0500 Subject: [PATCH] Fix pivot cache eligibility filtering --- crates/xlsx/src/passthrough/rebase.rs | 4 ++-- crates/xlsx/src/tests/passthrough_tests.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/xlsx/src/passthrough/rebase.rs b/crates/xlsx/src/passthrough/rebase.rs index a80717d60..a4b4ca4e3 100644 --- a/crates/xlsx/src/passthrough/rebase.rs +++ b/crates/xlsx/src/passthrough/rebase.rs @@ -25,8 +25,8 @@ impl<'a> SheetKept<'a> { q.content_type == CT_PIVOT_TABLE && crate::xml::parse(&q.data).ok().and_then(|x| x.attr_u32("cacheId")).is_some_and(|id| !cache_ids.contains(&id)) }; - let mut direct = direct.peekable(); - if direct.peek().is_some() { direct.any(|r| !wb_names.contains_key(&normalize(&r.target))) } else { by_id() } + let direct_is_orphan = direct.into_iter().any(|r| !wb_names.contains_key(&normalize(&r.target))); + direct_is_orphan || by_id() }) .map(|q| normalize(&q.name)) .collect(); diff --git a/crates/xlsx/src/tests/passthrough_tests.rs b/crates/xlsx/src/tests/passthrough_tests.rs index 187c22df8..e073d5988 100644 --- a/crates/xlsx/src/tests/passthrough_tests.rs +++ b/crates/xlsx/src/tests/passthrough_tests.rs @@ -1183,6 +1183,19 @@ fn a_kept_pivot_table_whose_cache_is_not_listed_is_dropped() { assert!(written_pivot_caches(&u).is_empty()); } +#[test] +fn a_kept_pivot_table_with_an_unlisted_cache_id_is_dropped_even_with_a_cache_relationship() { + let src = ext_pivot_package(false, false, "externalLinkPath"); + let mut parts = Unpacked::new(&src).parts; + let table = String::from_utf8(parts.remove("xl/pivotTables/pivotTable1.xml").unwrap()).unwrap().replace(r#"cacheId="1""#, r#"cacheId="99""#); + parts.insert("xl/pivotTables/pivotTable1.xml".into(), table.into_bytes()); + let list: Vec<(&str, &[u8])> = parts.iter().map(|(n, d)| (n.as_str(), d.as_slice())).collect(); + let (wb, _) = read_xlsx(&make_zip(&list)).unwrap(); + let u = Unpacked::new(&write_xlsx(&wb).unwrap()); + u.assert_valid(); + assert!(written_pivot_tables(&u, "Report").is_empty()); +} + // ---------------------------------------------------------------- PivotTables on a data connection or the Data Model /// Bytes standing in for the Data Model (Excel's `xl/model/item.data`).