pub mod csv;
pub mod template;
pub mod writer;
pub use csv::write_csv;
pub use template::{ReferenceSheet, TemplateSpec, build_template};
pub use writer::{ExcelWriter, SheetWriter};
use crate::column::ColumnDef;
#[derive(Debug, Clone)]
pub struct WriteOptions {
pub header_style: bool,
pub freeze_header: bool,
pub auto_filter: bool,
pub default_row_height: Option<f64>,
pub image_row_height: f64,
pub date_format: String,
pub datetime_format: String,
pub constant_memory: bool,
pub cell_image: bool,
}
impl Default for WriteOptions {
fn default() -> Self {
Self {
header_style: true,
freeze_header: true,
auto_filter: true,
default_row_height: None,
image_row_height: 60.0,
date_format: "yyyy-mm-dd".to_string(),
datetime_format: "yyyy-mm-dd hh:mm:ss".to_string(),
constant_memory: true,
cell_image: false,
}
}
}
impl WriteOptions {
pub fn new() -> Self {
Self::default()
}
pub fn in_memory(mut self) -> Self {
self.constant_memory = false;
self
}
pub fn cell_image(mut self) -> Self {
self.cell_image = true;
self.constant_memory = false;
self
}
pub fn without_header_style(mut self) -> Self {
self.header_style = false;
self
}
pub fn auto_filter(mut self, enable: bool) -> Self {
self.auto_filter = enable;
self
}
pub fn freeze_header(mut self, enable: bool) -> Self {
self.freeze_header = enable;
self
}
pub fn image_row_height(mut self, height: f64) -> Self {
self.image_row_height = height;
self
}
pub fn date_format(mut self, format: impl Into<String>) -> Self {
self.date_format = format.into();
self
}
pub fn datetime_format(mut self, format: impl Into<String>) -> Self {
self.datetime_format = format.into();
self
}
}
#[derive(Debug, Clone)]
pub struct SheetOptions {
pub name: String,
pub columns: Vec<ColumnDef>,
pub write: WriteOptions,
}
impl SheetOptions {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
columns: Vec::new(),
write: WriteOptions::default(),
}
}
pub fn columns<I>(mut self, columns: I) -> Self
where
I: IntoIterator<Item = ColumnDef>,
{
self.columns = columns.into_iter().collect();
self
}
pub fn write_options(mut self, write: WriteOptions) -> Self {
self.write = write;
self
}
}
impl Default for SheetOptions {
fn default() -> Self {
Self::new("Sheet1")
}
}
pub(crate) fn default_column_width(col: &ColumnDef) -> f64 {
let base: f64 = match col.kind {
crate::column::ColumnKind::DateTime => 20.0,
crate::column::ColumnKind::Date => 13.0,
crate::column::ColumnKind::Image => 16.0,
crate::column::ColumnKind::Text => 16.0,
_ => 12.0,
};
let header_width = col.header.chars().count() as f64 + 3.0;
base.max(header_width)
}