mirror of
https://github.com/storytold/gridcraft.git
synced 2026-12-02 02:33:55 -06:00
* Functions: GETPIVOTDATA (#284) The engine records each PivotTable report value cell (value field, row/column items, value) on refresh and when a workbook is opened; GETPIVOTDATA is an evaluator special form that looks the value up by value field and field/item pairs, #REF! when the report doesn't show it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * GETPIVOTDATA: shared lazy index, report values, hash lookup (review of #290) - PivotTable::data_index is an Arc-shared PivotLookup (serde-skipped), so the per-command workbook clone no longer copies the index; refresh sets it, clear resets it. - No index work on open: the first GETPIVOTDATA on a pivot without one builds it from the source via the engine-registered PIVOT_INDEXER; a failure is #REF!, not an empty index. - The result is the value the sheet shows at the cell's report position, so formula and report cell agree (test with a source edited after the last refresh). - Lookup by a pre-lowercased hash key; numbers/booleans fall back to a scan on a miss. - GETPIVOTDATA is no longer volatile: it depends on its pivot's whole report area. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * GETPIVOTDATA: keep the index of the stored report; collision-free keys; align estimates The lookup index describes the report as last refreshed and is saved with the pivot. A pivot loaded without one (XLSX) only builds it when the recomputed layout matches the stored labels. Lookup keys are length-prefixed and distinguish Boolean from text items. Function-parity estimates match. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * pivot: import PivotDataCell, PivotItemRef and PivotLookup explicitly after upstream dropped the glob import Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * pivot: add an ignored measurement of edits on a sheet with a large report Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * pivot: keep a failed lazy GETPIVOTDATA build retryable, never save an empty index A failed lazy index build stored unwrap_or_default() into the shared, first-set-wins PivotLookup, so a valid state coming later (undo, a source fix, a reopen) could never rebuild it, and the empty list was saved as a restored index. Cache only validated builds, treat an empty list as no index in serialization, reject reports with more value cells than the index cap instead of truncating, register the pivot indexer in gridcraft-cli check, and add two regression tests. * GETPIVOTDATA: move cached offsets with edits inside the report; exact numeric items first Inserting or deleting rows or columns inside a report remapped the cells and last_range but kept the cached value-cell offsets, so a lookup could read another item's cell. shift_sheet_features now maps each indexed cell through the edit into a new index (the shared one stays with the undo snapshots); a value cell deleted with its line drops out (#REF!). A numeric item was accepted on a formatted-label hash hit, so 0.6 and 1 both shown as "1" returned the first. A number now prefers the cell whose item has exactly that value, before any label match. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fix pivot chart test for the new Output.data field Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
5.0 KiB
Rust
135 lines
5.0 KiB
Rust
use super::output::Output;
|
||
use super::{CellRef, Chart, PivotLookup, Series, Workbook, quote_sheet};
|
||
|
||
// ---------------------------------------------------------------- PivotCharts
|
||
|
||
/// The link a PivotChart stores to its PivotTable: `Sheet2!PivotTable1`.
|
||
pub fn chart_link(sheet: &str, pivot: &str) -> String {
|
||
format!("{}!{pivot}", quote_sheet(sheet))
|
||
}
|
||
|
||
/// (sheet, PivotTable name) of a PivotChart link; a leading `[Book.xlsx]` is ignored.
|
||
pub fn parse_chart_link(link: &str) -> Option<(String, String)> {
|
||
let t = link.trim();
|
||
let t = match t.strip_prefix('[') {
|
||
Some(rest) => rest.split_once(']').map(|(_, r)| r).unwrap_or(rest),
|
||
None => t,
|
||
};
|
||
let (sheet, name) = t.rsplit_once('!')?;
|
||
let sheet = sheet.trim();
|
||
let sheet = gridcraft_core::unquote_sheet(sheet);
|
||
let name = name.trim();
|
||
if sheet.is_empty() || name.is_empty() {
|
||
return None;
|
||
}
|
||
Some((sheet, name.to_string()))
|
||
}
|
||
|
||
/// Whether a PivotChart link points at `pivot` on `sheet`.
|
||
pub fn links_to(link: &str, sheet: &str, pivot: &str) -> bool {
|
||
parse_chart_link(link).is_some_and(|(s, n)| s.eq_ignore_ascii_case(sheet) && n.eq_ignore_ascii_case(pivot))
|
||
}
|
||
|
||
/// An absolute reference to `col` over grid `rows` (contiguous runs joined into a union).
|
||
fn rows_ref(sheet: &str, origin: CellRef, col: u32, rows: &[u32]) -> Option<String> {
|
||
let q = quote_sheet(sheet);
|
||
let c = origin.col.checked_add(col)?;
|
||
let letters = gridcraft_core::col_to_letters(c);
|
||
let mut parts: Vec<String> = Vec::new();
|
||
let mut i = 0;
|
||
while let Some(&start) = rows.get(i) {
|
||
let mut end = start;
|
||
while rows.get(i + 1).is_some_and(|n| Some(*n) == end.checked_add(1)) {
|
||
end += 1;
|
||
i += 1;
|
||
}
|
||
i += 1;
|
||
let (a, b) = (origin.row.checked_add(start)? + 1, origin.row.checked_add(end)? + 1);
|
||
parts.push(if a == b { format!("{q}!${letters}${a}") } else { format!("{q}!${letters}${a}:${letters}${b}") });
|
||
}
|
||
match parts.len() {
|
||
0 => None,
|
||
1 => parts.pop(),
|
||
_ => Some(format!("({})", parts.join(","))),
|
||
}
|
||
}
|
||
|
||
/// Most category rows a PivotChart plots.
|
||
const MAX_CHART_ROWS: usize = 10_000;
|
||
|
||
/// Most series a PivotChart gets: the chart renderer reads at most 255 series, and each one owns
|
||
/// reference text as long as its category rows, copied into every linked chart.
|
||
const MAX_CHART_SERIES: usize = 255;
|
||
|
||
/// A PivotChart's series for a report written at `origin`: categories are the innermost row
|
||
/// items, one series per item column (column items × value fields); totals are left out.
|
||
pub fn chart_series(sheet: &str, origin: CellRef, out: &Output) -> Vec<Series> {
|
||
let rows: Vec<u32> = out.chart_rows.iter().copied().take(MAX_CHART_ROWS).collect();
|
||
let categories = out.label_col.and_then(|c| rows_ref(sheet, origin, c, &rows));
|
||
out.chart_cols
|
||
.iter()
|
||
.take(MAX_CHART_SERIES)
|
||
.filter_map(|(c, name)| {
|
||
Some(Series {
|
||
name: Some(format!("\"{}\"", name.replace('"', "\"\""))),
|
||
categories: categories.clone(),
|
||
values: rows_ref(sheet, origin, *c, &rows)?,
|
||
bubble_sizes: None,
|
||
color: None,
|
||
secondary: false,
|
||
kind: None,
|
||
quartiles: Default::default(),
|
||
smooth: false,
|
||
trendlines: Vec::new(),
|
||
})
|
||
})
|
||
.collect()
|
||
}
|
||
|
||
/// Rebuilds the series of every PivotChart of a PivotTable after its report was written.
|
||
pub(super) fn rebind_charts(wb: &mut Workbook, sheet: &str, pivot: &str, origin: CellRef, out: &Output) {
|
||
let linked = |ch: &Chart| ch.pivot.as_deref().is_some_and(|l| links_to(l, sheet, pivot));
|
||
let hits: Vec<usize> = (0..wb.sheets.len()).filter(|i| wb.sheet(*i).is_some_and(|sh| sh.charts.iter().any(linked))).collect();
|
||
if hits.is_empty() {
|
||
return;
|
||
}
|
||
let series = chart_series(sheet, origin, out);
|
||
for i in hits {
|
||
let Some(sh) = wb.sheet_mut(i) else { continue };
|
||
for ch in sh.charts.iter_mut().filter(|c| linked(c)) {
|
||
ch.series = series.clone();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Clears a pivot's last output area.
|
||
pub fn clear_output(wb: &mut Workbook, sheet: usize, idx: usize) {
|
||
let Some(shm) = wb.sheet_mut(sheet) else { return };
|
||
let Some(r) = shm.pivots.get(idx).and_then(|p| p.last_range) else { return };
|
||
shm.take_cells(r);
|
||
if let Some(p) = shm.pivots.get_mut(idx) {
|
||
p.last_range = None;
|
||
p.data_index = PivotLookup::default();
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn a_pivot_chart_gets_at_most_the_series_the_renderer_reads() {
|
||
let out = Output {
|
||
grid: vec![],
|
||
row_kinds: vec![],
|
||
body_offset: 0,
|
||
width: 1000,
|
||
chart_rows: vec![1, 2, 3],
|
||
chart_cols: (1..1000).map(|c| (c, format!("S{c}"))).collect(),
|
||
label_col: Some(0),
|
||
data: vec![],
|
||
};
|
||
assert_eq!(chart_series("Sheet1", CellRef::new(0, 0), &out).len(), MAX_CHART_SERIES);
|
||
}
|
||
}
|