use crate::coordinate::{CellRef, Range, Row};
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Table {
pub id: u32,
pub name: String,
pub display_name: String,
pub range: Range,
pub header_row_count: Option<u32>,
pub totals_row_count: Option<u32>,
pub auto_filter: Option<crate::model::AutoFilter>,
pub sort_state: Option<crate::model::SortState>,
pub columns: Vec<TableColumn>,
pub style: Option<TableStyle>,
pub extensions: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TableColumn {
pub id: u32,
pub name: String,
pub totals_row_function: Option<String>,
pub totals_row_label: Option<String>,
pub calculated_formula: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[expect(
clippy::struct_excessive_bools,
reason = "one field per attribute of <tableStyleInfo>; a bitfield would hide the format"
)]
pub struct TableStyle {
pub name: Option<String>,
pub show_first_column: bool,
pub show_last_column: bool,
pub show_row_stripes: bool,
pub show_column_stripes: bool,
}
impl Table {
#[must_use]
pub fn new(
id: u32,
name: &str,
range: Range,
columns: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
Self {
id,
name: name.to_owned(),
display_name: name.to_owned(),
range,
header_row_count: None,
totals_row_count: None,
auto_filter: None,
sort_state: None,
columns: columns
.into_iter()
.zip(1..)
.map(|(name, id)| TableColumn {
id,
name: name.into(),
..TableColumn::default()
})
.collect(),
style: None,
extensions: None,
}
}
#[must_use]
pub fn body(&self) -> Option<Range> {
let top = Row::new(
self.range
.start
.row
.index()
.checked_add(self.header_row_count.unwrap_or(1))?,
)?;
let bottom = Row::new(
self.range
.end
.row
.index()
.checked_sub(self.totals_row_count.unwrap_or(0))?,
)?;
if top > bottom {
return None;
}
Some(Range {
start: CellRef::new(self.range.start.col, top),
end: CellRef::new(self.range.end.col, bottom),
})
}
}