#[derive(Clone, Debug, PartialEq)]
pub enum TableStyle {
None,
Light(u32),
Medium(u32),
Dark(u32),
Custom(String),
}
impl TableStyle {
pub fn style_name(&self) -> String {
match self {
TableStyle::None => "TableStyleMedium2".to_string(),
TableStyle::Light(n) => format!("TableStyleLight{}", n.clamp(&1, &21)),
TableStyle::Medium(n) => format!("TableStyleMedium{}", n.clamp(&1, &28)),
TableStyle::Dark(n) => format!("TableStyleDark{}", n.clamp(&1, &11)),
TableStyle::Custom(name) => name.clone(),
}
}
pub fn blue() -> Self {
TableStyle::Medium(2)
}
pub fn green() -> Self {
TableStyle::Medium(7)
}
pub fn red() -> Self {
TableStyle::Medium(3)
}
pub fn gray() -> Self {
TableStyle::Medium(1)
}
}
impl Default for TableStyle {
fn default() -> Self {
TableStyle::Medium(2)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum TotalsRowFunction {
None,
Average,
Count,
CountNums,
Max,
Min,
StdDev,
Sum,
Var,
Custom(String),
}
impl TotalsRowFunction {
pub fn xml_name(&self) -> Option<&str> {
match self {
TotalsRowFunction::None => None,
TotalsRowFunction::Average => Some("average"),
TotalsRowFunction::Count => Some("count"),
TotalsRowFunction::CountNums => Some("countNums"),
TotalsRowFunction::Max => Some("max"),
TotalsRowFunction::Min => Some("min"),
TotalsRowFunction::StdDev => Some("stdDev"),
TotalsRowFunction::Sum => Some("sum"),
TotalsRowFunction::Var => Some("var"),
TotalsRowFunction::Custom(_) => Some("custom"),
}
}
}
#[derive(Clone, Debug)]
pub struct TableColumn {
pub id: u32,
pub name: String,
pub totals_row_function: TotalsRowFunction,
pub totals_row_label: Option<String>,
pub calculated_column_formula: Option<String>,
}
impl TableColumn {
pub fn new(id: u32, name: &str) -> Self {
TableColumn {
id,
name: name.to_string(),
totals_row_function: TotalsRowFunction::None,
totals_row_label: None,
calculated_column_formula: None,
}
}
pub fn with_totals_function(mut self, function: TotalsRowFunction) -> Self {
self.totals_row_function = function;
self
}
pub fn with_totals_label<S: Into<String>>(mut self, label: S) -> Self {
self.totals_row_label = Some(label.into());
self
}
pub fn with_formula<S: Into<String>>(mut self, formula: S) -> Self {
self.calculated_column_formula = Some(formula.into());
self
}
}
#[derive(Clone, Debug)]
pub struct Table {
pub id: u32,
pub name: String,
pub display_name: String,
pub range: String,
pub columns: Vec<TableColumn>,
pub style: TableStyle,
pub header_row: bool,
pub totals_row: bool,
pub show_first_column: bool,
pub show_last_column: bool,
pub show_row_stripes: bool,
pub show_column_stripes: bool,
pub auto_filter: bool,
pub comment: Option<String>,
}
impl Table {
pub fn new(id: u32, name: &str, range: &str) -> Self {
Table {
id,
name: name.to_string(),
display_name: name.to_string(),
range: range.to_string(),
columns: Vec::new(),
style: TableStyle::default(),
header_row: true,
totals_row: false,
show_first_column: false,
show_last_column: false,
show_row_stripes: true,
show_column_stripes: false,
auto_filter: true,
comment: None,
}
}
pub fn with_headers(id: u32, name: &str, range: &str, headers: &[&str]) -> Self {
let mut table = Self::new(id, name, range);
for (i, header) in headers.iter().enumerate() {
table.columns.push(TableColumn::new((i + 1) as u32, header));
}
table
}
pub fn with_style(mut self, style: TableStyle) -> Self {
self.style = style;
self
}
pub fn with_totals_row(mut self) -> Self {
self.totals_row = true;
self
}
pub fn without_header_row(mut self) -> Self {
self.header_row = false;
self
}
pub fn with_first_column(mut self) -> Self {
self.show_first_column = true;
self
}
pub fn with_last_column(mut self) -> Self {
self.show_last_column = true;
self
}
pub fn with_column_stripes(mut self) -> Self {
self.show_row_stripes = false;
self.show_column_stripes = true;
self
}
pub fn without_auto_filter(mut self) -> Self {
self.auto_filter = false;
self
}
pub fn add_column(&mut self, column: TableColumn) {
self.columns.push(column);
}
pub fn set_column_totals(&mut self, column_name: &str, function: TotalsRowFunction) {
for col in &mut self.columns {
if col.name == column_name {
col.totals_row_function = function;
break;
}
}
}
pub fn structured_ref(&self, column: Option<&str>, specifier: Option<&str>) -> String {
let mut ref_str = format!("{}[", self.name);
if let Some(spec) = specifier {
ref_str.push_str(&format!("[{}]", spec));
}
if let Some(col) = column {
if specifier.is_some() {
ref_str.push(',');
}
ref_str.push_str(&format!("[{}]", col));
}
ref_str.push(']');
ref_str
}
}
pub mod specifiers {
pub const DATA: &str = "#Data";
pub const HEADERS: &str = "#Headers";
pub const TOTALS: &str = "#Totals";
pub const ALL: &str = "#All";
pub const THIS_ROW: &str = "#This Row";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_table_creation() {
let table = Table::new(1, "SalesData", "A1:D100");
assert_eq!(table.name, "SalesData");
assert_eq!(table.range, "A1:D100");
assert!(table.header_row);
assert!(!table.totals_row);
}
#[test]
fn test_table_with_headers() {
let table = Table::with_headers(1, "Products", "A1:C10", &["Name", "Price", "Quantity"]);
assert_eq!(table.columns.len(), 3);
assert_eq!(table.columns[0].name, "Name");
assert_eq!(table.columns[1].name, "Price");
}
#[test]
fn test_table_style() {
let table = Table::new(1, "Test", "A1:B10").with_style(TableStyle::blue());
assert_eq!(table.style.style_name(), "TableStyleMedium2");
}
#[test]
fn test_table_totals() {
let mut table = Table::with_headers(1, "Data", "A1:B10", &["Item", "Value"]);
table = table.with_totals_row();
table.set_column_totals("Value", TotalsRowFunction::Sum);
assert!(table.totals_row);
assert_eq!(table.columns[1].totals_row_function, TotalsRowFunction::Sum);
}
#[test]
fn test_structured_reference() {
let table = Table::new(1, "Sales", "A1:C10");
assert_eq!(
table.structured_ref(Some("Amount"), None),
"Sales[[Amount]]"
);
assert_eq!(
table.structured_ref(Some("Amount"), Some("#Data")),
"Sales[[#Data],[Amount]]"
);
assert_eq!(
table.structured_ref(None, Some("#Totals")),
"Sales[[#Totals]]"
);
}
#[test]
fn test_table_column_formula() {
let col = TableColumn::new(1, "Total").with_formula("[@Price]*[@Quantity]");
assert_eq!(
col.calculated_column_formula,
Some("[@Price]*[@Quantity]".to_string())
);
}
}
#[cfg(test)]
mod coverage_tests {
use super::*;
#[test]
fn table_style_presets() {
assert!(!TableStyle::blue().style_name().is_empty());
assert!(!TableStyle::green().style_name().is_empty());
assert!(!TableStyle::red().style_name().is_empty());
assert!(!TableStyle::gray().style_name().is_empty());
}
#[test]
fn totals_functions_xml() {
assert_eq!(TotalsRowFunction::None.xml_name(), None);
assert_eq!(TotalsRowFunction::Sum.xml_name(), Some("sum"));
assert_eq!(TotalsRowFunction::Average.xml_name(), Some("average"));
assert_eq!(TotalsRowFunction::CountNums.xml_name(), Some("countNums"));
}
#[test]
fn table_column_builders() {
let c = TableColumn::new(1, "Amount")
.with_totals_function(TotalsRowFunction::Sum)
.with_totals_label("Total")
.with_formula("[Amount]*2");
assert_eq!(c.id, 1);
assert_eq!(c.name, "Amount");
}
#[test]
fn table_builders_and_structured_ref() {
let t = Table::new(1, "Sales", "A1:C10")
.with_style(TableStyle::blue())
.with_totals_row()
.with_first_column()
.with_last_column()
.with_column_stripes()
.without_auto_filter();
assert_eq!(t.name, "Sales");
assert!(t.totals_row);
assert!(!t.auto_filter, "without_auto_filter clears it");
let h = Table::with_headers(2, "T2", "A1:B5", &["Name", "Value"]).without_header_row();
assert_eq!(h.columns.len(), 2);
let mut t3 = Table::new(3, "T3", "A1:B3");
t3.add_column(TableColumn::new(1, "X"));
t3.set_column_totals("X", TotalsRowFunction::Count);
let sref = t.structured_ref(Some("Sales"), None);
assert!(sref.contains("Sales"));
}
}