diff --git a/crates/model/src/features/print.rs b/crates/model/src/features/print.rs index 694c2ac6d..c4cf82bad 100644 --- a/crates/model/src/features/print.rs +++ b/crates/model/src/features/print.rs @@ -53,6 +53,11 @@ pub struct PrintSettings { /// sheet is more than one page wide and tall. #[serde(default)] pub page_order: PageOrder, + /// Page Setup › Page › First page number: the number the sheet's first printed page takes, + /// or `None` for Automatic (XLSX `pageSetup firstPageNumber` with `useFirstPageNumber`). Left + /// out of a JSON workbook when Automatic, so files without it are written as before. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub first_page_number: Option, } fn yes() -> bool { @@ -187,6 +192,7 @@ impl Default for PrintSettings { hf_scale_with_doc: true, hf_align_with_margins: true, page_order: PageOrder::DownThenOver, + first_page_number: None, } } } @@ -281,4 +287,17 @@ mod tests { p = serde_json::from_value(saved).unwrap(); assert_eq!(p, PrintSettings::default()); } + + #[test] + fn first_page_number_is_saved_only_when_set() { + // Automatic writes nothing, so a JSON workbook without a first page number is unchanged and + // one saved before the field existed loads as Automatic. + let saved = serde_json::to_value(PrintSettings::default()).unwrap(); + assert!(saved.get("first_page_number").is_none(), "{saved}"); + assert_eq!(serde_json::from_value::(saved).unwrap().first_page_number, None); + let p = PrintSettings { first_page_number: Some(7), ..Default::default() }; + let saved = serde_json::to_value(&p).unwrap(); + assert_eq!(saved["first_page_number"], 7); + assert_eq!(serde_json::from_value::(saved).unwrap(), p); + } } diff --git a/crates/xlsx/src/sheet_read.rs b/crates/xlsx/src/sheet_read.rs index f2ec76683..eedf13120 100644 --- a/crates/xlsx/src/sheet_read.rs +++ b/crates/xlsx/src/sheet_read.rs @@ -267,6 +267,8 @@ pub fn read_sheet(cx: &mut Ctx<'_>, part: &str, name: &str) -> Result<(Sheet, cr } // `downThenOver` when absent (ECMA-376's default); an unknown value reads as that too. sheet.print.page_order = e.attr("pageOrder").and_then(PageOrder::parse).unwrap_or_default(); + // The number counts only when `useFirstPageNumber` says so; otherwise it's Automatic. + sheet.print.first_page_number = e.flag("useFirstPageNumber", false).then(|| e.attr_u32("firstPageNumber")).flatten(); if let Some(p) = e.attr_u32("paperSize").and_then(paper_name) { sheet.print.paper = p.to_string(); } diff --git a/crates/xlsx/src/sheet_write.rs b/crates/xlsx/src/sheet_write.rs index c405667b1..0e63c5524 100644 --- a/crates/xlsx/src/sheet_write.rs +++ b/crates/xlsx/src/sheet_write.rs @@ -409,6 +409,9 @@ fn write_sheet_printing<'a>(s: &mut String, sheet: &'a Sheet, rels: &mut Rels, o if p.scale != 100 && (10..=400).contains(&p.scale) { let _ = write!(s, " scale=\"{}\"", p.scale); } + if let Some(n) = p.first_page_number { + let _ = write!(s, " firstPageNumber=\"{n}\""); + } if let Some((w, h)) = p.fit_to { let _ = write!(s, " fitToWidth=\"{w}\" fitToHeight=\"{h}\""); } @@ -417,6 +420,10 @@ fn write_sheet_printing<'a>(s: &mut String, sheet: &'a Sheet, rels: &mut Rels, o let _ = write!(s, " pageOrder=\"{}\"", p.page_order.id()); } let _ = write!(s, " orientation=\"{}\"", if p.orientation == Orientation::Landscape { "landscape" } else { "portrait" }); + // In the schema's attribute order, as Excel writes it: after the orientation. + if p.first_page_number.is_some() { + s.push_str(" useFirstPageNumber=\"1\""); + } if let Some(id) = kept.page_setup_id() { let _ = write!(s, " r:id=\"{id}\""); } diff --git a/crates/xlsx/src/tests/page_order.rs b/crates/xlsx/src/tests/page_order.rs index b74dc6f96..7e616ace5 100644 --- a/crates/xlsx/src/tests/page_order.rs +++ b/crates/xlsx/src/tests/page_order.rs @@ -73,3 +73,39 @@ fn page_margins_are_written_as_their_decimals() { let xml = sheet_xml(&write_xlsx(&Workbook::new()).unwrap()); assert!(xml.contains(r#""#), "{xml}"); } + +#[test] +fn first_page_number_is_read_and_written_back() { + // #4293: Excel writes Page Setup's First page number 7 as these two attributes; both were lost. + let body = r#""#; + let (wb, _) = read_xlsx(&minimal(body, &[], "", "")).unwrap(); + assert_eq!(wb.sheet(0).unwrap().print.first_page_number, Some(7)); + let bytes = write_xlsx(&wb).unwrap(); + let xml = sheet_xml(&bytes); + assert_eq!(page_setup(&xml), r#""#); + let (back, _) = read_xlsx(&bytes).unwrap(); + assert_eq!(back.sheet(0).unwrap().print.first_page_number, Some(7)); + // In the schema's order with the other attributes: after the scale, before Fit To. + let mut wb = back; + wb.sheet_mut(0).unwrap().print.scale = 80; + wb.sheet_mut(0).unwrap().print.fit_to = Some((1, 2)); + let xml = sheet_xml(&write_xlsx(&wb).unwrap()); + assert!(page_setup(&xml).contains(r#"scale="80" firstPageNumber="7" fitToWidth="1""#), "{xml}"); + // Automatic: no number, a number Excel keeps without using it, and `useFirstPageNumber="0"`. + for body in [ + r#""#, + r#""#, + r#""#, + "", + ] { + let (wb, _) = read_xlsx(&minimal(body, &[], "", "")).unwrap(); + assert_eq!(wb.sheet(0).unwrap().print.first_page_number, None, "{body}"); + let xml = sheet_xml(&write_xlsx(&wb).unwrap()); + assert!(!xml.contains("irstPageNumber"), "{body}: {xml}"); + } + // 1 is a number of its own (Excel's field shows 1, not Automatic) and is kept too. + let mut wb = Workbook::new(); + wb.sheet_mut(0).unwrap().print.first_page_number = Some(1); + let xml = sheet_xml(&write_xlsx(&wb).unwrap()); + assert!(xml.contains(r#"firstPageNumber="1""#) && xml.contains(r#"useFirstPageNumber="1""#), "{xml}"); +} diff --git a/crates/xlsx/src/tests/roundtrip.rs b/crates/xlsx/src/tests/roundtrip.rs index da88df3d9..8f29b28de 100644 --- a/crates/xlsx/src/tests/roundtrip.rs +++ b/crates/xlsx/src/tests/roundtrip.rs @@ -310,6 +310,7 @@ fn sample() -> Workbook { hf_scale_with_doc: false, hf_align_with_margins: true, page_order: PageOrder::OverThenDown, + first_page_number: Some(7), }; s.sparklines.push(Sparkline { cell: at("J2"), source: "B2:B3".into(), kind: SparklineKind::WinLoss, color: Color::Rgb(0x112233), markers: true }); s.tables.push(Table {