Files
gridcraft/crates/engine/tests/sheet_name_quoting.rs
T
Eduardo GarciaandClaude Sonnet 5.5 0b4cc5618f fix #1458: one Excel sheet-name quoting rule for formulas, pivot sources and ADDRESS
Sheet names starting with '.' or reading as R1C1 (R, C, RC, R1C1...) were saved
unquoted, so Excel could not open the file; PivotTable sources and ADDRESS left
cell-like names (A1) bare. The quoting rule, unquoting and sheet splitting now
live once in gridcraft_core::addr and replace the copies in the formula printer,
pivot, ADDRESS, chart, xlsx, Solver, Go To and the Name Box (which now reads ''
as ').

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-19 17:36:40 -03:00

52 lines
2.2 KiB
Rust

//! #1458: sheet names are quoted in references the way Excel quotes them, in formulas, saved
//! files, PivotTable sources and ADDRESS.
use gridcraft_engine::Session;
use gridcraft_engine::core::{CellRef, Value};
use serde_json::{Value as Json, json};
fn run(s: &mut Session, command: &str, params: Json) -> Json {
s.execute(command, params).unwrap()
}
fn formula(s: &Session, cell: &str) -> String {
s.doc().unwrap().wb.active().unwrap().cell(CellRef::parse(cell).unwrap()).unwrap().formula.as_ref().unwrap().text.clone()
}
#[test]
fn formulas_keep_the_quotes_excel_needs_through_a_save() {
let mut s = Session::new();
s.new_workbook();
for name in [".x", "R1C1", "A1", "Bob's"] {
run(&mut s, "home.insertSheet", json!({"name": name}));
}
run(&mut s, "sheet.activate", json!({"sheet": "Sheet1"}));
let input = "='.x'!A1+'R1C1'!A1+'A1'!B1+'Bob''s'!A1";
run(&mut s, "cell.set", json!({"cell": "A1", "input": input}));
assert_eq!(formula(&s, "A1"), input.trim_start_matches('='));
let saved = run(&mut s, "file.saveBytes", json!({"format": "xlsx"}));
let mut reopened = Session::new();
run(&mut reopened, "file.open", json!({"name": "q.xlsx", "base64": saved["base64"]}));
run(&mut reopened, "sheet.activate", json!({"sheet": "Sheet1"}));
assert_eq!(formula(&reopened, "A1"), input.trim_start_matches('='));
}
#[test]
fn address_and_pivot_source_quote_a_sheet_named_like_a_cell() {
let mut s = Session::new();
s.new_workbook();
run(&mut s, "home.insertSheet", json!({"name": "A1"}));
run(&mut s, "sheet.activate", json!({"sheet": "A1"}));
for (cell, input) in [("A1", "K"), ("B1", "V"), ("A2", "a"), ("B2", "1"), ("A3", "b"), ("B3", "2")] {
run(&mut s, "cell.set", json!({"cell": cell, "input": input}));
}
run(&mut s, "cell.set", json!({"cell": "D1", "input": "=ADDRESS(1,1,1,TRUE,\"A1\")"}));
let d1 = s.doc().unwrap().wb.active().unwrap().value(CellRef::parse("D1").unwrap());
assert_eq!(d1, Value::from("'A1'!$A$1"));
run(&mut s, "selection.set", json!({"range": "A2"}));
let pivot = run(&mut s, "insert.pivotTable", json!({"rows": ["K"], "values": ["V"]}));
assert_eq!(pivot["source"], json!("'A1'!$A$1:$B$3"));
}