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 <noreply@anthropic.com>
This commit is contained in:
FredApps
2026-11-19 02:44:34 +01:00
co-authored by Claude Opus 5.5
parent 98ba4e989a
commit 586abdadd2
3 changed files with 28 additions and 1 deletions
+10 -1
View File
@@ -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.");
+5
View File
@@ -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();
+13
View File
@@ -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#"<table:table-row><table:table-cell office:value-type="float" office:value="1"/></table:table-row>"#;
let bytes = ods(&format!(
r#"<table:table table:name="Data">{cell}</table:table><table:table table:name="data">{cell}</table:table><table:table table:name="a/b">{cell}</table:table>"#
));
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#"