use crate::coordinate::{CellRef, Col, Range, Row};
use crate::error::{Error, Result};
use crate::model::{CellValue, Spreadsheet, Worksheet};
use super::CopyOrigin;
pub const MAX_OUTLINE_LEVEL: u8 = 7;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OutlineGroup {
pub level: u8,
pub first: u32,
pub last: u32,
}
impl OutlineGroup {
#[must_use]
pub const fn contains(&self, line: u32) -> bool {
self.first <= line && line <= self.last
}
#[must_use]
pub fn summary(&self, after: bool) -> Option<u32> {
if after {
Some(self.last + 1).filter(|&n| n <= MAX_LINE)
} else {
self.first.checked_sub(1)
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Lines {
Rows,
Columns,
}
const MAX_LINE: u32 = crate::coordinate::MAX_ROW - 1;
impl Lines {
fn level(self, ws: &Worksheet, line: u32) -> u8 {
match self {
Self::Rows => Row::new(line).map_or(0, |r| ws.row_outline_level(r)),
Self::Columns => Col::new(line).map_or(0, |c| ws.column_outline_level(c)),
}
}
fn levelled(self, ws: &Worksheet) -> Vec<(u32, u8)> {
match self {
Self::Rows => ws
.rows
.iter()
.filter(|(_, p)| p.outline_level > 0)
.map(|(r, p)| (r.index(), p.outline_level))
.collect(),
Self::Columns => {
let mut out: Vec<(u32, u8)> = ws
.columns
.iter()
.filter(|run| run.outline_level > 0)
.flat_map(|run| {
(run.first.index()..=run.last.index()).map(|c| (c, run.outline_level))
})
.collect();
out.sort_unstable();
out
}
}
}
fn summary_after(self, ws: &Worksheet) -> bool {
match self {
Self::Rows => ws.properties.summary_below,
Self::Columns => ws.properties.summary_right,
}
}
fn update(
self,
ws: &mut Worksheet,
line: u32,
change: impl FnOnce(&mut u8, &mut bool, &mut bool),
) {
match self {
Self::Rows => {
let Some(row) = Row::new(line) else { return };
let p = ws.rows.entry(row).or_default();
change(&mut p.outline_level, &mut p.hidden, &mut p.collapsed);
if !p.is_meaningful() {
ws.rows.remove(&row);
}
}
Self::Columns => {
let Some(col) = Col::new(line) else { return };
let run = ws.column_entry(col);
change(&mut run.outline_level, &mut run.hidden, &mut run.collapsed);
}
}
}
fn check(self, first: u32, last: u32) -> Result<()> {
let fits = match self {
Self::Rows => Row::new(last).is_some(),
Self::Columns => Col::new(last).is_some(),
};
if first > last || !fits {
return Err(Error::InvalidRange(format!(
"lines {} to {} are not on the sheet",
first + 1,
last + 1
)));
}
Ok(())
}
}
fn groups_of(levelled: &[(u32, u8)]) -> Vec<OutlineGroup> {
let deepest = levelled.iter().map(|&(_, l)| l).max().unwrap_or(0);
let mut out = Vec::new();
for level in 1..=deepest {
let mut open: Option<(u32, u32)> = None;
for &(line, l) in levelled {
if l < level {
if let Some((first, last)) = open.take() {
out.push(OutlineGroup { level, first, last });
}
continue;
}
open = match open {
Some((first, last)) if last + 1 == line => Some((first, line)),
Some((first, last)) => {
out.push(OutlineGroup { level, first, last });
Some((line, line))
}
None => Some((line, line)),
};
}
if let Some((first, last)) = open {
out.push(OutlineGroup { level, first, last });
}
}
out
}
#[must_use]
pub fn row_groups(ws: &Worksheet) -> Vec<OutlineGroup> {
groups_of(&Lines::Rows.levelled(ws))
}
#[must_use]
pub fn column_groups(ws: &Worksheet) -> Vec<OutlineGroup> {
groups_of(&Lines::Columns.levelled(ws))
}
fn sheet_mut(book: &mut Spreadsheet, sheet: usize) -> Result<&mut Worksheet> {
book.sheet_mut(sheet)
.ok_or(Error::SheetIndexOutOfRange(sheet))
}
fn group(book: &mut Spreadsheet, sheet: usize, lines: Lines, first: u32, last: u32) -> Result<()> {
lines.check(first, last)?;
let ws = sheet_mut(book, sheet)?;
if let Some(line) = (first..=last).find(|&l| lines.level(ws, l) >= MAX_OUTLINE_LEVEL) {
return Err(Error::InvalidRange(format!(
"line {} is already {MAX_OUTLINE_LEVEL} levels deep, as deep as an outline goes",
line + 1
)));
}
for line in first..=last {
lines.update(ws, line, |level, _, _| *level += 1);
}
Ok(())
}
fn ungroup(
book: &mut Spreadsheet,
sheet: usize,
lines: Lines,
first: u32,
last: u32,
) -> Result<()> {
lines.check(first, last)?;
let ws = sheet_mut(book, sheet)?;
let after = lines.summary_after(ws);
let before = groups_of(&lines.levelled(ws));
let touched: Vec<u32> = lines
.levelled(ws)
.into_iter()
.filter(|&(l, _)| first <= l && l <= last)
.map(|(l, _)| l)
.collect();
for line in touched {
lines.update(ws, line, |level, hidden, _| {
*level -= 1;
if *level == 0 {
*hidden = false;
}
});
}
let after_groups = groups_of(&lines.levelled(ws));
for g in before {
let Some(summary) = g.summary(after) else {
continue;
};
if !after_groups
.iter()
.any(|h| h.summary(after) == Some(summary))
{
lines.update(ws, summary, |_, _, collapsed| *collapsed = false);
}
}
Ok(())
}
fn group_named(groups: &[OutlineGroup], after: bool, line: u32) -> Option<OutlineGroup> {
groups
.iter()
.filter(|g| g.summary(after) == Some(line))
.max_by_key(|g| g.level)
.or_else(|| {
groups
.iter()
.filter(|g| g.contains(line))
.max_by_key(|g| g.level)
})
.copied()
}
fn set_detail(
book: &mut Spreadsheet,
sheet: usize,
lines: Lines,
line: u32,
show: bool,
) -> Result<bool> {
let ws = sheet_mut(book, sheet)?;
let after = lines.summary_after(ws);
let groups = groups_of(&lines.levelled(ws));
let Some(g) = group_named(&groups, after, line) else {
return Ok(false);
};
let collapsed_inside: Vec<OutlineGroup> = groups
.iter()
.filter(|o| o.level > g.level && g.first <= o.first && o.last <= g.last)
.filter(|o| {
o.summary(after)
.filter(|&s| s != g.summary(after).unwrap_or(u32::MAX))
.is_some_and(|s| is_collapsed(ws, lines, s))
})
.copied()
.collect();
for l in g.first..=g.last {
let hide = !show || collapsed_inside.iter().any(|o| o.contains(l));
lines.update(ws, l, |_, hidden, _| *hidden = hide);
}
if let Some(summary) = g.summary(after) {
lines.update(ws, summary, |_, _, collapsed| *collapsed = !show);
}
Ok(true)
}
fn is_collapsed(ws: &Worksheet, lines: Lines, line: u32) -> bool {
match lines {
Lines::Rows => Row::new(line)
.and_then(|r| ws.rows.get(&r))
.is_some_and(|p| p.collapsed),
Lines::Columns => Col::new(line)
.and_then(|c| ws.column_run(c))
.is_some_and(|run| run.collapsed),
}
}
fn show_level(ws: &mut Worksheet, lines: Lines, level: u8) {
let after = lines.summary_after(ws);
for (line, l) in lines.levelled(ws) {
lines.update(ws, line, |_, hidden, _| *hidden = l >= level);
}
let mut groups = groups_of(&lines.levelled(ws));
groups.sort_by_key(|g| std::cmp::Reverse(g.level));
for g in groups {
if let Some(summary) = g.summary(after) {
lines.update(ws, summary, |_, _, collapsed| *collapsed = g.level >= level);
}
}
}
pub fn group_rows(book: &mut Spreadsheet, sheet: usize, first: Row, last: Row) -> Result<()> {
group(book, sheet, Lines::Rows, first.index(), last.index())
}
pub fn ungroup_rows(book: &mut Spreadsheet, sheet: usize, first: Row, last: Row) -> Result<()> {
ungroup(book, sheet, Lines::Rows, first.index(), last.index())
}
pub fn group_columns(book: &mut Spreadsheet, sheet: usize, first: Col, last: Col) -> Result<()> {
group(book, sheet, Lines::Columns, first.index(), last.index())
}
pub fn ungroup_columns(book: &mut Spreadsheet, sheet: usize, first: Col, last: Col) -> Result<()> {
ungroup(book, sheet, Lines::Columns, first.index(), last.index())
}
pub fn collapse_rows(book: &mut Spreadsheet, sheet: usize, row: Row) -> Result<bool> {
set_detail(book, sheet, Lines::Rows, row.index(), false)
}
pub fn expand_rows(book: &mut Spreadsheet, sheet: usize, row: Row) -> Result<bool> {
set_detail(book, sheet, Lines::Rows, row.index(), true)
}
pub fn collapse_columns(book: &mut Spreadsheet, sheet: usize, col: Col) -> Result<bool> {
set_detail(book, sheet, Lines::Columns, col.index(), false)
}
pub fn expand_columns(book: &mut Spreadsheet, sheet: usize, col: Col) -> Result<bool> {
set_detail(book, sheet, Lines::Columns, col.index(), true)
}
pub fn show_outline_level(
book: &mut Spreadsheet,
sheet: usize,
rows: Option<u8>,
columns: Option<u8>,
) -> Result<()> {
for n in [rows, columns].into_iter().flatten() {
if !(1..=MAX_OUTLINE_LEVEL + 1).contains(&n) {
return Err(Error::InvalidRange(format!(
"an outline level runs from 1 to {}, not {n}",
MAX_OUTLINE_LEVEL + 1
)));
}
}
let ws = sheet_mut(book, sheet)?;
if let Some(n) = rows {
show_level(ws, Lines::Rows, n);
}
if let Some(n) = columns {
show_level(ws, Lines::Columns, n);
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SubtotalFunction {
#[default]
Sum,
Count,
Average,
Max,
Min,
Product,
CountNumbers,
StdDev,
StdDevP,
Var,
VarP,
}
impl SubtotalFunction {
#[must_use]
pub const fn code(self) -> u8 {
match self {
Self::Average => 1,
Self::CountNumbers => 2,
Self::Count => 3,
Self::Max => 4,
Self::Min => 5,
Self::Product => 6,
Self::StdDev => 7,
Self::StdDevP => 8,
Self::Sum => 9,
Self::Var => 10,
Self::VarP => 11,
}
}
#[must_use]
pub const fn label(self) -> &'static str {
match self {
Self::Sum => "Total",
Self::Count | Self::CountNumbers => "Count",
Self::Average => "Average",
Self::Max => "Max",
Self::Min => "Min",
Self::Product => "Product",
Self::StdDev => "StdDev",
Self::StdDevP => "StdDevp",
Self::Var => "Var",
Self::VarP => "Varp",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Subtotals {
pub group_by: Col,
pub function: SubtotalFunction,
pub columns: Vec<Col>,
}
pub fn subtotal(book: &mut Spreadsheet, sheet: usize, area: Range, how: &Subtotals) -> Result<u32> {
let ws = book
.sheet(sheet)
.ok_or(Error::SheetIndexOutOfRange(sheet))?;
let inside = |c: Col| area.start.col <= c && c <= area.end.col;
if let Some(c) = std::iter::once(&how.group_by)
.chain(&how.columns)
.find(|c| !inside(**c))
{
return Err(Error::InvalidRange(format!(
"column {} is outside {area}",
c.to_letters()
)));
}
if how.columns.is_empty() {
return Err(Error::InvalidRange("no column to add a subtotal to".into()));
}
let first = area.start.row.index() + 1;
let last = ws
.dimension()
.map_or(first, |used| area.end.row.index().min(used.end.row.index()));
if first > last {
return Err(Error::InvalidRange(format!(
"{area} has no data under its header"
)));
}
if (first..=last + 1).any(|r| Lines::Rows.level(ws, r) + 2 > MAX_OUTLINE_LEVEL) {
return Err(Error::InvalidRange(format!(
"the rows of {area} are grouped too deeply to take two more levels"
)));
}
if last + (last - first + 2) > MAX_LINE {
return Err(Error::WouldPushOffSheet);
}
let key = |r: u32| {
Row::new(r).map_or_else(String::new, |row| {
book.formatted(sheet, CellRef::new(how.group_by, row))
})
};
let mut runs: Vec<(u32, u32, String)> = Vec::new();
for r in first..=last {
let shown = key(r);
match runs.last_mut() {
Some((_, end, name)) if name.to_lowercase() == shown.to_lowercase() => *end = r,
_ => runs.push((r, r, shown)),
}
}
let label = how.function.label();
let code = how.function.code();
let bold = |book: &mut Spreadsheet, at: CellRef| {
let ws = book.sheet(sheet);
let id = ws
.and_then(|ws| ws.get(at))
.map(|c| c.style)
.unwrap_or_default();
let mut style = book.styles.get(id).cloned().unwrap_or_default();
style.font.bold = true;
let id = book.styles.intern(style);
if let Some(ws) = book.sheet_mut(sheet) {
ws.entry(at).style = id;
}
};
let write_total =
|book: &mut Spreadsheet, at_row: u32, from: u32, to: u32, text: String| -> Result<()> {
let row = Row::new(at_row).ok_or(Error::WouldPushOffSheet)?;
let ws = sheet_mut(book, sheet)?;
for &col in &how.columns {
let span = format!("{0}{1}:{0}{2}", col.to_letters(), from + 1, to + 1);
ws.set(
CellRef::new(col, row),
CellValue::formula(format!("SUBTOTAL({code},{span})")),
);
}
let at = CellRef::new(how.group_by, row);
if !how.columns.contains(&how.group_by) {
ws.set(at, text);
}
bold(book, at);
Ok(())
};
for (start, end, name) in runs.iter().rev() {
let below = Row::new(end + 1).ok_or(Error::WouldPushOffSheet)?;
super::insert_rows_with(book, sheet, below, 1, CopyOrigin::Blank)?;
write_total(book, end + 1, *start, *end, format!("{name} {label}"))?;
}
let groups = u32::try_from(runs.len()).unwrap_or(u32::MAX);
let last_total = last + groups;
let grand = Row::new(last_total + 1).ok_or(Error::WouldPushOffSheet)?;
super::insert_rows_with(book, sheet, grand, 1, CopyOrigin::Blank)?;
write_total(
book,
last_total + 1,
first,
last_total,
format!("Grand {label}"),
)?;
group(book, sheet, Lines::Rows, first, last_total)?;
for (i, (start, end, _)) in (0u32..).zip(&runs) {
group(book, sheet, Lines::Rows, start + i, end + i)?;
}
Ok(groups)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn groups_nest_and_split() {
let levelled = [(2, 1), (3, 1), (4, 2), (5, 2), (6, 1), (7, 1), (9, 1)];
assert_eq!(
groups_of(&levelled),
[
OutlineGroup {
level: 1,
first: 2,
last: 7
},
OutlineGroup {
level: 1,
first: 9,
last: 9
},
OutlineGroup {
level: 2,
first: 4,
last: 5
},
]
);
}
#[test]
fn summary_sides() {
let g = OutlineGroup {
level: 1,
first: 0,
last: 3,
};
assert_eq!(g.summary(true), Some(4));
assert_eq!(g.summary(false), None);
}
}