pub mod csv;
pub mod dynamic;
pub mod template;
pub mod writer;
pub use csv::write_csv;
pub use dynamic::{
DynamicExportRunner, DynamicSheet, align_row, named_xlsx_bytes, positional_xlsx_bytes,
write_named_xlsx, write_positional_xlsx,
};
pub use template::{DefaultExpr, ReferenceSheet, SampleExpr, TemplateSpec, build_template};
pub use writer::{ExcelWriter, SheetWriter};
use std::sync::Arc;
use crate::column::ColumnDef;
use crate::expr::{CellExpr, RowValues};
use crate::value::CellValue;
#[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(Clone)]
pub struct SheetOptions {
pub name: String,
pub columns: Vec<ColumnDef>,
pub write: WriteOptions,
pub computed: Vec<(ColumnDef, CellExpr)>,
pub transforms: Vec<(String, CellExpr)>,
}
impl std::fmt::Debug for SheetOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SheetOptions")
.field("name", &self.name)
.field("columns", &self.columns)
.field("write", &self.write)
.field(
"computed",
&self.computed.iter().map(|(d, _)| &d.header).collect::<Vec<_>>(),
)
.field(
"transforms",
&self.transforms.iter().map(|(h, _)| h).collect::<Vec<_>>(),
)
.finish()
}
}
impl SheetOptions {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
columns: Vec::new(),
write: WriteOptions::default(),
computed: Vec::new(),
transforms: Vec::new(),
}
}
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
}
pub fn computed<F>(mut self, def: ColumnDef, expr: F) -> Self
where
F: for<'a> Fn(&RowValues<'a>) -> CellValue + Send + Sync + 'static,
{
self.computed.push((def, Arc::new(expr)));
self
}
pub fn transform<F>(mut self, header: impl Into<String>, expr: F) -> Self
where
F: for<'a> Fn(&RowValues<'a>) -> CellValue + Send + Sync + 'static,
{
self.transforms.push((header.into(), Arc::new(expr)));
self
}
pub fn effective_columns(&self) -> Vec<ColumnDef> {
let mut cols = self.columns.clone();
cols.extend(self.computed.iter().map(|(def, _)| def.clone()));
cols
}
pub fn eval_row(&self, mut cells: Vec<CellValue>) -> Vec<CellValue> {
let base = &self.columns;
for (header, expr) in &self.transforms {
let Some(index) = base.iter().position(|c| &c.header == header) else {
continue;
};
let value = expr(&RowValues::new(&cells, base));
if index < cells.len() {
cells[index] = value;
} else {
while cells.len() < index {
cells.push(CellValue::Empty);
}
cells.push(value);
}
}
if !self.computed.is_empty() {
let all = self.effective_columns();
for (_, expr) in &self.computed {
let value = expr(&RowValues::new(&cells, &all));
cells.push(value);
}
}
cells
}
}
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)
}