From 586abdadd2bbe8223f3297ee76ffa8e5ec15b7c4 Mon Sep 17 00:00:00 2001 From: FredApps Date: Thu, 19 Nov 2026 02:44:34 +0100 Subject: [PATCH] xlsx: ODS import keeps only the ODS tables, with unique valid names The craft-ods cutover (#4513) built the workbook with Workbook::new(), which already holds an empty Sheet1, and appended the ODS tables after it. Opening an ODS file therefore showed an empty first sheet, and ods_import_keeps_cached_values_and_reports_limits (plus the reader's own sheets[0] tests) failed. It also stopped making sheet names unique and valid, so two tables named Data and data became two sheets with clashing names. Clear the default sheet as the XLSX, XLS and XLSB readers do, and name sheets through SheetNames again, as the reader did before the cutover. That restores SheetNames::insert, which the previous commit removed while it was unused. Co-Authored-By: Claude Opus 5.5 --- crates/xlsx/src/ods.rs | 11 ++++++++++- crates/xlsx/src/read.rs | 5 +++++ crates/xlsx/src/tests/ods_tests.rs | 13 +++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/xlsx/src/ods.rs b/crates/xlsx/src/ods.rs index cd7254110..38f1c5822 100644 --- a/crates/xlsx/src/ods.rs +++ b/crates/xlsx/src/ods.rs @@ -8,6 +8,7 @@ use gridcraft_core::{CellError, CellRef, RangeRef, Value}; use gridcraft_model::{Cell, NumFmt, Sheet, StyleId, Workbook}; use crate::package::MAX_ENTRIES; +use crate::read::SheetNames; use crate::{IoError, ReadReport}; const MIME: &[u8] = b"application/vnd.oasis.opendocument.spreadsheet"; @@ -40,7 +41,10 @@ pub fn read_ods(bytes: &[u8]) -> Result<(Workbook, ReadReport), IoError> { return Err(IoError::TooLarge("ODS contains more than 1024 sheets".into())); } + // `Workbook::new` starts with an empty Sheet1; the ODS tables are the only sheets. let mut wb = Workbook::new(); + wb.sheets.clear(); + let mut names = SheetNames::default(); let mut report = ReadReport::default(); report.warn("ODS data-only import: formulas become their saved results and will not recalculate. Source formatting, charts, images and other features are not imported. Save a new .xlsx file; ODS saving is not supported."); @@ -48,7 +52,12 @@ pub fn read_ods(bytes: &[u8]) -> Result<(Workbook, ReadReport), IoError> { let mut warned_unavailable = false; for src_sheet in book.sheets { - let mut sheet = Sheet::new(&src_sheet.name); + let mut name = names.unique(src_sheet.name.trim_matches('\''), wb.sheets.len()); + if wb.check_sheet_name(&name, None).is_err() { + name = wb.next_sheet_name(); + names.insert(&name); + } + let mut sheet = Sheet::new(name); if !warned_rename && sheet.name != src_sheet.name { warned_rename = true; report.warn("Some ODS sheet names were shortened or renamed to valid, unique XLSX sheet names."); diff --git a/crates/xlsx/src/read.rs b/crates/xlsx/src/read.rs index 6b7d1faf3..7c01cb421 100644 --- a/crates/xlsx/src/read.rs +++ b/crates/xlsx/src/read.rs @@ -65,6 +65,11 @@ pub(crate) struct SheetNames { } impl SheetNames { + /// Records `name` as taken. + pub(crate) fn insert(&mut self, name: &str) { + self.taken.insert(name.to_ascii_lowercase()); + } + /// A valid name for the `i`-th sheet based on `name`, not yet taken; it is recorded as taken. pub(crate) fn unique(&mut self, name: &str, i: usize) -> String { let mut base: String = name.chars().filter(|c| !matches!(c, ':' | '\\' | '/' | '?' | '*' | '[' | ']')).take(31).collect(); diff --git a/crates/xlsx/src/tests/ods_tests.rs b/crates/xlsx/src/tests/ods_tests.rs index bac55efeb..9d9e46632 100644 --- a/crates/xlsx/src/tests/ods_tests.rs +++ b/crates/xlsx/src/tests/ods_tests.rs @@ -111,6 +111,19 @@ fn ods_repeats_merges_sheet_names_and_empty_tail_remain_sparse() { assert_eq!(wb.sheets[1].value(CellRef::new(0, 0)), text("second sheet")); } +#[test] +fn ods_tables_are_the_only_sheets_with_unique_valid_names() { + let cell = r#""#; + let bytes = ods(&format!( + r#"{cell}{cell}{cell}"# + )); + let (wb, report) = read_ods(&bytes).unwrap(); + let names: Vec<&str> = wb.sheets.iter().map(|s| s.name.as_str()).collect(); + assert_eq!(names, ["Data", "data (2)", "ab"]); + assert!(wb.sheets.iter().all(|s| s.value(CellRef::new(0, 0)) == Value::Number(1.0))); + assert!(report.warnings.iter().any(|w| w.contains("renamed"))); +} + #[test] fn ods_dates_and_durations_use_formats_and_unsupported_values_stay_text() { let bytes = row(r#"