diff --git a/crates/xlsx/README.md b/crates/xlsx/README.md index 1dc30574d..367056ef9 100644 --- a/crates/xlsx/README.md +++ b/crates/xlsx/README.md @@ -36,8 +36,9 @@ were. Part names and relationship ids are kept, so a file read and written witho where it did, and writing it twice gives the same bytes. The calculation chain is dropped (Excel rebuilds it). `XlsxKind` is the workbook part's content type (`.xlsx`, `.xlsm`, `.xltx`, `.xltm`). Typed worksheet features (`SheetPart::features`), each mirroring its element with optional -attributes and the unnamed ones kept: sheet views (panes, selections), print setup (options, -margins, page setup, header and footer, page breaks), data validation (also read from the `x14` +attributes and the unnamed ones kept: sheet views (panes, selections), the default sizes +(`sheetFormatPr`), print setup (options, margins, page setup, header and footer, page breaks), +data validation (also read from the `x14` extension), conditional formats (colour scales, data bars, icon sets, rule extensions kept), hyperlinks (with their URLs), the legacy drawing. Comments (`Comments`, from the comments part) and the note shapes of the VML drawing (`NoteShape`). A sheet that gains comments and has no VML @@ -83,8 +84,11 @@ for new pictures (`vm = i + 1`) and the `XLDAPR` dynamic-array metadata (`cm = 1 The parts this crate writes use the transitional namespaces; a strict file's kept parts stay as they were. A binary workbook is not written. -A binary workbook gives the same `Workbook`, strings, rows and merged cells. Its styles are not -read (each cell keeps its format index). Its cell formulas are decoded from their tokens into the +A binary workbook gives the same `Workbook`, strings, rows (with their heights, outline levels, +hidden flags and styles), columns, default sizes and merged cells. Its styles are not read (each +cell keeps its format index). The descent Excel records for rows and the sheet +(`x14ac:dyDescent`) is in alternate-content blocks, which are skipped. Its cell formulas are +decoded from their tokens into the text an `.xlsx` file holds (`_xlfn.` and `_xlpm.` names as written, no implicit `@`), with `FormulaKind::Shared` or `FormulaKind::Array` and their ranges, and `BrtCellMeta` as the cell metadata index. Every cell of a shared formula gets its own text, decoded at the cell, since the @@ -102,8 +106,9 @@ other workbooks, Python code stored outside the cell, data tables. The XLSB func (40 million records, 4 million cells and as many shared strings, 256 MiB of text, as `craft-xls` allows). `binary_formula_text`: decoded formula text over the workbook (128 MiB); past it, formulas keep their saved results. A formula's tokens are bounded at 16 KB, its - nesting at 256 levels and an array constant at 65,536 values. Fixed caps: 1024 sheets, 100,000 merged ranges per sheet, 64 nested - extension blocks. + nesting at 256 levels and an array constant at 65,536 values. Fixed caps: 1024 sheets, 100,000 + merged ranges and 16,384 column records per sheet, 64 nested extension blocks. A row taller + than 8,192 twips keeps the default height. - Cells and rows outside the 1,048,576 × 16,384 grid are dropped and counted. SpreadsheetML is read leniently: a damaged optional part is a warning, a damaged worksheet keeps diff --git a/crates/xlsx/src/features.rs b/crates/xlsx/src/features.rs index 17c2c2aab..9bae562b5 100644 --- a/crates/xlsx/src/features.rs +++ b/crates/xlsx/src/features.rs @@ -1,6 +1,7 @@ -//! The worksheet's typed features, each mirroring its SpreadsheetML element: sheet views, print -//! setup, data validation, conditional formatting, hyperlinks (ECMA-376 Part 1, 18.3), and the -//! comments part (18.7) with the legacy note shapes Excel draws comments with. +//! The worksheet's typed features, each mirroring its SpreadsheetML element: sheet views, the +//! sheet's default sizes, print setup, data validation, conditional formatting, hyperlinks +//! (ECMA-376 Part 1, 18.3), and the comments part (18.7) with the legacy note shapes Excel draws +//! comments with. //! //! An attribute the file may leave out is an `Option` (`None`: not written, the schema's //! default applies), so a feature reads and writes back as it was. Attributes a type does not @@ -213,6 +214,27 @@ impl SheetView { } } +// ------------------------------------------------------------------------------- sheet format + +element! { + /// ``: the sheet's default column width and row height, and its outline depth. + pub struct SheetFormat { + /// Characters of the default font's maximum digit width, padding excluded (8 when absent). + base_col_width: u32 = "baseColWidth", + /// Characters, padding included, like [`crate::Column::width`]. + default_col_width: f64 = "defaultColWidth", + /// Points. + default_row_height: f64 = "defaultRowHeight", + custom_height: bool = "customHeight", + /// Rows are hidden unless they say otherwise. + zero_height: bool = "zeroHeight", + thick_top: bool = "thickTop", + thick_bottom: bool = "thickBottom", + outline_level_row: u32 = "outlineLevelRow", + outline_level_col: u32 = "outlineLevelCol", + } +} + // ------------------------------------------------------------------------------- printing element! { @@ -643,6 +665,8 @@ impl Hyperlink { #[derive(Clone, Debug, Default, PartialEq)] pub struct Features { pub views: Vec, + /// `sheetFormatPr`. + pub format: Option, pub print: Print, pub validations: Option, pub conditional_formats: Vec, @@ -660,6 +684,7 @@ impl Features { let attrs_of = |n: &str| root.child(n); Features { views: root.child("sheetViews").map(|v| v.children("sheetView").map(SheetView::read).collect()).unwrap_or_default(), + format: attrs_of("sheetFormatPr").map(SheetFormat::read_attrs), print: Print { options: attrs_of("printOptions").map(PrintOptions::read_attrs), margins: attrs_of("pageMargins").map(PageMargins::read_attrs), @@ -708,6 +733,7 @@ impl Features { let p = &self.print; vec![ ("sheetViews", Vec::from_iter((!self.views.is_empty()).then_some(views))), + ("sheetFormatPr", Vec::from_iter(self.format.as_ref().map(|f| el("sheetFormatPr", &|e| f.write_attrs(e))))), ("conditionalFormatting", self.conditional_formats.iter().map(ConditionalFormatting::to_el).collect()), ("dataValidations", Vec::from_iter(validations)), ("hyperlinks", Vec::from_iter((!self.hyperlinks.is_empty()).then_some(links))), diff --git a/crates/xlsx/src/lib.rs b/crates/xlsx/src/lib.rs index 5a64ae0ab..9105ba886 100644 --- a/crates/xlsx/src/lib.rs +++ b/crates/xlsx/src/lib.rs @@ -75,8 +75,8 @@ pub use document::{Document, DrawingPart, KeptPart, PivotCachePart, PivotTablePa pub use drawing::{Anchor, Drawing, Item, Marker, ObjectKind, Placement}; pub use features::{ Break, CfRule, Cfvo, ColorScale, Comment, Comments, ConditionalFormatting, DataBar, DataValidation, DataValidations, Features, HeaderFooter, - Hyperlink, IconSet, NoteShape, PageMargins, PageSetup, Pane, Print, PrintOptions, Selection, SheetView, Table, TableColumn, TableStyleInfo, - notes_vml, parse_sqref, range_ref, + Hyperlink, IconSet, NoteShape, PageMargins, PageSetup, Pane, Print, PrintOptions, Selection, SheetFormat, SheetView, Table, TableColumn, + TableStyleInfo, notes_vml, parse_sqref, range_ref, }; pub use package::{Package, is_xlsb}; pub use pivot::{ diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 2fdde59be..20953cbad 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -1,7 +1,8 @@ -//! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheet cells, from -//! their records. Cell formulas are decoded from their tokens ([`formula`]) into the text an -//! `.xlsx` file holds; one that can't be keeps only its saved result, with a warning saying why. -//! Styles are not read. A record that does not parse fails the read. +//! Binary workbooks ([MS-XLSB]): the workbook part, shared strings and worksheets (rows, cells, +//! columns, default sizes, merged cells), from their records. Cell formulas are decoded from +//! their tokens ([`formula`]) into the text an `.xlsx` file holds; one that can't be keeps only +//! its saved result, with a warning saying why. Styles are not read. A record that does not parse +//! fails the read. mod expr; mod formula; @@ -13,7 +14,8 @@ use std::sync::Arc; use craft_opc::{ContentTypes, Rels}; use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS, Range, Visibility}; -use crate::sheet::{Cell, CellFormula, CellValue, FormulaKind, Row, SheetPart}; +use crate::features::SheetFormat; +use crate::sheet::{Cell, CellFormula, CellValue, Column, FormulaKind, Row, SheetPart}; use crate::strings::StringItem; use crate::workbook::{SheetEntry, SheetKind, Workbook}; use crate::{Error, Limits, Warnings}; @@ -28,6 +30,10 @@ pub(crate) const STRINGS: &str = "application/vnd.ms-excel.sharedStrings"; const MAX_SHEETS: usize = 1024; /// Most merged ranges in one worksheet. const MAX_MERGES: usize = 100_000; +/// Most column records in one worksheet: one per column. +const MAX_COLUMNS: usize = MAX_COLS as usize; +/// The tallest row, in twips (409.6 points). +const MAX_ROW_TWIPS: u16 = 8192; /// Defined names (Excel's hidden ones for newer functions included) read at most; past it, the /// workbook's formulas keep their saved results. const MAX_NAMES: usize = 65_536; @@ -731,7 +737,87 @@ fn value(id: u16, b: &mut Bytes<'_>, strings: usize, limits: &Limits, budget: &B Ok(value) } -/// A worksheet part: rows (BrtRowHdr) and cells inside BrtBeginSheetData, merged cells after. +/// `BrtRowHdr` after `rw`, up to its column spans: the row's height, flags and style, as the +/// `.xlsx` row Excel writes. `miyRw` is the height of every row: it is `ht` when set by hand +/// (`fUnsynced`) or when it differs from the sheet's default, and past 8192 twips it is ignored. +fn row_header(index: u32, b: &mut Bytes<'_>, default_twips: Option) -> Result { + let ixfe = b.u32()?; + let twips = b.u16()?; + let [borders, flags, phonetic] = [b.u8()?, b.u8()?, b.u8()?]; + let custom = flags & 0x20 != 0; + let height = (twips <= MAX_ROW_TWIPS && (custom || default_twips.is_some_and(|d| d != twips))).then(|| f64::from(twips) / 20.0); + let mut attrs = vec![]; + for (bit, byte, name) in [(1, borders, "thickTop"), (2, borders, "thickBot"), (1, phonetic, "ph")] { + if byte & bit != 0 { + attrs.push((name.to_string(), "1".to_string())); + } + } + Ok(Row { + index, + height, + custom_height: custom && height.is_some(), + hidden: flags & 0x10 != 0, + outline_level: flags & 7, + collapsed: flags & 0x08 != 0, + style: (flags & 0x40 != 0).then_some(ixfe), + cells: vec![], + attrs, + }) +} + +/// `BrtWsFmtInfo`: `sheetFormatPr` as Excel writes it, and the default row height in twips. +/// `dxGCol` is the default width in 256ths of a character (`0xFFFFFFFF`: none set); +/// `cchDefColWidth` is the base width, left out at its default of 8. +fn sheet_format(b: &mut Bytes<'_>) -> Result<(SheetFormat, u16), Error> { + let width = b.u32()?; + let base = b.u16()?; + let twips = b.u16()?; + let flags = b.u16()?; + let rows = b.u8()?; + let cols = b.u8()?; + let set = |bit: u16| (flags & bit != 0).then_some(true); + let level = |n: u8| (n > 0).then_some(u32::from(n.min(7))); + let format = SheetFormat { + base_col_width: (base != 8).then_some(u32::from(base)), + default_col_width: (width != u32::MAX).then(|| f64::from(width) / 256.0), + default_row_height: Some(f64::from(twips) / 20.0), + custom_height: set(1), + zero_height: set(2), + thick_top: set(4), + thick_bottom: set(8), + outline_level_row: level(rows), + outline_level_col: level(cols), + attrs: vec![], + }; + Ok((format, twips)) +} + +/// `BrtColInfo`: `None` for a range outside the grid or backwards. `coldx` is the width in 256ths +/// of a character, which is the `.xlsx` width exactly. +fn column(b: &mut Bytes<'_>) -> Result, Error> { + let first = b.u32()?; + let last = b.u32()?; + let width = b.u32()?; + let style = b.u32()?; + let flags = b.u16()?; + if first > last || last >= MAX_COLS { + return Ok(None); + } + Ok(Some(Column { + first, + last, + width: Some(f64::from(width) / 256.0), + custom_width: flags & 2 != 0, + best_fit: flags & 4 != 0, + hidden: flags & 1 != 0, + outline_level: ((flags >> 8) & 7) as u8, + collapsed: flags & 0x1000 != 0, + style: (style != 0).then_some(style), + })) +} + +/// A worksheet part: the sheet's default sizes (BrtWsFmtInfo) and columns (BrtColInfo) before +/// BrtBeginSheetData, rows (BrtRowHdr) and cells inside it, merged cells after. /// Formulas refer to the workbook's `links`; with none, each keeps its saved result. #[allow(clippy::too_many_arguments)] pub(crate) fn worksheet( @@ -750,6 +836,9 @@ pub(crate) fn worksheet( let mut data_seen = false; let mut row: Option = None; let mut col = None; + // The default row height in twips: a row of another height has `ht` even when it was not set + // by hand (a large font, a thick border), as Excel writes it in `.xlsx`. + let mut default_twips: Option = None; // BrtCellMeta applies to the cell record that follows it (a dynamic-array formula), as `cm` // does in `.xlsx`: a one-based index into the metadata part's cell metadata. Excel may write // the cell's BrtValueMeta (a rich value, such as a spill error) in between. @@ -818,15 +907,14 @@ pub(crate) fn worksheet( if r >= MAX_ROWS || row.as_ref().is_some_and(|previous| r <= previous.index) { return Err(invalid("row is out of bounds or unordered")); } - // ixfe, miyRw and the flags are not read. - b.take(9)?; + let next = row_header(r, &mut b, default_twips)?; let spans = b.u32()?; if spans > 16 { return Err(invalid("too many row spans")); } b.take(spans as usize * 8)?; b.finish()?; - if let Some(previous) = row.replace(Row { index: r, ..Row::default() }) { + if let Some(previous) = row.replace(next) { each(previous)?; } sheet.rows += 1; @@ -885,6 +973,30 @@ pub(crate) fn worksheet( }); col = Some(c); } + 485 => { + if data_seen { + return Err(invalid("sheet format after worksheet data")); + } + let (format, twips) = sheet_format(&mut b)?; + b.finish()?; + default_twips = Some(twips); + sheet.features.format = Some(format); + } + 60 => { + if data_seen { + return Err(invalid("column after worksheet data")); + } + let column = column(&mut b)?; + b.finish()?; + let Some(column) = column else { + warnings.warn("invalid XLSB column ranges were skipped"); + continue; + }; + if sheet.columns.len() >= MAX_COLUMNS { + return Err(Error::TooLarge(format!("XLSB exceeds {MAX_COLUMNS} column records"))); + } + sheet.columns.push(column); + } 176 => { if data { return Err(invalid("merge inside worksheet data")); diff --git a/crates/xlsx/tests/xlsb.rs b/crates/xlsx/tests/xlsb.rs index ee9ac3afd..0cf2c5c14 100644 --- a/crates/xlsx/tests/xlsb.rs +++ b/crates/xlsx/tests/xlsb.rs @@ -5,7 +5,7 @@ mod common; use common::{read, read_with}; use craft_xlsx::testing::zip; -use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Range, SheetKind, Value, Visibility, is_xlsb}; +use craft_xlsx::{CellError, CellRef, Error, FormulaKind, Limits, Package, Range, Row, SheetKind, SheetPart, Value, Visibility, is_xlsb}; const ROOT: &str = r#""#; const RELS: &str = r#""#; @@ -930,3 +930,130 @@ fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() { assert_eq!(sheet.cell_metadata.iter().collect::>(), [(&at("C1"), &1)]); assert!(warned(&wb, "missing from the file"), "{:?}", wb.warnings); } + +/// `BrtRowHdr` with its style, height and three flag bytes, and no column spans. +fn row_hdr(r: u32, ixfe: u32, twips: u16, flags: [u8; 3]) -> Vec { + record(0, &[&r.to_le_bytes()[..], &ixfe.to_le_bytes(), &twips.to_le_bytes(), &flags, &0u32.to_le_bytes()].concat()) +} +/// `BrtColInfo`. +fn col_info(first: u32, last: u32, coldx: u32, ixfe: u32, flags: u16) -> Vec { + record(60, &[&first.to_le_bytes()[..], &last.to_le_bytes(), &coldx.to_le_bytes(), &ixfe.to_le_bytes(), &flags.to_le_bytes()].concat()) +} +/// `BrtWsFmtInfo`. +fn ws_fmt_info(dx: u32, base: u16, twips: u16, flags: u16, levels: [u8; 2]) -> Vec { + record(485, &[&dx.to_le_bytes()[..], &base.to_le_bytes(), &twips.to_le_bytes(), &flags.to_le_bytes(), &levels].concat()) +} +/// A worksheet with `head` before its data (the sheet format and columns) and `rows` in it. +fn sized_sheet(head: &[u8], rows: &[u8]) -> Vec { + [&record(129, &[])[..], head, &record(145, &[]), rows, &record(146, &[]), &record(130, &[])].concat() +} +fn sizes_of(bytes: &[u8]) -> Result<(Vec, SheetPart, Vec), Error> { + let mut p = Package::open(bytes, &Limits::default())?; + let wb = p.workbook()?; + let mut rows = vec![]; + let part = p.sheet(&wb.sheets[0], |r| { + rows.push(r); + Ok(()) + })?; + Ok((rows, part, p.warnings().to_vec())) +} +fn xml_sheet(xml: &str) -> (Vec, SheetPart) { + let types = r#""#; + let root = r#""#; + let rels = r#""#; + let book = r#""#; + let bytes = zip(&[ + ("[Content_Types].xml", types.as_bytes()), + ("_rels/.rels", root.as_bytes()), + ("xl/workbook.xml", book.as_bytes()), + ("xl/_rels/workbook.xml.rels", rels.as_bytes()), + ("xl/sheet.xml", xml.as_bytes()), + ]); + let (rows, part, _) = sizes_of(&bytes).unwrap(); + (rows, part) +} + +/// The binary sheet below as Excel spells it in `.xlsx`. Excel writes `ht` for a row whose +/// height differs from the default even when it was not set by hand. +const SIZES_XML: &str = r#"1"#; + +fn sizes_bin() -> Vec { + let head = [ + ws_fmt_info(3225, 10, 285, 0x0A, [2, 1]), + record(390, &[]), + col_info(2, 2, 5273, 0, 0x0002), + col_info(3, 5, 0, 0, 0x0103), + col_info(7, 7, 2321, 7, 0), + col_info(8, 8, 6075, 0, 0x0006), + col_info(9, 9, 2321, 0, 0x1000), + col_info(16_383, 16_383, 7833, 0, 0x0002), + record(391, &[]), + ] + .concat(); + let rows = [ + row_hdr(0, 0, 285, [0, 0, 0]), + cell(2, 0, &((1u32 << 2) | 2).to_le_bytes()), + row_hdr(1, 0, 600, [0, 0x20, 0]), + row_hdr(2, 0, 533, [0, 0, 0]), + row_hdr(3, 0, 285, [0, 0x10, 0]), + row_hdr(4, 0, 300, [3, 0, 0]), + row_hdr(5, 0, 285, [0, 2, 0]), + row_hdr(6, 0, 285, [0, 0x11, 0]), + row_hdr(7, 0, 285, [0, 0x08, 0]), + row_hdr(8, 3, 285, [0, 0x40, 0]), + // A style without fGhostDirty is not applied. + row_hdr(9, 5, 285, [0, 0, 0]), + row_hdr(10, 0, 285, [0, 0, 1]), + // Past 8192 twips the height is ignored, set by hand or not. + row_hdr(11, 0, 9000, [0, 0x20, 0]), + ] + .concat(); + sized_sheet(&head, &rows) +} + +#[test] +fn row_heights_and_column_widths_are_read_as_their_xlsx_spelling() { + let (rows, part, _) = sizes_of(&file(&sizes_bin())).unwrap(); + let (xml_rows, xml_part) = xml_sheet(SIZES_XML); + assert_eq!(rows, xml_rows); + assert_eq!(part.columns, xml_part.columns); + assert_eq!(part.features.format, xml_part.features.format); + // A few of the conversions, spelled out. + assert_eq!((rows[1].height, rows[1].custom_height), (Some(30.0), true)); + assert_eq!((rows[2].height, rows[2].custom_height), (Some(26.65), false)); + assert_eq!(rows[8].style, Some(3)); + assert_eq!(part.columns[0].width, Some(5273.0 / 256.0)); + let format = part.features.format.clone().unwrap(); + assert_eq!((format.default_col_width, format.default_row_height), (Some(12.59765625), Some(14.25))); + // Written as SpreadsheetML, the binary sheet has its default sizes and columns. + let written = String::from_utf8(part.to_xml(&rows)).unwrap(); + assert!(written.contains(r#""#), "{written}"); + assert!(written.contains(r#""#), "{written}"); +} + +#[test] +fn damaged_sizes_are_skipped_or_refused() { + // A column range outside the grid, or backwards, is skipped with a warning. + let head = [record(390, &[]), col_info(0, 16_384, 2321, 0, 0), col_info(5, 4, 2321, 0, 0), col_info(1, 1, 512, 0, 2), record(391, &[])].concat(); + let (_, part, warnings) = sizes_of(&file(&sized_sheet(&head, &row(0)))).unwrap(); + assert_eq!(part.columns.len(), 1); + assert_eq!((part.columns[0].first, part.columns[0].width), (1, Some(2.0))); + assert!(warnings.iter().any(|w| w.contains("column ranges")), "{warnings:?}"); + // Without a sheet format, only a height set by hand is a row's `ht`. + let (rows, _, _) = sizes_of(&file(&sized_sheet(&[], &[row_hdr(0, 0, 300, [0, 0, 0]), row_hdr(1, 0, 400, [0, 0x20, 0])].concat()))).unwrap(); + assert_eq!((rows[0].height, rows[1].height), (None, Some(20.0))); + // More column records than columns are refused. + let many = [&record(390, &[])[..], &col_info(0, 0, 2321, 0, 0).repeat(16_385), &record(391, &[])].concat(); + assert!(matches!(sizes_of(&file(&sized_sheet(&many, &[]))), Err(Error::TooLarge(_)))); + // A sheet format or column after the data, or a record of the wrong size, fails the read. + for (why, sheet) in [ + ("format after data", worksheet(&[], &ws_fmt_info(u32::MAX, 8, 285, 0, [0, 0]))), + ("column after data", worksheet(&[], &col_info(0, 0, 2321, 0, 0))), + ("short column", sized_sheet(&record(60, &[0; 16]), &[])), + ("short format", sized_sheet(&record(485, &[0; 11]), &[])), + ("long row header", sized_sheet(&[], &record(0, &[0; 18]))), + ] { + assert!(matches!(sizes_of(&file(&sheet)), Err(Error::Invalid(_))), "{why}"); + } +}