diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index 6dd96b042..c5a38b2f6 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -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). diff --git a/crates/engine/src/io/tests.rs b/crates/engine/src/io/tests.rs index ad8ca40f2..9533bc168 100644 --- a/crates/engine/src/io/tests.rs +++ b/crates/engine/src/io/tests.rs @@ -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> { + 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(()) +}