Text defaults: a JSON workbook without sheets keeps them on open

When GridCraft JSON has no sheets, the reader replaces the workbook with
Workbook::new() and carries only the macros across, so its text defaults
were dropped and a save lost them. They are carried across too.
This commit is contained in:
alehel
2026-11-14 10:54:29 +01:00
parent a85ee924d6
commit 810b95bf68
2 changed files with 21 additions and 1 deletions
+4 -1
View File
@@ -319,10 +319,13 @@ fn read_plain(name: &str, bytes: &[u8], csv_as_text: bool) -> Result<(Workbook,
let mut wb: Workbook = serde_json::from_slice(bytes).map_err(|e| EngineError::Other(format!("not a GridCraft JSON workbook: {e}")))?;
wb.styles.rebuild_index();
if wb.sheets.is_empty() {
// The file's macros are read after this (`macros::load`), so they come along.
// The file's macros are read after this (`macros::load`), so they come along, and
// so do its text defaults: a save would otherwise lose them.
let macros = std::mem::take(&mut wb.macros_read);
let text_defaults = std::mem::take(&mut wb.text_defaults);
wb = Workbook::new();
wb.macros_read = macros;
wb.text_defaults = text_defaults;
}
// The file's band placement and cell count are derived state: rebuild them from the
// cells themselves, dropping what is outside the sheet grid (#1008).
+17
View File
@@ -881,3 +881,20 @@ fn the_merge_index_holds_a_bounded_number_of_cells() {
sh.merges = vec![RangeRef::new(CellRef::new(0, 0), CellRef::new(1_000_000, 0))];
assert!(merge_index(&sh, rect).is_none());
}
#[test]
fn json_without_sheets_keeps_its_text_defaults() -> std::result::Result<(), Box<dyn std::error::Error>> {
let mut saved = Workbook::new();
saved.sheets.clear();
saved.text_defaults.asian = gridcraft_model::ScriptDefault {
language: Some("ja".into()),
country: Some("JP".into()),
font: Some("Noto Sans CJK JP".into()),
..Default::default()
};
let json = serde_json::to_vec(&saved)?;
let (wb, _) = open_bytes("book.json", &json)?;
assert!(!wb.sheets.is_empty(), "a workbook without sheets opens as a new one");
assert_eq!(wb.text_defaults, saved.text_defaults);
Ok(())
}