refactor
Nicolas Sanchez 1 month ago
parent a11ca56849
commit 9f544e63d1

Binary file not shown.

@ -122,6 +122,12 @@ pub(crate) struct RawArguments {
/// avoid nth first rows of xlsx file
#[arg(short = 'k', long, default_value_t = 0)]
pub skip_rows: u32,
/// avoid empty lines
#[arg(short, long, default_value_t = false)]
pub avoid_empty_rows: bool,
/// consider first line (after row skipped) as header line
#[arg(short, long, default_value_t = false)]
pub header: bool,
/// change end of line character
#[arg(short, long, default_value_t = String::from("\n"))]
pub end_of_line: String,
@ -160,6 +166,10 @@ pub struct Arguments {
pub(crate) number_rows: NumberRows,
/// Avoid nth first rows of xlsx file
pub(crate) skip_rows: u32,
/// avoid empty lines
pub avoid_empty_rows: bool,
/// first line (after row skipped) is header line
pub header: bool,
///# csv output specific options
/// Separator for output
pub(crate) separator: char,
@ -194,6 +204,8 @@ impl Default for Arguments {
trim: Default::default(),
number_rows: Default::default(),
skip_rows: Default::default(),
avoid_empty_rows: false,
header: false,
end_of_line: String::from("\n"),
replace_end_of_line_by: Default::default(),
}
@ -231,6 +243,8 @@ impl From<RawArguments> for Arguments {
trim: raw.trim,
number_rows: raw.number_rows,
skip_rows: raw.skip_rows,
avoid_empty_rows: raw.avoid_empty_rows,
header: raw.header,
end_of_line: raw.end_of_line,
replace_end_of_line_by: raw.replace_end_of_line_by,
}

@ -3,6 +3,7 @@ pub use xlsxtocsv::{error::Error, xlsx::XlsxReader};
fn main() -> Result<(), Error> {
let lf = XlsxReader::new("noms.xlsx")
.with_active_worksheet()
.with_fill_merged_cells(xlsxtocsv::arguments::FillMergedCells::Both)
.to_lazyframe()?;
let df = lf.collect()?;

@ -13,9 +13,11 @@ pub struct XlsxReader {
pub(crate) book: Option<Spreadsheet>,
pub(crate) sheet_index: Option<usize>,
pub(crate) worksheet_dimensions: RefCell<Option<(u32, u32)>>,
pub(crate) current_row: u32,
}
impl XlsxReader {
/// Create an XlsxReader from a path or clap arguments
pub fn new(args: impl IntoArgs) -> Self {
let args = args.into_args();
@ -24,6 +26,7 @@ impl XlsxReader {
book: None,
sheet_index: None,
worksheet_dimensions: RefCell::new(None),
current_row: 0u32,
}
}
@ -91,7 +94,6 @@ impl XlsxReader {
let mut num_cols = 0;
let mut num_rows = 0;
let sheet = self.get_sheet().unwrap();
for cell in sheet.get_cell_collection() {
let value = get_value(cell); //.get_formatted_value();
@ -127,13 +129,23 @@ impl XlsxReader {
let value = cell.get_formatted_value();
let coord = cell.get_coordinate();
let col = *coord.get_col_num() - 1;
res[col as usize] = value;
if col < num_cols.try_into().unwrap() {
res[col as usize] = value;
}
}
res
}
}
impl Iterator for XlsxReader {
type Item = Result<String, Error>;
fn next(&mut self) -> Option<Self::Item> {
todo!()
}
}
pub fn list_worksheets(book: Spreadsheet) -> Result<Vec<(usize, String)>, Error> {
let sheets = book.get_sheet_collection();
let mut res = vec![];

@ -104,6 +104,16 @@ impl XlsxReader {
self
}
pub fn with_avoid_empty_rows(mut self) -> Self {
self.args.avoid_empty_rows = true;
self
}
pub fn with_header(mut self) -> Self {
self.args.header = true;
self
}
pub fn with_end_of_line(mut self, eol: String) -> Self {
self.args.end_of_line = eol;
self

Loading…
Cancel
Save