Compare commits

...
Author SHA1 Message Date
FredApps 6d1125dd0e Native JSON workbook: read and write through craft-gridcraft-format
The `.json`/`.scjson` extension check, the reader and its error text, and the
pretty writer with newer-format macros written back into `macros` now come from
craft-gridcraft-format (storytold/craft-libs#1749), pinned at its PR tip. The
Workbook model, the style-index and cell-band rebuilds after a read, and macro
reading and checks stay here.

The golden tests from the previous commit pass unchanged.

Part of storytold/craft-libs#778.
2026-11-15 21:50:47 +01:00
FredApps 7d4e29372f Native JSON workbook: golden bytes before the move to craft-libs
A workbook with text, numbers, a formula, bold, a number format and a macro is
saved as native JSON. Its bytes, the bytes saved after a newer-format macro is
kept, and the error for a non-workbook file are golden files written by the
current code. Opening the golden file as .json and .scjson and saving it again
gives it back.
2026-11-15 21:48:56 +01:00
11 changed files with 989 additions and 13 deletions
Generated
+11
View File
@@ -937,6 +937,16 @@ name = "craft-fonts-build"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=aa84415bae3b765fef6b7ef42404f66ad1cff9d9#aa84415bae3b765fef6b7ef42404f66ad1cff9d9"
[[package]]
name = "craft-gridcraft-format"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=1ff72a0445f370cdaba156ecb87013badbb58147#1ff72a0445f370cdaba156ecb87013badbb58147"
dependencies = [
"serde",
"serde_json",
"thiserror 2.0.21",
]
[[package]]
name = "craft-i18n"
version = "0.1.0"
@@ -2204,6 +2214,7 @@ name = "gridcraft-engine"
version = "0.19.1"
dependencies = [
"craft-csv",
"craft-gridcraft-format",
"gridcraft-calc",
"gridcraft-chart",
"gridcraft-core",
+2
View File
@@ -39,6 +39,8 @@ craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0
# Delimited text (CSV, TSV) and SYLK; `crates/xlsx/src/csv.rs` and `crates/engine/src/io/sylk.rs`
# turn them into workbooks.
craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" }
# The native JSON workbook (`.json`, `.scjson`): read, written, and kept newer macros written back.
craft-gridcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "1ff72a0445f370cdaba156ecb87013badbb58147", version = "0.1.0" }
# Excel 97-2003 (.xls, BIFF8) workbooks; `crates/xlsx/src/xls.rs` maps them onto the model.
craft-xls = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" }
+1
View File
@@ -22,6 +22,7 @@ thiserror = { workspace = true }
log = { workspace = true }
gridcraft-xlsx = { workspace = true }
craft-csv = { workspace = true }
craft-gridcraft-format = { workspace = true }
gridcraft-chart = { workspace = true }
gridcraft-pdf = { workspace = true }
gridcraft-math = { workspace = true }
+2 -2
View File
@@ -42,7 +42,7 @@ impl FileKind {
"ods" => FileKind::Ods,
"csv" => FileKind::Csv,
"tsv" | "tab" | "txt" => FileKind::Tsv,
"json" | "scjson" => FileKind::Json,
ext if craft_gridcraft_format::is_extension(ext) => FileKind::Json,
"html" | "htm" => FileKind::Html,
"slk" => FileKind::Sylk,
_ => return None,
@@ -316,7 +316,7 @@ fn read_plain(name: &str, bytes: &[u8], csv_as_text: bool) -> Result<(Workbook,
}
match kind {
Some(FileKind::Json) => {
let mut wb: Workbook = serde_json::from_slice(bytes).map_err(|e| EngineError::Other(format!("not a GridCraft JSON workbook: {e}")))?;
let mut wb: Workbook = craft_gridcraft_format::read(bytes).map_err(|e| EngineError::Other(e.to_string()))?;
wb.styles.rebuild_index();
if wb.sheets.is_empty() {
// The file's macros are read after this (`macros::load`), so they come along.
+1 -11
View File
@@ -213,17 +213,7 @@ pub fn json_bytes(wb: &Workbook) -> crate::Result<Vec<u8>> {
for m in &wb.macros {
check(m).map_err(crate::EngineError::Other)?;
}
let bytes = if wb.macros_unread.is_empty() {
serde_json::to_vec_pretty(wb)
} else {
serde_json::to_value(wb).and_then(|mut v| {
if let Some(list) = v.as_object_mut().map(|o| o.entry("macros").or_insert_with(|| Json::Array(vec![]))).and_then(Json::as_array_mut) {
list.extend(wb.macros_unread.iter().cloned());
}
serde_json::to_vec_pretty(&v)
})
};
bytes.map_err(|e| crate::EngineError::Other(e.to_string()))
craft_gridcraft_format::write_with_kept(wb, "macros", &wb.macros_unread).map_err(|e| crate::EngineError::Other(e.to_string()))
}
/// Whether `v` is a macro in a newer format than this build reads.
+1
View File
@@ -6494,6 +6494,7 @@ fn a_script_cannot_switch_windows() {
mod controls;
mod equation;
mod json_golden;
mod macros;
mod objects;
mod python_cells;
+80
View File
@@ -0,0 +1,80 @@
//! Byte-for-byte checks on the native JSON workbook (`.json`, `.scjson`). The files in
//! `crates/engine/tests/golden` were written before the format moved to `craft-gridcraft-format`
//! (storytold/craft-libs). Saving the same workbook must give the same bytes, and opening a
//! golden file and saving it again must give it back.
//! `GRIDCRAFT_BLESS_GOLDEN=1` rewrites them. Only do that for a deliberate format change.
use std::path::PathBuf;
use serde_json::json;
use super::s;
use crate::Session;
fn golden(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("golden").join(name)
}
fn check(name: &str, actual: &[u8]) {
let path = golden(name);
if std::env::var_os("GRIDCRAFT_BLESS_GOLDEN").is_some() {
std::fs::write(&path, actual).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
return;
}
let expected = std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
assert!(expected == actual, "{name} changed ({} bytes, golden has {})", actual.len(), expected.len());
}
fn save(s: &mut Session) -> Vec<u8> {
let r = s.execute("file.saveBytes", json!({"format": "json"})).unwrap();
crate::io::base64_decode(r["base64"].as_str().unwrap()).unwrap()
}
fn open(s: &mut Session, name: &str, bytes: &[u8]) {
s.execute("file.open", json!({"name": name, "base64": crate::io::base64_encode(bytes)})).unwrap();
}
/// A workbook with text, numbers, a formula, a style, a number format, a second sheet and a macro.
fn book() -> Session {
let mut s = s();
for (cell, input) in [("A1", "Item"), ("B1", "Qty"), ("A2", "Äpfel ✓"), ("B2", "3.5"), ("A3", "Pears"), ("B3", "2"), ("B4", "=SUM(B2:B3)")] {
s.execute("cell.set", json!({"cell": cell, "input": input})).unwrap();
}
s.execute("home.bold", json!({"range": "A1:B1"})).unwrap();
s.execute("home.numberFormat", json!({"range": "B2:B4", "format": "0.00"})).unwrap();
s.execute("macro.save", json!({"name": "Fill", "steps": [{"command": "cell.set", "params": {"cell": "C1", "input": "x"}}]})).unwrap();
s
}
#[test]
fn a_save_writes_the_golden_workbook_bytes() {
let mut s = book();
check("book.json", &save(&mut s));
}
#[test]
fn the_golden_workbook_opens_and_saves_back_unchanged() {
let bytes = std::fs::read(golden("book.json")).unwrap();
for name in ["book.json", "book.scjson"] {
let mut s = s();
open(&mut s, name, &bytes);
assert_eq!(super::v(&s, "B4"), gridcraft_core::Value::Number(5.5), "{name}");
assert!(save(&mut s) == bytes, "{name} saved back differently");
}
}
#[test]
fn macros_in_a_newer_format_are_written_back_as_the_file_held_them() {
// The save that splices kept macros back into `macros` goes through a JSON value.
let mut v: serde_json::Value = serde_json::from_slice(&std::fs::read(golden("book.json")).unwrap()).unwrap();
v["macros"].as_array_mut().unwrap().push(json!({"name": "Future", "v": gridcraft_model::MACRO_VERSION + 1, "extra": [1, 2]}));
let mut s = s();
open(&mut s, "kept.json", &serde_json::to_vec_pretty(&v).unwrap());
check("kept.json", &save(&mut s));
}
#[test]
fn what_is_not_a_json_workbook_says_so() {
let err = crate::io::open_bytes("bad.json", b"{\"sheets\": 7}").unwrap_err().to_string();
check("bad.error.txt", err.as_bytes());
}
@@ -0,0 +1,2 @@
# Golden files are compared byte for byte: no line-ending conversion on checkout.
* -text
+1
View File
@@ -0,0 +1 @@
not a GridCraft JSON workbook: invalid type: integer `7`, expected a sequence at line 1 column 12
+440
View File
@@ -0,0 +1,440 @@
{
"sheets": [
{
"name": "Sheet1",
"cells": {
"bands": {
"0": {
"rows": {
"0": {
"0": {
"value": {
"t": "Text",
"v": "Item"
},
"formula": null,
"style": 1
},
"1": {
"value": {
"t": "Text",
"v": "Qty"
},
"formula": null,
"style": 1
}
},
"1": {
"0": {
"value": {
"t": "Text",
"v": "Äpfel ✓"
},
"formula": null,
"style": 0
},
"1": {
"value": {
"t": "Number",
"v": 3.5
},
"formula": null,
"style": 2
}
},
"2": {
"0": {
"value": {
"t": "Text",
"v": "Pears"
},
"formula": null,
"style": 0
},
"1": {
"value": {
"t": "Number",
"v": 2.0
},
"formula": null,
"style": 2
}
},
"3": {
"1": {
"value": {
"t": "Number",
"v": 5.5
},
"formula": {
"text": "SUM(B2:B3)",
"array": null
},
"style": 2
}
}
}
}
},
"len": 7
},
"cols": {},
"rows": {},
"default_col_width": 64.0,
"default_row_height": 20.0,
"merges": [],
"freeze": null,
"tab_color": null,
"visibility": "Visible",
"show_gridlines": true,
"show_headings": true,
"show_formulas": false,
"show_zeros": true,
"show_outline_symbols": true,
"summary_below": true,
"summary_right": true,
"right_to_left": false,
"zoom": 100,
"cond_formats": [],
"validations": [],
"tables": [],
"autofilter": null,
"comments": {},
"hyperlinks": {},
"charts": [],
"images": [],
"shapes": [],
"sparklines": [],
"slicers": [],
"pivots": [],
"protection": null,
"print": {
"orientation": "Portrait",
"paper": "Letter",
"margins": [
0.7,
0.7,
0.75,
0.75,
0.3,
0.3
],
"print_area": null,
"title_rows": null,
"title_cols": null,
"scale": 100,
"fit_to": null,
"gridlines": false,
"headings": false,
"center_h": false,
"center_v": false,
"header": "",
"footer": "",
"row_breaks": [],
"col_breaks": [],
"different_first": false,
"different_odd_even": false,
"first_header": "",
"first_footer": "",
"even_header": "",
"even_footer": "",
"hf_scale_with_doc": true,
"hf_align_with_margins": true,
"page_order": "downThenOver"
},
"view_active": {
"row": 0,
"col": 0
},
"view_top_left": {
"row": 0,
"col": 0
},
"view": "normal"
}
],
"names": [],
"styles": {
"styles": [
{
"font": {
"name": "Calibri",
"size": 11.0,
"bold": false,
"italic": false,
"underline": "None",
"strike": false,
"color": "Auto",
"vert": "Baseline",
"charset": null,
"family": null,
"minor": true
},
"fill": {
"pattern": "None",
"fg": "Auto",
"bg": "Auto"
},
"borders": {
"left": {
"style": "None",
"color": "Auto"
},
"right": {
"style": "None",
"color": "Auto"
},
"top": {
"style": "None",
"color": "Auto"
},
"bottom": {
"style": "None",
"color": "Auto"
},
"diag_down": {
"style": "None",
"color": "Auto"
},
"diag_up": {
"style": "None",
"color": "Auto"
}
},
"align": {
"h": "General",
"v": "Bottom",
"wrap": false,
"shrink": false,
"indent": 0,
"rotation": 0,
"reading_order": null
},
"num_fmt": "General",
"protection": {
"locked": true,
"hidden": false
},
"dxf_stated": {
"bold": false,
"italic": false,
"strike": false,
"underline": false,
"size": false,
"name": false,
"scheme": null,
"protection": false
}
},
{
"font": {
"name": "Calibri",
"size": 11.0,
"bold": true,
"italic": false,
"underline": "None",
"strike": false,
"color": "Auto",
"vert": "Baseline",
"charset": null,
"family": null,
"minor": true
},
"fill": {
"pattern": "None",
"fg": "Auto",
"bg": "Auto"
},
"borders": {
"left": {
"style": "None",
"color": "Auto"
},
"right": {
"style": "None",
"color": "Auto"
},
"top": {
"style": "None",
"color": "Auto"
},
"bottom": {
"style": "None",
"color": "Auto"
},
"diag_down": {
"style": "None",
"color": "Auto"
},
"diag_up": {
"style": "None",
"color": "Auto"
}
},
"align": {
"h": "General",
"v": "Bottom",
"wrap": false,
"shrink": false,
"indent": 0,
"rotation": 0,
"reading_order": null
},
"num_fmt": "General",
"protection": {
"locked": true,
"hidden": false
},
"dxf_stated": {
"bold": false,
"italic": false,
"strike": false,
"underline": false,
"size": false,
"name": false,
"scheme": null,
"protection": false
}
},
{
"font": {
"name": "Calibri",
"size": 11.0,
"bold": false,
"italic": false,
"underline": "None",
"strike": false,
"color": "Auto",
"vert": "Baseline",
"charset": null,
"family": null,
"minor": true
},
"fill": {
"pattern": "None",
"fg": "Auto",
"bg": "Auto"
},
"borders": {
"left": {
"style": "None",
"color": "Auto"
},
"right": {
"style": "None",
"color": "Auto"
},
"top": {
"style": "None",
"color": "Auto"
},
"bottom": {
"style": "None",
"color": "Auto"
},
"diag_down": {
"style": "None",
"color": "Auto"
},
"diag_up": {
"style": "None",
"color": "Auto"
}
},
"align": {
"h": "General",
"v": "Bottom",
"wrap": false,
"shrink": false,
"indent": 0,
"rotation": 0,
"reading_order": null
},
"num_fmt": "0.00",
"protection": {
"locked": true,
"hidden": false
},
"dxf_stated": {
"bold": false,
"italic": false,
"strike": false,
"underline": false,
"size": false,
"name": false,
"scheme": null,
"protection": false
}
}
]
},
"theme": {
"name": "Craft",
"colors": [
16777215,
0,
15263976,
927809,
1400962,
15298866,
1665828,
1023701,
10496915,
5154606,
4618374,
9855101
],
"major_font": "Calibri",
"minor_font": "Calibri",
"major_font_ea": "",
"minor_font_ea": "",
"major_font_cs": "",
"minor_font_cs": "",
"major_font_scripts": [],
"minor_font_scripts": []
},
"date_system": "D1900",
"calc": {
"mode": "Automatic",
"iterative": false,
"max_iterations": 100,
"max_change": 0.001,
"precision_as_displayed": false,
"r1c1": false
},
"active_sheet": 0,
"props": {
"title": "",
"subject": "",
"author": "GridCraft User",
"company": "",
"keywords": "",
"description": ""
},
"protected_structure": false,
"custom_lists": [],
"cell_styles": [],
"vba_project": null,
"macros": [
{
"v": 1,
"name": "Fill",
"description": "",
"inputs": [],
"steps": [
{
"type": "command",
"id": "cell.set",
"params": {
"cell": "C1",
"input": "x"
}
}
],
"output": {
"onConflict": "ask"
},
"permissions": []
}
]
}
+448
View File
@@ -0,0 +1,448 @@
{
"active_sheet": 0,
"calc": {
"iterative": false,
"max_change": 0.001,
"max_iterations": 100,
"mode": "Automatic",
"precision_as_displayed": false,
"r1c1": false
},
"cell_styles": [],
"custom_lists": [],
"date_system": "D1900",
"macros": [
{
"description": "",
"inputs": [],
"name": "Fill",
"output": {
"onConflict": "ask"
},
"permissions": [],
"steps": [
{
"id": "cell.set",
"params": {
"cell": "C1",
"input": "x"
},
"type": "command"
}
],
"v": 1
},
{
"extra": [
1,
2
],
"name": "Future",
"v": 2
}
],
"names": [],
"props": {
"author": "GridCraft User",
"company": "",
"description": "",
"keywords": "",
"subject": "",
"title": ""
},
"protected_structure": false,
"sheets": [
{
"autofilter": null,
"cells": {
"bands": {
"0": {
"rows": {
"0": {
"0": {
"formula": null,
"style": 1,
"value": {
"t": "Text",
"v": "Item"
}
},
"1": {
"formula": null,
"style": 1,
"value": {
"t": "Text",
"v": "Qty"
}
}
},
"1": {
"0": {
"formula": null,
"style": 0,
"value": {
"t": "Text",
"v": "Äpfel ✓"
}
},
"1": {
"formula": null,
"style": 2,
"value": {
"t": "Number",
"v": 3.5
}
}
},
"2": {
"0": {
"formula": null,
"style": 0,
"value": {
"t": "Text",
"v": "Pears"
}
},
"1": {
"formula": null,
"style": 2,
"value": {
"t": "Number",
"v": 2.0
}
}
},
"3": {
"1": {
"formula": {
"array": null,
"text": "SUM(B2:B3)"
},
"style": 2,
"value": {
"t": "Number",
"v": 5.5
}
}
}
}
}
},
"len": 7
},
"charts": [],
"cols": {},
"comments": {},
"cond_formats": [],
"default_col_width": 64.0,
"default_row_height": 20.0,
"freeze": null,
"hyperlinks": {},
"images": [],
"merges": [],
"name": "Sheet1",
"pivots": [],
"print": {
"center_h": false,
"center_v": false,
"col_breaks": [],
"different_first": false,
"different_odd_even": false,
"even_footer": "",
"even_header": "",
"first_footer": "",
"first_header": "",
"fit_to": null,
"footer": "",
"gridlines": false,
"header": "",
"headings": false,
"hf_align_with_margins": true,
"hf_scale_with_doc": true,
"margins": [
0.699999988079071,
0.699999988079071,
0.75,
0.75,
0.30000001192092896,
0.30000001192092896
],
"orientation": "Portrait",
"page_order": "downThenOver",
"paper": "Letter",
"print_area": null,
"row_breaks": [],
"scale": 100,
"title_cols": null,
"title_rows": null
},
"protection": null,
"right_to_left": false,
"rows": {},
"shapes": [],
"show_formulas": false,
"show_gridlines": true,
"show_headings": true,
"show_outline_symbols": true,
"show_zeros": true,
"slicers": [],
"sparklines": [],
"summary_below": true,
"summary_right": true,
"tab_color": null,
"tables": [],
"validations": [],
"view": "normal",
"view_active": {
"col": 0,
"row": 0
},
"view_top_left": {
"col": 0,
"row": 0
},
"visibility": "Visible",
"zoom": 100
}
],
"styles": {
"styles": [
{
"align": {
"h": "General",
"indent": 0,
"reading_order": null,
"rotation": 0,
"shrink": false,
"v": "Bottom",
"wrap": false
},
"borders": {
"bottom": {
"color": "Auto",
"style": "None"
},
"diag_down": {
"color": "Auto",
"style": "None"
},
"diag_up": {
"color": "Auto",
"style": "None"
},
"left": {
"color": "Auto",
"style": "None"
},
"right": {
"color": "Auto",
"style": "None"
},
"top": {
"color": "Auto",
"style": "None"
}
},
"dxf_stated": {
"bold": false,
"italic": false,
"name": false,
"protection": false,
"scheme": null,
"size": false,
"strike": false,
"underline": false
},
"fill": {
"bg": "Auto",
"fg": "Auto",
"pattern": "None"
},
"font": {
"bold": false,
"charset": null,
"color": "Auto",
"family": null,
"italic": false,
"minor": true,
"name": "Calibri",
"size": 11.0,
"strike": false,
"underline": "None",
"vert": "Baseline"
},
"num_fmt": "General",
"protection": {
"hidden": false,
"locked": true
}
},
{
"align": {
"h": "General",
"indent": 0,
"reading_order": null,
"rotation": 0,
"shrink": false,
"v": "Bottom",
"wrap": false
},
"borders": {
"bottom": {
"color": "Auto",
"style": "None"
},
"diag_down": {
"color": "Auto",
"style": "None"
},
"diag_up": {
"color": "Auto",
"style": "None"
},
"left": {
"color": "Auto",
"style": "None"
},
"right": {
"color": "Auto",
"style": "None"
},
"top": {
"color": "Auto",
"style": "None"
}
},
"dxf_stated": {
"bold": false,
"italic": false,
"name": false,
"protection": false,
"scheme": null,
"size": false,
"strike": false,
"underline": false
},
"fill": {
"bg": "Auto",
"fg": "Auto",
"pattern": "None"
},
"font": {
"bold": true,
"charset": null,
"color": "Auto",
"family": null,
"italic": false,
"minor": true,
"name": "Calibri",
"size": 11.0,
"strike": false,
"underline": "None",
"vert": "Baseline"
},
"num_fmt": "General",
"protection": {
"hidden": false,
"locked": true
}
},
{
"align": {
"h": "General",
"indent": 0,
"reading_order": null,
"rotation": 0,
"shrink": false,
"v": "Bottom",
"wrap": false
},
"borders": {
"bottom": {
"color": "Auto",
"style": "None"
},
"diag_down": {
"color": "Auto",
"style": "None"
},
"diag_up": {
"color": "Auto",
"style": "None"
},
"left": {
"color": "Auto",
"style": "None"
},
"right": {
"color": "Auto",
"style": "None"
},
"top": {
"color": "Auto",
"style": "None"
}
},
"dxf_stated": {
"bold": false,
"italic": false,
"name": false,
"protection": false,
"scheme": null,
"size": false,
"strike": false,
"underline": false
},
"fill": {
"bg": "Auto",
"fg": "Auto",
"pattern": "None"
},
"font": {
"bold": false,
"charset": null,
"color": "Auto",
"family": null,
"italic": false,
"minor": true,
"name": "Calibri",
"size": 11.0,
"strike": false,
"underline": "None",
"vert": "Baseline"
},
"num_fmt": "0.00",
"protection": {
"hidden": false,
"locked": true
}
}
]
},
"theme": {
"colors": [
16777215,
0,
15263976,
927809,
1400962,
15298866,
1665828,
1023701,
10496915,
5154606,
4618374,
9855101
],
"major_font": "Calibri",
"major_font_cs": "",
"major_font_ea": "",
"major_font_scripts": [],
"minor_font": "Calibri",
"minor_font_cs": "",
"minor_font_ea": "",
"minor_font_scripts": [],
"name": "Craft"
},
"vba_project": null
}