use anyhow::bail;
use quick_xml::events::{BytesStart, Event};
use std::io::Read;
use crate::{excel::XmlReader, helper::string_to_unsignedint};
use super::{
calculated_column_formula::XlsxCalculatedColumnFormula,
totals_row_formula::XlsxTotalsRowFormula, xml_column_properties::XlsxXmlColumnProperties,
};
pub type XlsxTableColumns = Vec<XlsxTableColumn>;
pub(crate) fn load_table_columns(
reader: &mut XmlReader<impl Read>,
) -> anyhow::Result<XlsxTableColumns> {
let mut buf = Vec::new();
let mut columns: XlsxTableColumns = vec![];
loop {
buf.clear();
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"tableColumn" => {
columns.push(XlsxTableColumn::load(reader, e)?);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"tableColumns" => break,
Ok(Event::Eof) => bail!("unexpected end of file at `tableColumns`."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
return Ok(columns);
}
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxTableColumn {
pub calculated_column_formula: Option<XlsxCalculatedColumnFormula>,
pub totals_row_formula: Option<XlsxTotalsRowFormula>,
pub xml_column_properties: Option<XlsxXmlColumnProperties>,
pub data_cell_style: Option<String>,
pub data_dxf_id: Option<u64>,
pub header_row_cell_style: Option<String>,
pub header_row_dxf_id: Option<u64>,
pub id: Option<u64>,
pub name: Option<String>,
pub query_table_field_id: Option<u64>,
pub totals_row_cell_style: Option<String>,
pub totals_row_dxf_id: Option<u64>,
pub totals_row_function: Option<String>,
pub totals_row_label: Option<String>,
pub unique_name: Option<String>,
}
impl XlsxTableColumn {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let attributes = e.attributes();
let mut column = Self {
calculated_column_formula: None,
totals_row_formula: None,
xml_column_properties: None,
data_cell_style: None,
data_dxf_id: None,
header_row_cell_style: None,
header_row_dxf_id: None,
id: None,
name: None,
query_table_field_id: None,
totals_row_cell_style: None,
totals_row_dxf_id: None,
totals_row_function: None,
totals_row_label: None,
unique_name: None,
};
for a in attributes {
match a {
Ok(a) => {
let string_value = String::from_utf8(a.value.to_vec())?;
match a.key.local_name().as_ref() {
b"dataCellStyle" => {
column.data_cell_style = Some(string_value);
}
b"dataDxfId" => {
column.data_dxf_id = string_to_unsignedint(&string_value);
}
b"headerRowCellStyle" => {
column.header_row_cell_style = Some(string_value);
}
b"headerRowDxfId" => {
column.header_row_dxf_id = string_to_unsignedint(&string_value);
}
b"id" => {
column.id = string_to_unsignedint(&string_value);
}
b"name" => {
column.name = Some(string_value);
}
b"queryTableFieldId" => {
column.query_table_field_id = string_to_unsignedint(&string_value);
}
b"totalsRowCellStyle" => {
column.totals_row_cell_style = Some(string_value);
}
b"totalsRowDxfId" => {
column.totals_row_dxf_id = string_to_unsignedint(&string_value);
}
b"totalsRowFunction" => {
column.totals_row_function = Some(string_value);
}
b"totalsRowLabel" => {
column.totals_row_label = Some(string_value);
}
b"uniqueName" => {
column.unique_name = Some(string_value);
}
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
let mut buf = Vec::new();
loop {
buf.clear();
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"extLst" => {
let _ = reader.read_to_end_into(e.to_end().to_owned().name(), &mut Vec::new());
}
Ok(Event::Start(ref e))
if e.local_name().as_ref() == b"calculatedColumnFormula" =>
{
column.calculated_column_formula =
Some(XlsxCalculatedColumnFormula::load(reader, e)?)
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"totalsRowFormula" => {
column.totals_row_formula = Some(XlsxTotalsRowFormula::load(reader, e)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"xmlColumnPr" => {
column.xml_column_properties = Some(XlsxXmlColumnProperties::load(reader, e)?);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"tableColumn" => break,
Ok(Event::Eof) => bail!("unexpected end of file at `tableColumn`."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(column)
}
}