Style model: cell padding kept in Alignment

`Alignment::padding: [Option<u16>; 4]` (left, right, top, bottom, in
hundredths of a point; `None` where a file states none) holds an
OpenDocument cell's padding, which LibreOffice lays rows out with. It is
part of style equality and hashing, so styles that differ only by padding
intern apart; serde default, skipped when all `None`, so older JSON reads.

Excel has no cell padding: the XLSX and XLS readers set none, and the
XLSX writer leaves it out (a style that differs from the default only by
padding writes no `<alignment>`). GridCraft does not draw padding yet.
This commit is contained in:
alehel committed 2026-11-11 09:02:08 +00:00
1 parent 632e4a1bc5
commit 09d52cc7cd
5 files changed
+77 -3

No files matched your search

+33
View File
@@ -359,6 +359,17 @@ pub struct Alignment {
pub indent: u8,
/// Degrees -90..=90, or 255 for vertical stacked text.
pub rotation: i16,
/// Space between the cell's edges and its text, left, right, top, bottom, in hundredths
/// of a point; `None` where the file stated none (the application's own). An OpenDocument
/// cell's padding; Excel has none. Not drawn yet.
#[serde(default, skip_serializing_if = "Alignment::no_padding")]
pub padding: [Option<u16>; 4],
}
impl Alignment {
fn no_padding(p: &[Option<u16>; 4]) -> bool {
p.iter().all(Option::is_none)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
@@ -499,6 +510,28 @@ mod tests {
assert!(!t.get(StyleId(999)).font.bold);
}
#[test]
fn padding_keeps_styles_apart_and_survives_json() -> Result<(), Box<dyn std::error::Error>> {
let mut t = StyleTable::default();
let padded = t.derive(StyleId::DEFAULT, |s| s.align.padding = [Some(10), None, Some(0), Some(65535)]);
let other = t.derive(StyleId::DEFAULT, |s| s.align.padding = [Some(10), None, Some(0), Some(1)]);
assert_ne!(padded, StyleId::DEFAULT);
assert_ne!(padded, other);
assert_eq!(t.derive(StyleId::DEFAULT, |s| s.align.padding = [Some(10), None, Some(0), Some(65535)]), padded);
assert_eq!(t.derive(padded, |s| s.align.padding = [None; 4]), StyleId::DEFAULT);
let style = t.get(padded).clone();
let json = serde_json::to_string(&style)?;
assert!(json.contains("padding"), "{json}");
assert_eq!(serde_json::from_str::<Style>(&json)?, style);
// No padding: nothing written, and JSON written before the field existed still reads.
let plain = serde_json::to_string(&Alignment::default())?;
assert!(!plain.contains("padding"), "{plain}");
assert_eq!(serde_json::from_str::<Alignment>(&plain)?, Alignment::default());
Ok(())
}
#[test]
fn a_theme_saved_before_per_script_fonts_still_opens() -> Result<(), Box<dyn std::error::Error>> {
let mut json = serde_json::to_value(Theme::default())?;
+4 -1
View File
@@ -261,6 +261,7 @@ fn read_alignment(a: &El) -> Alignment {
shrink: a.flag("shrinkToFit", false),
indent: a.attr_u32("indent").unwrap_or(0).min(250) as u8,
rotation: 0,
padding: [None; 4],
};
if let Some(r) = a.attr_i64("textRotation") {
al.rotation = match r {
@@ -530,7 +531,9 @@ fn border_xml(b: &Borders) -> String {
}
fn alignment_xml(a: &Alignment) -> Option<String> {
if *a == Alignment::default() {
// SpreadsheetML has no cell padding: it is not written, and a style that differs from the
// default only by padding writes no `<alignment>`.
if (Alignment { padding: [None; 4], ..*a }) == Alignment::default() {
return None;
}
let mut s = String::from("<alignment");
+37 -1
View File
@@ -40,7 +40,7 @@ fn sample() -> Workbook {
s.fill = Fill::solid(Color::Rgb(0xFFEE00));
s.borders.bottom = BorderLine { style: BorderStyle::Double, color: Color::Rgb(0xFF0000) };
s.borders.diag_down = BorderLine::thin();
s.align = Alignment { h: HAlign::Center, v: VAlign::Top, wrap: true, shrink: false, indent: 1, rotation: -30 };
s.align = Alignment { h: HAlign::Center, v: VAlign::Top, wrap: true, shrink: false, indent: 1, rotation: -30, padding: [None; 4] };
s.protection.locked = false;
});
let money = wb.styles.derive(StyleId::DEFAULT, |s| s.num_fmt = NumFmt::new("#,##0.00 \"€\""));
@@ -1741,3 +1741,39 @@ fn remove_personal_information_is_kept_and_honoured() {
assert_eq!(book.child("workbookPr").and_then(|p| p.attr("filterPrivacy")), None);
assert!(String::from_utf8(pkg.read("docProps/core.xml").unwrap().unwrap()).unwrap().contains("Jane Example"));
}
#[test]
fn cell_padding_is_dropped_by_xlsx_and_nothing_else_is() {
// SpreadsheetML has no cell padding: it is not written, and everything else in the style is.
let mut wb = Workbook::new();
let full = wb.styles.derive(StyleId::DEFAULT, |s| {
s.font.bold = true;
s.fill = Fill::solid(Color::Rgb(0x00AAFF));
s.align = Alignment {
h: HAlign::Right,
v: VAlign::Center,
wrap: true,
shrink: false,
indent: 2,
rotation: 45,
padding: [Some(100), Some(200), Some(0), None],
};
});
let only_padding = wb.styles.derive(StyleId::DEFAULT, |s| s.align.padding = [Some(50); 4]);
let mut s = Sheet::new("Padded");
s.cells.set(at("A1"), Cell { value: Value::text("full"), formula: None, style: full, py: None });
s.cells.set(at("B2"), Cell { value: Value::Number(7.0), formula: None, style: only_padding, py: None });
wb.sheets = vec![Arc::new(s)];
let bytes = write_xlsx(&wb).unwrap();
assert_eq!(write_xlsx(&wb).unwrap(), bytes, "output is deterministic");
let (back, _) = read_xlsx(&bytes).unwrap();
let (a, b) = (wb.sheet(0).unwrap(), back.sheet(0).unwrap());
for cell in ["A1", "B2"] {
let (ca, cb) = (a.cells.get(at(cell)).unwrap(), b.cells.get(at(cell)).unwrap());
assert_eq!(ca.value, cb.value, "{cell}");
let mut want = wb.styles.get(ca.style).clone();
want.align.padding = [None; 4];
assert_eq!(back.styles.get(cb.style), &want, "{cell}");
}
}
+1
View File
@@ -339,6 +339,7 @@ fn xf_style(xf: &Xf, fonts: &[Font], formats: &HashMap<u16, String>, table: &[u3
},
indent: a2 & 0x0F,
shrink: a2 & 0x10 != 0,
padding: [None; 4],
};
let b1 = word32(4);
let b2 = word32(8);
+2 -1
View File
@@ -1,6 +1,6 @@
# File-format parity
> **Last reviewed:** 2026-11-01 · **Last updated:** 2026-11-03 · **Change:** minor (a web page's first table opens as a sheet, also when the page is named `.xls`; earlier: `workbookPr filterPrivacy` kept, and honoured on save; earlier: a workbook's R1C1 reference style, `calcPr refMode`, is kept through open and save; earlier: XLSX page margins are written as their decimals, `0.3` rather than `0.30000001192092896`; earlier: Python cells in `.xlsx` as [MS-XLSX] specifies, `_xlfn._xlws.PY(…)` naming code in the Python part, with cached values, and in GridCraft JSON, with limits on open; earlier: equations in xlsx as Office Math with a marker in `descr` so an edit made in Excel reads back, read back only up to the equation source cap, and a brace with a label and a script on its other side keeps both; Symbol font in PDF; earlier: XLSX reads and writes Page Setup's page order, `pageSetup pageOrder`, and PDF export follows it; earlier: chart data tables: native c:dTable import/export; earlier: XLSX workbook-view selection, normalized tab ratios and sheet-edit regressions; fidelity estimates unchanged; earlier: formulas from Google Sheets' XLSX export open as the original formula instead of `IFERROR(__xludf.DUMMYFUNCTION(…),…)` when GridCraft calculates every function in it, and save as it; `file.exportCsv {values: true}` writes raw values for automation; .xls dialog sheets are skipped instead of read as worksheets; `.xls` import reads each sheet's display options from WINDOW2: right-to-left, gridlines, headings, zeros, formulas; XLSX text boxes and shapes keep their paragraphs' right-to-left direction on save; CSV/TSV/TXT can open with every field kept as text via `file.open {csvAsText}`; formulas over Excel's 8,192 characters, and `%` or call chains past the nesting limit, are parse errors instead of stack overflows when a file is opened; XLSX PivotTables read and write `rowGrandTotals`/`colGrandTotals` the way Excel does, so a table saved with only one grand total opens with the right one; HTML export keeps right-to-left; CSV and text import detects likely Windows-1256 Arabic when invalid UTF-8 has Arabic byte runs; other invalid UTF-8 remains Windows-1252; HTML export places pictures and charts, as inline SVG, at their anchors; earlier: `file.exportCsv {values: true}` writes raw values for automation; .xls dialog sheets are skipped instead of read as worksheets; `.xls` import reads each sheet's display options from WINDOW2: right-to-left, gridlines, headings, zeros, formulas; XLSX text boxes and shapes keep their paragraphs' right-to-left direction on save; CSV/TSV/TXT can open with every field kept as text via `file.open {csvAsText}`; formulas over Excel's 8,192 characters, and `%` or call chains past the nesting limit, are parse errors instead of stack overflows when a file is opened; XLSX PivotTables read and write `rowGrandTotals`/`colGrandTotals` the way Excel does, so a table saved with only one grand total opens with the right one; HTML export keeps right-to-left; CSV and text import detects likely Windows-1256 Arabic when invalid UTF-8 has Arabic byte runs; other invalid UTF-8 remains Windows-1252; HTML export places pictures and charts, as inline SVG, at their anchors; CSV and text open detects each column's date order from values that read only one way, warns when every date is ambiguous, and keeps a column with both kinds as text (#2765)) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4
> **Last reviewed:** 2026-11-01 · **Last updated:** 2026-11-11 · **Change:** minor (the style model keeps a cell's padding, for OpenDocument; XLSX and XLS have none, so they read none and the XLSX writer leaves it out; not drawn yet; earlier: a web page's first table opens as a sheet, also when the page is named `.xls`; earlier: `workbookPr filterPrivacy` kept, and honoured on save; earlier: a workbook's R1C1 reference style, `calcPr refMode`, is kept through open and save; earlier: XLSX page margins are written as their decimals, `0.3` rather than `0.30000001192092896`; earlier: Python cells in `.xlsx` as [MS-XLSX] specifies, `_xlfn._xlws.PY(…)` naming code in the Python part, with cached values, and in GridCraft JSON, with limits on open; earlier: equations in xlsx as Office Math with a marker in `descr` so an edit made in Excel reads back, read back only up to the equation source cap, and a brace with a label and a script on its other side keeps both; Symbol font in PDF; earlier: XLSX reads and writes Page Setup's page order, `pageSetup pageOrder`, and PDF export follows it; earlier: chart data tables: native c:dTable import/export; earlier: XLSX workbook-view selection, normalized tab ratios and sheet-edit regressions; fidelity estimates unchanged; earlier: formulas from Google Sheets' XLSX export open as the original formula instead of `IFERROR(__xludf.DUMMYFUNCTION(…),…)` when GridCraft calculates every function in it, and save as it; `file.exportCsv {values: true}` writes raw values for automation; .xls dialog sheets are skipped instead of read as worksheets; `.xls` import reads each sheet's display options from WINDOW2: right-to-left, gridlines, headings, zeros, formulas; XLSX text boxes and shapes keep their paragraphs' right-to-left direction on save; CSV/TSV/TXT can open with every field kept as text via `file.open {csvAsText}`; formulas over Excel's 8,192 characters, and `%` or call chains past the nesting limit, are parse errors instead of stack overflows when a file is opened; XLSX PivotTables read and write `rowGrandTotals`/`colGrandTotals` the way Excel does, so a table saved with only one grand total opens with the right one; HTML export keeps right-to-left; CSV and text import detects likely Windows-1256 Arabic when invalid UTF-8 has Arabic byte runs; other invalid UTF-8 remains Windows-1252; HTML export places pictures and charts, as inline SVG, at their anchors; earlier: `file.exportCsv {values: true}` writes raw values for automation; .xls dialog sheets are skipped instead of read as worksheets; `.xls` import reads each sheet's display options from WINDOW2: right-to-left, gridlines, headings, zeros, formulas; XLSX text boxes and shapes keep their paragraphs' right-to-left direction on save; CSV/TSV/TXT can open with every field kept as text via `file.open {csvAsText}`; formulas over Excel's 8,192 characters, and `%` or call chains past the nesting limit, are parse errors instead of stack overflows when a file is opened; XLSX PivotTables read and write `rowGrandTotals`/`colGrandTotals` the way Excel does, so a table saved with only one grand total opens with the right one; HTML export keeps right-to-left; CSV and text import detects likely Windows-1256 Arabic when invalid UTF-8 has Arabic byte runs; other invalid UTF-8 remains Windows-1252; HTML export places pictures and charts, as inline SVG, at their anchors; CSV and text open detects each column's date order from values that read only one way, warns when every date is ambiguous, and keeps a column with both kinds as text (#2765)) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4
Every format Excel opens or saves, with GridCraft's read and write support, fidelity and tests.
Part of [target-app-parity.md](target-app-parity.md). **File formats: ~50% ready, 80–120 h**
@@ -158,6 +158,7 @@ refresh. Counted under features (Get & Transform, ~10% ready), not here.
| Date | Change | Summary |
|---|---|---|
| 2026-11-11 | minor | Style model: `Alignment::padding` (left, right, top, bottom, hundredths of a point; none where a file states none) keeps an OpenDocument cell's padding; styles differing only by padding stay apart. XLSX and XLS read none and the XLSX writer leaves it out; GridCraft does not draw it yet |
| 2026-11-03 | minor | SYLK (`.slk`) import: values, R1C1 formulas as A1 (shared formulas too) with cached results, number formats and column widths; import only (#1481) |
| 2026-11-03 | minor | Opening a web page (HTML) reads its first table as a sheet, whatever the file is called: a web app's "Export to Excel" saved as `.xls` no longer opens as lines of markup. Values read as in a CSV (or as text with `csvAsText`), `<th>` cells are bold and centred, `<x:Name>` names the sheet, and a page not named `.htm`/`.html` opens with a warning (#3166) |
| 2026-11-03 | minor | XLSX `<workbookPr filterPrivacy="1"/>` (Remove personal information from file properties on save) is read and written back, and while it is on GridCraft saves no author (`dc:creator`) and no company, as Excel leaves them empty (#3089, file part) |