Resize Table: name formula headers by value and clear pictures under normalized headers

A formula header with formulas shown was named by its source text, so the
header became constant text like "=1+1". Use the value text instead.

Header normalization wrote through cells.set, which left a picture at that
address in place. Use Sheet::set_cell so the picture is removed with it.
This commit is contained in:
Harrison Booth committed 2026-10-24 17:26:54 -05:00
1 parent 79324b6c17
commit 707ce9e14c
2 files changed
+28 -2

No files matched your search

+2 -2
View File
@@ -889,7 +889,7 @@ fn table_resize(s: &mut Session, p: &Json) -> Result<Json> {
let v = sheet.value(at);
let text = match v.as_text() {
Some(x) => x.to_string(),
None => crate::display::cell_text(wb, sheet, at),
None => crate::display::value_text(wb, sheet, at),
};
(!text.trim().is_empty()).then_some(text)
})
@@ -930,7 +930,7 @@ fn table_resize(s: &mut Session, p: &Json) -> Result<Json> {
let constant_text = sheet.value(at).as_text() == Some(col.name.as_str()) && sheet.cell(at).is_some_and(|x| x.formula.is_none());
if !constant_text {
let style = sheet.style_id(at);
sheet.cells.set(at, Cell { value: Value::text(col.name.as_str()), style, ..Default::default() });
sheet.set_cell(at, Cell { value: Value::text(col.name.as_str()), style, ..Default::default() });
}
}
}
@@ -193,3 +193,29 @@ fn resizing_a_table_without_a_header_row_keeps_its_names_and_data() {
assert_eq!(names(&s, 0), ["a", "b", "Column1"]);
assert_eq!(sheet(&s).value(r.start).display(), first);
}
#[test]
fn resized_formula_header_is_named_by_its_value_when_formulas_show() {
let mut s = with_headers("A1:B3", &["x", "y"]);
s.execute("cell.set", json!({"cell": "C1", "input": "=1+1"})).unwrap();
{
let wb = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb);
std::sync::Arc::make_mut(&mut wb.sheets[0]).show_formulas = true;
}
s.execute("table.resize", json!({"table": "T", "range": "A1:C3"})).unwrap();
assert_eq!(names(&s, 0), ["x", "y", "2"]);
assert_eq!(headers(&s, 0), names(&s, 0));
}
#[test]
fn resized_picture_header_is_replaced_and_its_picture_removed() {
let mut s = with_headers("A1:B3", &["x", "y"]);
{
let wb = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb);
let picture = gridcraft_engine::model::CellPicture { data: vec![1, 2, 3], mime: "image/png".into(), alt: String::new() };
std::sync::Arc::make_mut(&mut wb.sheets[0]).set_picture(gridcraft_core::CellRef::new(0, 2), std::sync::Arc::new(picture));
}
s.execute("table.resize", json!({"table": "T", "range": "A1:C3"})).unwrap();
assert!(!sheet(&s).cell_pictures.contains_key(&gridcraft_core::CellRef::new(0, 2)));
assert_eq!(headers(&s, 0), names(&s, 0));
}