mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-28 17:30:24 +01:00
Merge pull request #4143 from alehel/split/cell-padding
Style model: cell padding kept in Alignment (groundwork for ODS)
This commit is contained in:
5 files changed
+84
-2
No files matched your search
@@ -376,6 +376,17 @@ pub struct Alignment {
|
||||
/// Unspecified inherits in differential styles and uses context for ordinary cells.
|
||||
#[serde(default)]
|
||||
pub reading_order: Option<ReadingOrder>,
|
||||
/// 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)]
|
||||
@@ -559,6 +570,28 @@ mod tests {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[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())?;
|
||||
|
||||
@@ -292,6 +292,7 @@ fn read_alignment(a: &El) -> Alignment {
|
||||
Some(2) => Some(ReadingOrder::RightToLeft),
|
||||
_ => None,
|
||||
},
|
||||
padding: [None; 4],
|
||||
};
|
||||
if let Some(r) = a.attr_i64("textRotation") {
|
||||
al.rotation = match r {
|
||||
@@ -595,7 +596,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");
|
||||
|
||||
@@ -48,6 +48,7 @@ fn sample() -> Workbook {
|
||||
indent: 1,
|
||||
rotation: -30,
|
||||
reading_order: Some(ReadingOrder::RightToLeft),
|
||||
padding: [None; 4],
|
||||
};
|
||||
s.protection.locked = false;
|
||||
});
|
||||
@@ -2241,6 +2242,49 @@ fn remove_personal_information_is_kept_and_honoured() {
|
||||
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,
|
||||
reading_order: None,
|
||||
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");
|
||||
// Only the full style writes `<alignment>`: the padding-only one differs from the default by
|
||||
// nothing SpreadsheetML has, so it writes neither an empty `<alignment/>` nor `applyAlignment`.
|
||||
let mut pkg = Package::open(&bytes).unwrap();
|
||||
let styles = String::from_utf8(pkg.read("xl/styles.xml").unwrap().unwrap()).unwrap();
|
||||
assert_eq!(styles.matches("<alignment").count(), 1, "{styles}");
|
||||
assert_eq!(styles.matches("applyAlignment").count(), 1, "{styles}");
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issue_778_xlsx_xml_import_save_reopen_roundtrip() {
|
||||
let mut wb = Workbook::new();
|
||||
|
||||
@@ -443,6 +443,7 @@ fn style_of(f: &x::CellFormat) -> Style {
|
||||
indent: a.indent,
|
||||
rotation: a.rotation,
|
||||
reading_order: None,
|
||||
padding: [None; 4],
|
||||
},
|
||||
borders: Borders {
|
||||
left: line(b.left),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# File-format parity
|
||||
|
||||
> **Last reviewed:** 2026-11-01 · **Last updated:** 2026-11-14 · **Change:** minor (XLSX fonts keep outlined letters, `<outline/>`, through open and save; GridCraft draws them solid; earlier: PDF honours cell reading direction and mirrors right-to-left sheets; earlier: Arabic PDF export no longer fails when a character or the Arabic font is missing; earlier: Arabic PDF text is shaped with embedded Noto Arabic glyphs beside the non-embedded standard, Symbol and CID fonts; earlier: .xls data validation is read; earlier: .xls is read by the shared craft-xls crate: the workbook opens on the sheet it was saved on, and rich-text (RString) cells are read; earlier: form controls: buttons, scroll bars and spin buttons read and written; 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
|
||||
> **Last reviewed:** 2026-11-01 · **Last updated:** 2026-11-14 · **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: XLSX fonts keep outlined letters, `<outline/>`, through open and save; GridCraft draws them solid; earlier: PDF honours cell reading direction and mirrors right-to-left sheets; earlier: Arabic PDF export no longer fails when a character or the Arabic font is missing; earlier: Arabic PDF text is shaped with embedded Noto Arabic glyphs beside the non-embedded standard, Symbol and CID fonts; earlier: .xls data validation is read; earlier: .xls is read by the shared craft-xls crate: the workbook opens on the sheet it was saved on, and rich-text (RString) cells are read; earlier: form controls: buttons, scroll bars and spin buttons read and written; 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**
|
||||
@@ -161,6 +161,7 @@ refresh. Counted under features (Get & Transform, ~10% ready), not here.
|
||||
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-11-14 | 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-14 | minor | XLSX fonts' outlined (hollow) letters, `<outline/>`, are read and written back after `<strike/>` as Excel writes them, in cell fonts and differential formats (a differential format that turns it off, `<outline val="0"/>`, keeps that too); they were dropped before. Two fonts differing only in it are different styles. Set by `home.formatCells {style: {font: {outline: true}}}`; GridCraft does not draw outlined text yet (it shows solid) |
|
||||
| 2026-11-13 | minor | PDF of a right-to-left sheet keeps explicit Left and Right alignment on the side the grid shows them |
|
||||
| 2026-11-12 | minor | `.xls` files open with their data validation (drop-down lists, input messages, error alerts and the rules), read by craft-xls and mapped as the `.xlsx` reader maps the same rules. A rule Excel would refuse, or one past 65,536 in a workbook, is skipped with a warning (#3634, from #3636) |
|
||||
|
||||
Reference in new issue
Block a user