mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
Merge pull request #1323 from keithcsqueensu/xlsb-sizes
craft-xlsx: read row heights, column widths and default sizes of a binary sheet
This commit is contained in:
5 files changed
+296
-22
No files matched your search
@@ -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,13 +84,15 @@ 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, and the same `Styles`:
|
||||
A binary workbook gives the same `Workbook`, strings, rows (with their heights, outline levels,
|
||||
hidden flags and styles), columns, default sizes and merged cells, and the same `Styles`:
|
||||
number formats, fonts, fills, borders, cell and cell style formats, cell styles, differential
|
||||
formats and the indexed colours, as Excel writes them in `styles.xml` (default fill colours and
|
||||
empty settings left out, a cell style format's `apply*="0"`, a number format with a newer locale tag
|
||||
in its Excel 2007 spelling, as `formatCode` has it). Custom table styles and recent colours are not
|
||||
read; a damaged styles part is a warning and no styles, while one past a cap (65,536 entries in a
|
||||
table, 1,024 gradient stops) or past `binary_text` is too large. Its cell formulas are decoded from
|
||||
table, 1,024 gradient stops) or past `binary_text` is too large. 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, apart from
|
||||
the four ISO/IEC 29500 functions `.xlsx` names bare, no implicit `@`, the spaces and line breaks
|
||||
typed in the formula, numbers as Excel spells them), with `FormulaKind::Shared` or
|
||||
@@ -111,7 +114,8 @@ are this crate's own, apart from `craft-xls`'s.
|
||||
past it, formulas keep their saved results. A formula's tokens are bounded at 16 KB, its
|
||||
nesting at 256 levels and its array constants at 65,536 values in all, each charged before
|
||||
it is held; once the formula text allowance is spent, no formula is decoded. Fixed caps: 1024
|
||||
sheets, 100,000 merged ranges per sheet, 64 nested extension blocks.
|
||||
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
|
||||
|
||||
@@ -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! {
|
||||
/// `<sheetFormatPr>`: 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<SheetView>,
|
||||
/// `sheetFormatPr`.
|
||||
pub format: Option<SheetFormat>,
|
||||
pub print: Print,
|
||||
pub validations: Option<DataValidations>,
|
||||
pub conditional_formats: Vec<ConditionalFormatting>,
|
||||
@@ -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))),
|
||||
|
||||
@@ -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::{
|
||||
|
||||
+124
-10
@@ -1,8 +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.
|
||||
//! The styles part is read into the same [`crate::styles::Styles`] ([`styles`]). 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. The styles part is read into the same
|
||||
//! [`crate::styles::Styles`] ([`styles`]). A record that does not parse fails the read.
|
||||
|
||||
mod expr;
|
||||
mod formula;
|
||||
@@ -15,7 +15,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};
|
||||
@@ -35,6 +36,10 @@ const MAX_SHEETS: usize = 1024;
|
||||
const MAX_BOOKS: usize = 65_536;
|
||||
/// 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;
|
||||
@@ -766,7 +771,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<u16>) -> Result<Row, Error> {
|
||||
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<Option<Column>, 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(
|
||||
@@ -783,8 +868,13 @@ pub(crate) fn worksheet(
|
||||
let mut sheet = SheetPart::default();
|
||||
let mut data = false;
|
||||
let mut data_seen = false;
|
||||
// Every BrtColInfo counts toward the cap, a skipped one too.
|
||||
let mut column_records = 0usize;
|
||||
let mut row: Option<Row> = 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<u16> = 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.
|
||||
@@ -853,15 +943,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;
|
||||
@@ -920,6 +1009,31 @@ 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"));
|
||||
}
|
||||
if column_records >= MAX_COLUMNS {
|
||||
return Err(Error::TooLarge(format!("XLSB exceeds {MAX_COLUMNS} column records")));
|
||||
}
|
||||
column_records += 1;
|
||||
let column = column(&mut b)?;
|
||||
b.finish()?;
|
||||
let Some(column) = column else {
|
||||
warnings.warn("invalid XLSB column ranges were skipped");
|
||||
continue;
|
||||
};
|
||||
sheet.columns.push(column);
|
||||
}
|
||||
176 => {
|
||||
if data {
|
||||
return Err(invalid("merge inside worksheet data"));
|
||||
|
||||
+132
-2
@@ -6,8 +6,8 @@ mod common;
|
||||
use common::{read, read_with};
|
||||
use craft_xlsx::testing::zip;
|
||||
use craft_xlsx::{
|
||||
CellError, CellRef, Color, ColorSource, Error, Fill, FormulaKind, Limits, Package, Pattern, PatternFill, Protection, Range, SheetKind, Styles,
|
||||
Value, Visibility, is_xlsb,
|
||||
CellError, CellRef, Color, ColorSource, Error, Fill, FormulaKind, Limits, Package, Pattern, PatternFill, Protection, Range, Row, SheetKind,
|
||||
SheetPart, Styles, Value, Visibility, is_xlsb,
|
||||
};
|
||||
|
||||
const ROOT: &str = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="book" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="records/book.bin"/></Relationships>"#;
|
||||
@@ -934,6 +934,136 @@ fn a_dynamic_array_kept_as_its_value_has_no_cell_metadata() {
|
||||
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<u8> {
|
||||
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<u8> {
|
||||
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<u8> {
|
||||
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<u8> {
|
||||
[&record(129, &[])[..], head, &record(145, &[]), rows, &record(146, &[]), &record(130, &[])].concat()
|
||||
}
|
||||
fn sizes_of(bytes: &[u8]) -> Result<(Vec<Row>, SheetPart, Vec<String>), 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<Row>, SheetPart) {
|
||||
let types = r#"<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/><Override PartName="/xl/sheet.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/></Types>"#;
|
||||
let root = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="r" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>"#;
|
||||
let rels = r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="s" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="sheet.xml"/></Relationships>"#;
|
||||
let book = r#"<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Sizes" sheetId="1" r:id="s"/></sheets></workbook>"#;
|
||||
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#"<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetFormatPr baseColWidth="10" defaultColWidth="12.59765625" defaultRowHeight="14.25" zeroHeight="1" thickBottom="1" outlineLevelRow="2" outlineLevelCol="1"/><cols><col min="3" max="3" width="20.59765625" customWidth="1"/><col min="4" max="6" width="0" hidden="1" customWidth="1" outlineLevel="1"/><col min="8" max="8" width="9.06640625" style="7"/><col min="9" max="9" width="23.73046875" bestFit="1" customWidth="1"/><col min="10" max="10" width="9.06640625" collapsed="1"/><col min="16384" max="16384" width="30.59765625" customWidth="1"/></cols><sheetData><row r="1"><c r="A1"><v>1</v></c></row><row r="2" ht="30" customHeight="1"/><row r="3" ht="26.65"/><row r="4" hidden="1"/><row r="5" ht="15" thickTop="1" thickBot="1"/><row r="6" outlineLevel="2"/><row r="7" hidden="1" outlineLevel="1"/><row r="8" collapsed="1"/><row r="9" s="3" customFormat="1"/><row r="10"/><row r="11" ph="1"/><row r="12"/></sheetData></worksheet>"#;
|
||||
|
||||
fn sizes_bin() -> Vec<u8> {
|
||||
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#"<sheetFormatPr baseColWidth="10" defaultColWidth="12.59765625" defaultRowHeight="14.25""#), "{written}");
|
||||
assert!(written.contains(r#"<cols><col min="3" max="3" width="20.59765625" customWidth="1"/>"#), "{written}");
|
||||
assert!(written.contains(r#"<row r="2" ht="30" customHeight="1"/>"#), "{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(_))));
|
||||
// Skipped (reversed) column records count toward that cap too.
|
||||
let reversed = [&record(390, &[])[..], &col_info(5, 4, 2321, 0, 0).repeat(16_385), &record(391, &[])].concat();
|
||||
assert!(matches!(sizes_of(&file(&sized_sheet(&reversed, &[]))), 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}");
|
||||
}
|
||||
}
|
||||
|
||||
// Styles ([MS-XLSB] 2.1.7.50): a styles part assembled from records, and the `styles.xml` Excel
|
||||
// writes for the same formats when it saves the workbook as `.xlsx`.
|
||||
|
||||
|
||||
Reference in new issue
Block a user