Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b1fa239
XLSX: legacy formulas (no dynamic-array cm) open with Excel's implici…
neilcawse Oct 11, 2026
abe3fc3
XLSX: long operator chains analysed in full and unfinished legacy ana…
neilcawse Oct 12, 2026
47e883f
XLSX: a legacy union already in parentheses saves back without a seco…
neilcawse Oct 12, 2026
6bd0af0
XLSX: drop the unreachable keep-unions retry; an @ of a bare union sa…
neilcawse Oct 12, 2026
16ff307
XLSX: an @ keeps parentheses the reader would not put back (redundant…
neilcawse Oct 13, 2026
8f9d588
XLSX: a redundant @ keeps a legacy formula legacy; IFERROR and IFNA i…
Oct 14, 2026
3ea9245
XLS: formulas open as legacy formulas with Excel's implicit @ (#410)
Harrison-Booth Oct 14, 2026
982945b
Merge main into fix/xls-legacy-implicit-intersection
Harrison-Booth Oct 16, 2026
aa11201
Merge main into fix/xls-legacy-implicit-intersection
Harrison-Booth Oct 19, 2026
aed6e6c
Share defined-name analysis across formula cells; dedupe parity rows
Harrison-Booth Oct 19, 2026
e32f23f
Docs: say the .xls legacy-formula rule excludes Ctrl+Shift+Enter arrays
Harrison-Booth Oct 20, 2026
45af064
Merge main into fix/xls-legacy-implicit-intersection; keep the legacy…
Harrison-Booth Oct 23, 2026
1c52fb9
Merge main into fix/xls-legacy-implicit-intersection; keep both sides…
Harrison-Booth Oct 25, 2026
0d02e8c
Merge remote-tracking branch 'origin/main' into gc-mg-1367r
Harrison-Booth Oct 26, 2026
8781f17
Merge branch 'main' of https://github.com/storytold/gridcraft into gc…
Harrison-Booth Oct 26, 2026
b3946f8
Implicit @ on a 2-D range intersects row and column, as Excel does (#…
Harrison-Booth Oct 26, 2026
8df436f
Merge main into fix/xls-legacy-implicit-intersection; keep the legacy…
Harrison-Booth Oct 27, 2026
f7742f6
Merge upstream main into fix/xls-legacy-implicit-intersection
Harrison-Booth Oct 28, 2026
cc5c15e
Restore legacy formula export in cell writer
Harrison-Booth Oct 28, 2026
37f463f
Merge main into fix/xls-legacy-implicit-intersection
FredApps Nov 10, 2026
0a851f4
Share a name's analysis only when it replays exactly
FredApps Nov 10, 2026
4bc7a25
Document .xls legacy formulas in the file format parity table
FredApps Nov 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/calc/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ impl<'h> Evaluator<'h> {
if r.height() == 1 && r.start.col <= self.at.col && self.at.col <= r.end.col {
return Some(CellRef::new(r.start.row, self.at.col));
}
// A 2-D range intersects on both the formula's row and column (observed in Excel, on
// another sheet too); one that includes the formula's own cell is a circular reference.
if r.start.row <= self.at.row && self.at.row <= r.end.row && r.start.col <= self.at.col && self.at.col <= r.end.col {
return Some(self.at);
}
None
}

Expand Down
19 changes: 16 additions & 3 deletions crates/calc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1923,8 +1923,8 @@ fn offset_extreme_arguments_return_ref() {
}

/// `@` intersects a reference with the formula's row (a column range) or column (a row range);
/// a 2D range or one the formula does not line up with gives #VALUE!. An array gives its
/// top-left value. Legacy formulas read from files use it where Excel intersects implicitly.
/// a 2D range both (circular when it includes the formula's cell); a range the formula does not
/// line up with gives #VALUE!. An array gives its top-left value. Legacy formulas read from files use it where Excel intersects implicitly.
#[test]
fn implicit_intersection_operator() {
let mut t = T::new();
Expand All @@ -1941,9 +1941,22 @@ fn implicit_intersection_operator() {
t.set("D3", "=SUM(@A1:A3)");
assert_eq!(t.num("B2"), 2f64.tanh());
assert_eq!(t.num("B7"), 5f64.tanh());
for at in ["B3", "B9", "E9"] {
for at in ["B9", "E9"] {
assert_eq!(t.get(at), Value::Error(CellError::Value), "{at}");
}
// A 2D range that includes the formula's own cell is a circular reference (Excel shows 0).
assert_eq!(t.get("B3"), Value::Error(CellError::Circ));
// From another sheet a 2D range intersects on both row and column; outside it is #VALUE!.
t.wb.sheets.push(std::sync::Arc::new(gridcraft_model::Sheet::new("Other")));
for (at, input) in [("B2", "=@Sheet1!A1:C3"), ("C1", "=@Sheet1!A1:C3"), ("B9", "=@Sheet1!A1:C3"), ("E2", "=@Sheet1!A1:C3")] {
t.wb.sheet_mut(1).unwrap().cells.set(c(at), Cell::formula(Formula::new(input)));
}
t.calc.recalc_all(&mut t.wb);
let other = |at: &str| t.wb.sheet(1).unwrap().value(c(at));
assert_eq!(other("B2"), Value::Number(2f64.tanh())); // Sheet1!B2
assert_eq!(other("C1"), Value::Number(0.0));
assert_eq!(other("B9"), Value::Error(CellError::Value));
assert_eq!(other("E2"), Value::Error(CellError::Value));
assert_eq!(t.num("D1"), 7.0);
assert_eq!(t.num("D2"), 2.0);
assert_eq!(t.num("D3"), 3.0);
Expand Down
5 changes: 3 additions & 2 deletions crates/engine/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{EngineError, Result};

mod sylk;

/// The function library's answers to what the XLSX reader and writer need to know about functions
/// The function library's answers to what the XLSX and XLS readers and the XLSX writer need to know about functions
/// (which names are built in, which parameters take a single value).
pub const XLSX_FUNCTIONS: gridcraft_xlsx::FunctionInfo = gridcraft_xlsx::FunctionInfo {
is_builtin: |name| gridcraft_functions::lookup(name).is_some(),
Expand Down Expand Up @@ -271,7 +271,8 @@ fn read_plain(name: &str, bytes: &[u8], csv_as_text: bool) -> Result<(Workbook,
let kind = FileKind::from_path(name);
let sniffed = gridcraft_xlsx::sniff(bytes);
if sniffed == gridcraft_xlsx::Format::Xls {
let (wb, report) = gridcraft_xlsx::read_xls(bytes).map_err(|e| EngineError::Other(format!("We can't open '{name}': {e}")))?;
let (wb, report) =
gridcraft_xlsx::read_xls_with(bytes, XLSX_FUNCTIONS).map_err(|e| EngineError::Other(format!("We can't open '{name}': {e}")))?;
return Ok((wb, report.warnings));
}
if sniffed == gridcraft_xlsx::Format::LegacyBinary {
Expand Down
73 changes: 73 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3664,6 +3664,79 @@ fn xls_import_keeps_formulas_and_never_saves_back_to_xls() {
assert!(s.execute("file.saveBytes", json!({"format": "xlsx"})).is_ok());
}

#[test]
fn xls_formulas_calculate_with_legacy_implicit_intersection() {
// Data!A1 = 11, B1:G1 = 1, A2 = 5, A3 = 7. Report!B1:G1 are filled, so a formula in Report!A1
// that spilled would show #SPILL!. Every cached result is 0, so the values below are GridCraft's.
fn rec(id: u16, payload: &[u8]) -> Vec<u8> {
[&id.to_le_bytes()[..], &(payload.len() as u16).to_le_bytes(), payload].concat()
}
let u16s = |v: &[u16]| v.iter().flat_map(|x| x.to_le_bytes()).collect::<Vec<u8>>();
let bof = |dt: u16| rec(0x0809, &[&0x0600u16.to_le_bytes()[..], &dt.to_le_bytes(), &[0; 12]].concat());
let number = |r: u16, c: u16, n: f64| rec(0x0203, &[&u16s(&[r, c, 0])[..], &n.to_le_bytes()].concat());
let formula = |r: u16, c: u16, rgce: &[u8]| {
rec(0x0006, &[&u16s(&[r, c, 0])[..], &0f64.to_le_bytes(), &[0; 6], &(rgce.len() as u16).to_le_bytes(), rgce].concat())
};
let area3d = |r0: u16, r1: u16, c0: u16, c1: u16| [&[0x3Bu8, 0, 0][..], &u16s(&[r0, r1, c0, c1])].concat();
let a2a3 = area3d(1, 2, 0, 0);
let mut data = bof(0x0010);
data.extend([number(0, 0, 11.0), number(1, 0, 5.0), number(2, 0, 7.0)].concat());
(1..7).for_each(|c| data.extend(number(0, c, 1.0)));
data.extend(rec(0x000A, &[]));
let mut report = bof(0x0010);
(1..7).for_each(|c| report.extend(number(0, c, 0.0)));
// A1 = Data!A1:G1; B2 = Data!$A$2:$A$3*10; C2 = SQRT(Data!$A$2:$A$3) (row 2: SQRT(5));
// H1 = {SUM(Data!$A$2:$A$3*2)}, a one-cell Ctrl+Shift+Enter array.
let cse = [&a2a3[..], &[0x1E, 2, 0, 0x05, 0x22, 1, 4, 0]].concat();
report.extend(formula(0, 0, &area3d(0, 0, 0xC000, 6 | 0xC000)));
report.extend(formula(1, 1, &[&a2a3[..], &[0x1E, 10, 0, 0x05]].concat()));
report.extend(formula(1, 2, &[&a2a3[..], &[0x21, 20, 0]].concat()));
report.extend(formula(0, 7, &[1, 0, 0, 7, 0]));
report.extend(rec(0x0221, &[&u16s(&[0, 0])[..], &[7, 7], &[0; 6], &(cse.len() as u16).to_le_bytes(), &cse].concat()));
report.extend(rec(0x000A, &[]));
let boundsheet = |pos: u32, name: &str| rec(0x0085, &[&pos.to_le_bytes()[..], &[0, 0, name.len() as u8, 0], name.as_bytes()].concat());
let globals = |p1: u32, p2: u32| {
[
bof(0x0005),
boundsheet(p1, "Data"),
boundsheet(p2, "Report"),
rec(0x01AE, &u16s(&[2, 0x0401])),
rec(0x0017, &u16s(&[1, 0, 0, 0])),
rec(0x000A, &[]),
]
.concat()
};
let p1 = globals(0, 0).len() as u32;
let stream = [globals(p1, p1 + data.len() as u32), data, report].concat();
let bytes = gridcraft_xlsx::testing::compound_file(&[("Workbook", &stream)]);

let mut s = s();
s.execute("file.open", json!({"name": "legacy.xls", "base64": crate::io::base64_encode(&bytes)})).unwrap();
s.execute("formulas.calculateNow", json!({})).unwrap();
let report = |s: &Session, a1: &str| s.doc().unwrap().wb.sheets[1].value(CellRef::parse(a1).unwrap());
assert_eq!(report(&s, "A1"), Value::Number(11.0));
assert_eq!(report(&s, "B2"), Value::Number(50.0));
assert_eq!(report(&s, "C2"), Value::Number(5f64.sqrt()));
assert_eq!(report(&s, "H1"), Value::Number(24.0));
// Nothing spilled out of A1 or B2.
assert_eq!(report(&s, "B1"), Value::Number(0.0));
assert_eq!(report(&s, "B3"), Value::Empty);
// Saved as .xlsx, the formulas carry no dynamic-array metadata.
let r = s.execute("file.saveBytes", json!({"format": "xlsx"})).unwrap();
let saved = crate::io::base64_decode(r["base64"].as_str().unwrap()).unwrap();
let (wb, _) = gridcraft_xlsx::read_xlsx(&saved).unwrap();
let f = |a1: &str| wb.sheets[1].cell(CellRef::parse(a1).unwrap()).and_then(|c| c.formula.clone()).unwrap();
assert_eq!(f("A1").text, "@Data!A1:G1");
assert_eq!((f("H1").text.as_str(), f("H1").array), ("SUM(Data!$A$2:$A$3*2)", gridcraft_core::RangeRef::parse("H1")));
let mut sheet = String::new();
std::io::Read::read_to_string(
&mut zip::ZipArchive::new(std::io::Cursor::new(&saved)).unwrap().by_name("xl/worksheets/sheet2.xml").unwrap(),
&mut sheet,
)
.unwrap();
assert!(!sheet.contains("cm=") && !sheet.contains("SINGLE"), "{sheet}");
}

#[test]
fn malformed_xlsb_keeps_the_current_workbook() {
let mut s = s();
Expand Down
33 changes: 32 additions & 1 deletion crates/engine/tests/legacy_array_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,28 @@ fn legacy_unions_names_and_parentheses_calculate_and_save_as_read() {
assert!(!xml.contains("cm=") && !xml.contains("SINGLE") && !xml.contains("t=\"array\""), "{xml}");
}

/// A whole column in a scalar argument of a legacy formula meets the formula's row (issue #1636,
/// from PR #1660): `ISBLANK(B:B)` in row 4 tests `B4`, so the formula shows `C4` instead of
/// spilling into `G5`, and in row 6, where `B6` is blank, shows 0.
#[test]
fn whole_column_in_a_legacy_scalar_argument_meets_the_formulas_row() {
let formulas: Vec<(String, String)> = [
("B4", r#""Thierry""#),
("C4", "7"),
("G4", r#"IF(ISBLANK(B:B),0,IF(B4="Thierry",C4,0))"#),
("G5", "1"),
("G6", r#"IF(ISBLANK(B:B),0,IF(B6="Thierry",C6,0))"#),
]
.iter()
.map(|(a, f)| (a.to_string(), f.to_string()))
.collect();
let s = open(&legacy_xlsx(&formulas, ""));
let at = |a: &str| CellRef::parse(a).unwrap();
assert_eq!(value(&s, at("G4")), Value::Number(7.0), "B:B meets row 4 instead of spilling");
assert_eq!(value(&s, at("G5")), Value::Number(1.0), "nothing spills below G4");
assert_eq!(one_value(&s, at("G6"), "G6").unwrap(), Value::Number(0.0), "B6 is blank");
}

/// One-cell ranges written as ranges (`B1:B1`) outside any `@`, and formulas whose reading
/// reaches the parser's nesting limit (254 nested functions, far past Excel's 64): saved as read,
/// without `@`. Only a one-cell range under an `@` counts as one an edit may have shrunk. All but
Expand Down Expand Up @@ -302,9 +324,18 @@ fn legacy_xlsx(formulas: &[(String, String)], names: &str) -> Vec<u8> {
let esc = |s: &str| s.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;").replace('"', "&quot;");
let mut rows =
String::from(r#"<row r="1"><c r="A1"><v>1</v></c><c r="B1"><v>2</v></c></row><row r="2"><c r="A2"><v>3</v></c><c r="B2"><v>4</v></c></row>"#);
// Cells in one row share its `<row>`, in the order given.
let mut by_row: Vec<(String, String)> = Vec::new();
for (a, f) in formulas {
let r: String = a.chars().filter(char::is_ascii_digit).collect();
rows.push_str(&format!(r#"<row r="{r}"><c r="{a}"><f>{}</f></c></row>"#, esc(f)));
let c = format!(r#"<c r="{a}"><f>{}</f></c>"#, esc(f));
match by_row.iter_mut().find(|(row, _)| *row == r) {
Some((_, cells)) => cells.push_str(&c),
None => by_row.push((r, c)),
}
}
for (r, cells) in by_row {
rows.push_str(&format!(r#"<row r="{r}">{cells}</row>"#));
}
let names = if names.is_empty() { String::new() } else { format!("<definedNames>{names}</definedNames>") };
let parts: [(&str, String); 5] = [
Expand Down
Loading
Loading