From 243d4e54f94cd6660399a6ca1399768fc836386c Mon Sep 17 00:00:00 2001 From: SgtPepper47 <121734141+SgtPepper47@users.noreply.github.com> Date: Wed, 8 May 2024 16:57:44 -0400 Subject: [PATCH 1/3] Update mod.rs with memory check Check the Vec size against the memory allocated to the machine. --- src/xlsx/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/xlsx/mod.rs b/src/xlsx/mod.rs index 75863609..ea645963 100644 --- a/src/xlsx/mod.rs +++ b/src/xlsx/mod.rs @@ -808,6 +808,7 @@ impl Xlsx { ) -> Result>, XlsxError> { let mut cell_reader = self.worksheet_cells_reader(name)?; let len = cell_reader.dimensions().len(); + let usize_len: usize = len as usize; let mut cells = Vec::new(); if len < 100_000 { cells.reserve(len as usize); @@ -823,6 +824,13 @@ impl Xlsx { Err(e) => return Err(e), } } + /// Chcek for machine memory error + if cells.capacity() < usize_len { + match cells.try_reserve(len as usize - cells.capacity()) { + Ok(_) => (), + Err(e) => return Err(XlsxError::CellError(e.to_string())), + } + } Ok(Range::from_sparse(cells)) } } From e154fc3dbca001a3ad41eb102be3b06d7e8fb98d Mon Sep 17 00:00:00 2001 From: SgtPepper47 <121734141+SgtPepper47@users.noreply.github.com> Date: Wed, 8 May 2024 17:05:01 -0400 Subject: [PATCH 2/3] Update excel_to_csv.rs with error catching and proper csv format Catch the error from the range variable without a panic fail. Change the writing to write a proper csv with a comma delimiter and quoting. --- examples/excel_to_csv.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/excel_to_csv.rs b/examples/excel_to_csv.rs index 8e6d6e85..4a4d8197 100644 --- a/examples/excel_to_csv.rs +++ b/examples/excel_to_csv.rs @@ -24,9 +24,13 @@ fn main() { let dest = sce.with_extension("csv"); let mut dest = BufWriter::new(File::create(dest).unwrap()); let mut xl = open_workbook_auto(&sce).unwrap(); - let range = xl.worksheet_range(&sheet).unwrap(); - - write_range(&mut dest, &range).unwrap(); + let range = xl.worksheet_range(&sheet); + if range.is_err() { + /// Capture error however you want + std::process::exit(1); + } else { + write_range(&mut dest, &range.unwrap()).unwrap(); + } } fn write_range(dest: &mut W, range: &Range) -> std::io::Result<()> { @@ -36,7 +40,7 @@ fn write_range(dest: &mut W, range: &Range) -> std::io::Result<( match *c { Data::Empty => Ok(()), Data::String(ref s) | Data::DateTimeIso(ref s) | Data::DurationIso(ref s) => { - write!(dest, "{}", s) + write!(dest, "\"{}\"", s.replace("\"", "\"\"") } Data::Float(ref f) => write!(dest, "{}", f), Data::DateTime(ref d) => write!(dest, "{}", d.as_f64()), @@ -45,7 +49,7 @@ fn write_range(dest: &mut W, range: &Range) -> std::io::Result<( Data::Bool(ref b) => write!(dest, "{}", b), }?; if i != n { - write!(dest, ";")?; + write!(dest, ",")?; } } write!(dest, "\r\n")?; From 2d0931c551d334081897e5f430fe11d9c8dc2232 Mon Sep 17 00:00:00 2001 From: SgtPepper47 <121734141+SgtPepper47@users.noreply.github.com> Date: Wed, 15 May 2024 10:51:20 -0400 Subject: [PATCH 3/3] fix: added unclosed parentheses in excel_to_csv.rs --- examples/excel_to_csv.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/excel_to_csv.rs b/examples/excel_to_csv.rs index 4a4d8197..d6632826 100644 --- a/examples/excel_to_csv.rs +++ b/examples/excel_to_csv.rs @@ -40,7 +40,7 @@ fn write_range(dest: &mut W, range: &Range) -> std::io::Result<( match *c { Data::Empty => Ok(()), Data::String(ref s) | Data::DateTimeIso(ref s) | Data::DurationIso(ref s) => { - write!(dest, "\"{}\"", s.replace("\"", "\"\"") + write!(dest, "\"{}\"", s.replace("\"", "\"\"")) } Data::Float(ref f) => write!(dest, "{}", f), Data::DateTime(ref d) => write!(dest, "{}", d.as_f64()),