mirror of
https://github.com/storytold/gridcraft.git
synced 2026-12-01 10:14:21 -06:00
Filter: bound a table-header filter by the table, refuse a narrower range that would drop criteria, fail the test helper on errors
This commit is contained in:
@@ -552,10 +552,9 @@ fn install_filter(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
}
|
||||
// A selected header row filters the data below it (#201).
|
||||
if p.get("range").is_none() && r.height() == 1 {
|
||||
let region = if sh.table_at(r.start).is_some() {
|
||||
current_region(sh, r.start)
|
||||
} else {
|
||||
crate::selection::current_region_outside_tables(sh, r.start)
|
||||
let region = match sh.table_at(r.start) {
|
||||
Some(t) => t.data_range().map(|dr| RangeRef::new(t.range.start, dr.end)).unwrap_or(t.range),
|
||||
None => crate::selection::current_region_outside_tables(sh, r.start),
|
||||
};
|
||||
if region.end.row > r.end.row {
|
||||
r = RangeRef::new(r.start, CellRef::new(region.end.row, r.end.col));
|
||||
@@ -569,6 +568,13 @@ fn install_filter(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
if host.is_none() && sh.tables.iter().any(|t| t.range.intersects(&r)) {
|
||||
return Err(bad("data.filter", "a filter range can't overlap a table; filter inside the table instead"));
|
||||
}
|
||||
// A narrower range would leave out the table's criteria on the columns it doesn't cover, and saving would drop them.
|
||||
if let Some(t) = host.and_then(|i| sh.tables.get(i)) {
|
||||
let shift = r.start.col.saturating_sub(t.range.start.col);
|
||||
if t.filter.iter().any(|(o, _)| o.checked_sub(shift).is_none_or(|c| c >= r.width())) {
|
||||
return Err(bad("data.filter", "the table is filtered on a column this range leaves out; filter the table's full width"));
|
||||
}
|
||||
}
|
||||
edit(s, |cx| {
|
||||
let sh = cx.sheet_mut(sheet)?;
|
||||
let mut criteria = vec![];
|
||||
|
||||
@@ -34,7 +34,7 @@ fn session(sample: &str, steps: &[(&str, Json)]) -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"sample": sample})).unwrap();
|
||||
for (id, p) in steps {
|
||||
let _ = s.execute(id, p.clone());
|
||||
s.execute(id, p.clone()).unwrap();
|
||||
}
|
||||
s
|
||||
}
|
||||
@@ -109,6 +109,18 @@ fn rows_hidden_by_hand_without_a_filter_still_count_for_subtotal_9() {
|
||||
assert_eq!(e1_e2(&back), (Value::Number(111111.0), Value::Number(110101.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_narrower_range_inside_a_filtered_table_is_refused_not_dropped() {
|
||||
let mut s = table_book();
|
||||
s.execute("data.filterBy", json!({"column":"A","values":["a"]})).unwrap();
|
||||
let back = reopened(wb(&s));
|
||||
let mut s = Session::new();
|
||||
s.add_document(gridcraft_engine::DocState::new(back, None, "t".into(), gridcraft_engine::Trust::FromFile));
|
||||
s.execute("selection.set", json!({"cell":"D1"})).unwrap();
|
||||
assert!(s.execute("data.filter", json!({"range":"B1:B7"})).is_err());
|
||||
assert_eq!(wb(&s).active().unwrap().tables[0].filter.len(), 1, "the table's criteria are kept");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filtering_a_reopened_table_again_keeps_both_criteria() {
|
||||
let mut s = table_book();
|
||||
|
||||
Reference in New Issue
Block a user