From 804d83269c6901b21bbf4a2cc3617ac4c9558736 Mon Sep 17 00:00:00 2001 From: Jilani Syed Date: Tue, 6 Feb 2024 20:13:07 +0530 Subject: [PATCH] some improvements/optimizations (#174) * some improvements/optimisations * fix for linting --- src/helper/const_str.rs | 7 +- src/reader/xlsx/doc_props_app.rs | 8 +- src/structs/address.rs | 10 +- src/structs/cell.rs | 14 +- src/structs/cells.rs | 21 +- src/structs/numbering_formats.rs | 2 +- src/structs/rows.rs | 11 +- src/structs/spreadsheet.rs | 111 +++----- src/structs/worksheet.rs | 432 ++++++++++++++--------------- src/writer/xlsx/vba_project_bin.rs | 5 +- src/writer/xlsx/vml_drawing.rs | 7 +- 11 files changed, 301 insertions(+), 327 deletions(-) diff --git a/src/helper/const_str.rs b/src/helper/const_str.rs index 3e64d2ce..dfbaadb4 100644 --- a/src/helper/const_str.rs +++ b/src/helper/const_str.rs @@ -21,6 +21,7 @@ pub(crate) const DRAWINGML_MAIN_NS: &str = "http://schemas.openxmlformats.org/dr pub(crate) const DRAWINGS_NS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"; pub(crate) const ENCRYPTION_NS: &str = "http://schemas.microsoft.com/office/2006/encryption"; +pub(crate) const EXCEL_NS: &str = "urn:schemas-microsoft-com:office:excel"; pub(crate) const HYPERLINK_NS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"; pub(crate) const IMAGE_NS: &str = @@ -28,6 +29,7 @@ pub(crate) const IMAGE_NS: &str = pub(crate) const MC_NS: &str = "http://schemas.openxmlformats.org/markup-compatibility/2006"; pub(crate) const OFCDOC_NS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"; +pub(crate) const OFFICE_NS: &str = "urn:schemas-microsoft-com:office:office"; pub(crate) const OLE_OBJECT_NS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject"; pub(crate) const PASSWORD_NS: &str = @@ -56,10 +58,11 @@ pub(crate) const TABLE_NS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table"; pub(crate) const THEME_NS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"; -pub(crate) const VML_DRAWING_NS: &str = - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing"; pub(crate) const VBA_PROJECT_NS: &str = "http://schemas.microsoft.com/office/2006/relationships/vbaProject"; +pub(crate) const VML_NS: &str = "urn:schemas-microsoft-com:vml"; +pub(crate) const VML_DRAWING_NS: &str = + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing"; pub(crate) const VTYPES_NS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"; pub(crate) const WORKSHEET_NS: &str = diff --git a/src/reader/xlsx/doc_props_app.rs b/src/reader/xlsx/doc_props_app.rs index 5d5402c0..fcd6d4b3 100644 --- a/src/reader/xlsx/doc_props_app.rs +++ b/src/reader/xlsx/doc_props_app.rs @@ -29,14 +29,10 @@ pub(crate) fn read( Ok(Event::End(ref e)) => { match e.name().into_inner() { b"Manager" => { - spreadsheet - .get_properties_mut() - .set_manager(string_value.clone()); + spreadsheet.get_properties_mut().set_manager(&string_value); } b"Company" => { - spreadsheet - .get_properties_mut() - .set_company(string_value.clone()); + spreadsheet.get_properties_mut().set_company(&string_value); } _ => (), } diff --git a/src/structs/address.rs b/src/structs/address.rs index 115a5d03..0b7aae0f 100644 --- a/src/structs/address.rs +++ b/src/structs/address.rs @@ -53,21 +53,21 @@ impl Address { if self.sheet_name.is_empty() { return range; } - let mut with_space_char = String::from(""); + let mut with_space_char = ""; let mut sheet_name = self.sheet_name.clone(); if sheet_name.contains(char::is_whitespace) { - with_space_char = String::from("'"); + with_space_char = "'"; } if is_ptn2 { if sheet_name.contains("!") { - with_space_char = String::from("'"); + with_space_char = "'"; } if sheet_name.contains("'") { - with_space_char = String::from("'"); + with_space_char = "'"; sheet_name = sheet_name.replace("'", "''"); } if sheet_name.contains(r#"""#) { - with_space_char = String::from("'"); + with_space_char = "'"; } } format!( diff --git a/src/structs/cell.rs b/src/structs/cell.rs index d3db893a..b4c453af 100644 --- a/src/structs/cell.rs +++ b/src/structs/cell.rs @@ -228,9 +228,9 @@ impl Cell { } pub(crate) fn set_obj(&mut self, cell: Self) -> &mut Self { - self.cell_value = cell.get_cell_value().clone(); - self.style = cell.get_style().clone(); - self.hyperlink = cell.get_hyperlink().cloned(); + self.cell_value = cell.cell_value; + self.style = cell.style; + self.hyperlink = cell.hyperlink; self } @@ -310,11 +310,11 @@ impl Cell { } Ok(Event::End(ref e)) => match e.name().into_inner() { b"f" => { - self.set_formula(string_value.clone()); + self.set_formula(&string_value); } b"v" => { if type_value == "str" { - self.set_value_str(string_value.clone()); + self.set_value_str(&string_value); } if type_value == "s" { let index = string_value.parse::().unwrap(); @@ -329,12 +329,12 @@ impl Cell { } else if type_value == "e" { self.set_error(); } else if type_value.is_empty() || type_value == "n" { - self.set_value_crate(string_value.clone()); + self.set_value_crate(&string_value); }; } b"is" => { if type_value == "inlineStr" { - self.set_value_crate(string_value.clone()); + self.set_value_crate(&string_value); } } b"c" => return, diff --git a/src/structs/cells.rs b/src/structs/cells.rs index 88face41..2c189df7 100644 --- a/src/structs/cells.rs +++ b/src/structs/cells.rs @@ -276,13 +276,18 @@ impl Cells { } pub(crate) fn rebuild_map(&mut self) { - let mut rebuild: HashMap<(u32, u32), Cell> = HashMap::new(); - for ((_, _), cell) in self.get_collection_to_hashmap_mut() { - let col_num = cell.get_coordinate().get_col_num(); - let row_num = cell.get_coordinate().get_row_num(); - let k = (row_num.to_owned(), col_num.to_owned()); - rebuild.insert(k, cell.clone()); - } - self.map = rebuild; + self.map = self + .get_collection_to_hashmap_mut() + .iter_mut() + .map(|(_, cell)| { + ( + ( + *cell.get_coordinate().get_row_num(), + *cell.get_coordinate().get_col_num(), + ), + std::mem::take(cell), + ) + }) + .collect() } } diff --git a/src/structs/numbering_formats.rs b/src/structs/numbering_formats.rs index f5c6a283..449c17d7 100644 --- a/src/structs/numbering_formats.rs +++ b/src/structs/numbering_formats.rs @@ -95,7 +95,7 @@ impl NumberingFormats { } pub(crate) fn write_to(&self, writer: &mut Writer>>) { - let formats_to_write: HashMap<_,_> = self + let formats_to_write: HashMap<_, _> = self .numbering_format .iter() .filter(|(k, v)| !*v.get_is_build_in()) diff --git a/src/structs/rows.rs b/src/structs/rows.rs index 350849af..6381e799 100644 --- a/src/structs/rows.rs +++ b/src/structs/rows.rs @@ -81,11 +81,10 @@ impl Rows { } pub(crate) fn rebuild_map(&mut self) { - let mut rebuild: HashMap = HashMap::new(); - for (_, row) in self.get_row_dimensions_to_hashmap_mut() { - let row_num = row.get_row_num(); - rebuild.insert(row_num.to_owned(), row.clone()); - } - self.rows = rebuild; + self.rows = self + .get_row_dimensions_to_hashmap_mut() + .iter_mut() + .map(|(_, row)| (*row.get_row_num(), std::mem::take(row))) + .collect(); } } diff --git a/src/structs/spreadsheet.rs b/src/structs/spreadsheet.rs index 61f53f35..8c766b69 100644 --- a/src/structs/spreadsheet.rs +++ b/src/structs/spreadsheet.rs @@ -363,12 +363,9 @@ impl Spreadsheet { } pub(crate) fn find_sheet_index_by_name(&self, sheet_name: &str) -> Option { - for (result, sheet) in self.work_sheet_collection.iter().enumerate() { - if sheet.get_name() == sheet_name { - return Some(result); - } - } - None + self.work_sheet_collection + .iter() + .position(|sheet| sheet.get_name() == sheet_name) } /// Get Work Sheet. @@ -377,13 +374,12 @@ impl Spreadsheet { /// # Return value /// * `Option<&Worksheet>`. pub fn get_sheet(&self, index: &usize) -> Option<&Worksheet> { - match self.work_sheet_collection.get(*index) { - Some(v) => { + self.work_sheet_collection + .get(*index) + .map(|v| { assert!(v.is_deserialized(),"This Worksheet is Not Deserialized. Please exec to read_sheet(&mut self, index: usize);"); - Some(v) - } - None => None, - } + v + }) } /// Get Work Sheet. @@ -392,23 +388,21 @@ impl Spreadsheet { /// # Return value /// * `Option<&Worksheet>. pub fn get_sheet_by_name(&self, sheet_name: &str) -> Option<&Worksheet> { - match self.find_sheet_index_by_name(sheet_name) { - Some(index) => { - return self.get_sheet(&index); - } - None => None, - } + self.find_sheet_index_by_name(sheet_name) + .and_then(|index| self.get_sheet(&index)) } pub fn get_lazy_read_sheet_cells(&self, index: &usize) -> Result { let shared_string_table = self.get_shared_string_table(); - match self.work_sheet_collection.get(*index) { - Some(v) => Ok(v.get_cell_collection_stream( - &shared_string_table.read().unwrap(), - self.get_stylesheet(), - )), - None => Err("Not found."), - } + self.work_sheet_collection + .get(*index) + .map(|v| { + v.get_cell_collection_stream( + &shared_string_table.read().unwrap(), + self.get_stylesheet(), + ) + }) + .ok_or("Not found.") } /// Get Work Sheet in mutable. @@ -420,13 +414,10 @@ impl Spreadsheet { let theme = self.get_theme().clone(); let shared_string_table = self.get_shared_string_table(); let stylesheet = self.get_stylesheet().clone(); - match self.work_sheet_collection.get_mut(*index) { - Some(v) => { - raw_to_deserialize_by_worksheet(v, &theme, shared_string_table, &stylesheet); - Some(v) - } - None => None, - } + self.work_sheet_collection.get_mut(*index).map(|v| { + raw_to_deserialize_by_worksheet(v, &theme, shared_string_table, &stylesheet); + v + }) } /// Get Work Sheet in mutable. @@ -435,12 +426,8 @@ impl Spreadsheet { /// # Return value /// * `Option<&mut Worksheet>`. pub fn get_sheet_by_name_mut(&mut self, sheet_name: &str) -> Option<&mut Worksheet> { - match self.find_sheet_index_by_name(sheet_name) { - Some(index) => { - return self.get_sheet_mut(&index); - } - None => None, - } + self.find_sheet_index_by_name(sheet_name) + .and_then(move |index| self.get_sheet_mut(&index)) } pub fn set_active_sheet(&mut self, index: u32) -> &mut Self { @@ -471,9 +458,7 @@ impl Spreadsheet { /// * `Result<&mut Worksheet, &'static str>` - OK:added work sheet. Err:Error. pub fn add_sheet(&mut self, value: Worksheet) -> Result<&mut Worksheet, &'static str> { let title = value.get_name(); - if let Err(e) = Spreadsheet::check_sheet_name(self, title) { - return Err(e); - } + Spreadsheet::check_sheet_name(self, title)?; self.work_sheet_collection.push(value); Ok(self.work_sheet_collection.last_mut().unwrap()) } @@ -517,9 +502,7 @@ impl Spreadsheet { sheet_title: S, ) -> Result<&mut Worksheet, &'static str> { let v = sheet_title.into(); - if let Err(e) = Spreadsheet::check_sheet_name(self, &v) { - return Err(e); - } + Spreadsheet::check_sheet_name(self, &v)?; let sheet_id = (self.work_sheet_collection.len() + 1).to_string(); Ok(Spreadsheet::add_new_sheet_crate(self, sheet_id, v)) } @@ -558,27 +541,26 @@ impl Spreadsheet { sheet_name: S, ) -> Result<(), &'static str> { let sheet_name_str = sheet_name.into(); - if let Err(e) = Spreadsheet::check_sheet_name(self, sheet_name_str.as_ref()) { - return Err(e); - } - match self.work_sheet_collection.get_mut(index) { - Some(sheet) => { + Spreadsheet::check_sheet_name(self, sheet_name_str.as_ref())?; + self.work_sheet_collection + .get_mut(index) + .map(|sheet| { sheet.set_name(sheet_name_str); - Ok(()) - } - None => Err("sheet not found."), - } + }) + .ok_or("sheet not found.") } /// (This method is crate only.) /// Check for duplicate sheet name. pub(crate) fn check_sheet_name(&self, value: &str) -> Result<(), &'static str> { - for work_sheet in &self.work_sheet_collection { - if value == work_sheet.get_name() { - return Err("name duplicate."); - } + match self + .work_sheet_collection + .iter() + .any(|work_sheet| value == work_sheet.get_name()) + { + true => Err("name duplicate."), + false => Ok(()), } - Ok(()) } /// (This method is crate only.) @@ -654,15 +636,10 @@ impl Spreadsheet { } pub(crate) fn update_pivot_caches(&mut self, key: String, value: String) -> &mut Self { - let mut result: Vec<(String, String, String)> = Vec::new(); - for (val1, val2, val3) in &self.pivot_caches { - let mut result_value = &value; - if val1 != &key { - result_value = val3; - } - result.push((val1.to_string(), val2.to_string(), result_value.to_string())); - } - self.pivot_caches = result; + self.pivot_caches.iter_mut().for_each(|(val1, _, val3)| { + let result_value = if val1 == &key { &value } else { &val3 }; + *val3 = result_value.to_string(); + }); self } diff --git a/src/structs/worksheet.rs b/src/structs/worksheet.rs index b3e36ba7..cb8158e8 100644 --- a/src/structs/worksheet.rs +++ b/src/structs/worksheet.rs @@ -504,15 +504,12 @@ impl Worksheet { pub(crate) fn get_hyperlink_collection_to_hashmap(&self) -> HashMap { let mut result: HashMap = HashMap::new(); for cell in self.cell_collection.get_collection() { - match cell.get_hyperlink() { - Some(hyperlink) => { - let coordition = coordinate_from_index( - cell.get_coordinate().get_col_num(), - cell.get_coordinate().get_row_num(), - ); - result.insert(coordition, hyperlink); - } - None => {} + if let Some(hyperlink) = cell.get_hyperlink() { + let coordition = coordinate_from_index( + cell.get_coordinate().get_col_num(), + cell.get_coordinate().get_row_num(), + ); + result.insert(coordition, hyperlink); } } result @@ -936,76 +933,75 @@ impl Worksheet { self.get_row_dimensions_crate_mut() .adjustment_insert_coordinate(root_row_num, offset_row_num); } - if offset_col_num != &0 || offset_row_num != &0 { - // defined_names - let title = self.title.clone(); - for defined_name in &mut self.defined_names { - defined_name.adjustment_insert_coordinate( - &title, - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); - } + if (offset_col_num == &0 && offset_row_num == &0) { + return; + } - // cell - self.get_cell_collection_crate_mut() - .adjustment_insert_coordinate( - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); + // defined_names + let title = &self.title; + for defined_name in &mut self.defined_names { + defined_name.adjustment_insert_coordinate( + title, + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); + } - // comments - for comment in &mut self.comments { - comment.adjustment_insert_coordinate( - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); - } + // cell + self.get_cell_collection_crate_mut() + .adjustment_insert_coordinate( + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); - // conditional styles - for conditional_styles in &mut self.conditional_formatting_collection { - for range in conditional_styles - .get_sequence_of_references_mut() - .get_range_collection_mut() - { - range.adjustment_insert_coordinate( - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); - } - } + // comments + for comment in &mut self.comments { + comment.adjustment_insert_coordinate( + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); + } - // merge cells - for merge_cell in self.get_merge_cells_mut() { - merge_cell.adjustment_insert_coordinate( + // conditional styles + for conditional_styles in &mut self.conditional_formatting_collection { + for range in conditional_styles + .get_sequence_of_references_mut() + .get_range_collection_mut() + { + range.adjustment_insert_coordinate( root_col_num, offset_col_num, root_row_num, offset_row_num, ); } + } - // auto filter - match self.get_auto_filter_mut() { - Some(v) => { - v.get_range_mut().adjustment_insert_coordinate( - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); - } - None => {} - }; + // merge cells + for merge_cell in self.get_merge_cells_mut() { + merge_cell.adjustment_insert_coordinate( + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); } + + // auto filter + if let Some(v) = self.get_auto_filter_mut() { + v.get_range_mut().adjustment_insert_coordinate( + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); + }; } pub(crate) fn adjustment_insert_coordinate_from_other_sheet( @@ -1016,28 +1012,30 @@ impl Worksheet { root_row_num: &u32, offset_row_num: &u32, ) { - if offset_col_num != &0 || offset_row_num != &0 { - // cell formula coordinate - let title = self.title.clone(); - self.get_cell_collection_crate_mut() - .adjustment_insert_formula_coordinate( - &title, - sheet_name, - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); + if (offset_col_num == &0 && offset_row_num == &0) { + return; + } - // chart - self.worksheet_drawing.adjustment_insert_coordinate( + // cell formula coordinate + let title = self.title.clone(); + self.get_cell_collection_crate_mut() + .adjustment_insert_formula_coordinate( + &title, sheet_name, root_col_num, offset_col_num, root_row_num, offset_row_num, ); - } + + // chart + self.worksheet_drawing.adjustment_insert_coordinate( + sheet_name, + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); } /// (This method is crate only.) @@ -1059,125 +1057,121 @@ impl Worksheet { self.get_row_dimensions_crate_mut() .adjustment_remove_coordinate(root_row_num, offset_row_num); } - if offset_col_num != &0 || offset_row_num != &0 { - // defined_names - let sheet_name = self.title.clone(); - let mut idx = 0 as usize; - while idx < self.defined_names.len() { - if self.defined_names[idx].is_remove( - &sheet_name, - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ) { - self.defined_names.remove(idx); - } - idx += 1; - } - for defined_name in &mut self.defined_names { - defined_name.adjustment_remove_coordinate( - &sheet_name, - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); - } - - // cell - self.get_cell_collection_crate_mut() - .adjustment_remove_coordinate( - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); + if (offset_col_num == &0 && offset_row_num == &0) { + return; + } - // comments - self.comments.retain(|x| { - !(x.get_coordinate().is_remove( - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - )) - }); - for comment in &mut self.comments { - comment.adjustment_remove_coordinate( - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); + // defined_names + let sheet_name = self.title.clone(); + let mut idx = 0 as usize; + while idx < self.defined_names.len() { + if self.defined_names[idx].is_remove( + &sheet_name, + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ) { + self.defined_names.remove(idx); } + idx += 1; + } + for defined_name in &mut self.defined_names { + defined_name.adjustment_remove_coordinate( + &sheet_name, + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); + } - // conditional styles - for conditional_styles in &mut self.conditional_formatting_collection { - conditional_styles - .get_sequence_of_references_mut() - .get_range_collection_mut() - .retain(|x| { - !(x.is_remove(root_col_num, offset_col_num, root_row_num, offset_row_num)) - }); - } - self.conditional_formatting_collection.retain(|x| { - !x.get_sequence_of_references() - .get_range_collection() - .is_empty() - }); - for conditional_styles in &mut self.conditional_formatting_collection { - for range in conditional_styles - .get_sequence_of_references_mut() - .get_range_collection_mut() - { - range.adjustment_remove_coordinate( - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); - } - } + // cell + self.get_cell_collection_crate_mut() + .adjustment_remove_coordinate( + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); - // merge cells - self.get_merge_cells_mut().retain(|x| { - !(x.is_remove(root_col_num, offset_col_num, root_row_num, offset_row_num)) - }); - for merge_cell in self.get_merge_cells_mut() { - merge_cell.adjustment_remove_coordinate( + // comments + self.comments.retain(|x| { + !(x.get_coordinate().is_remove( + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + )) + }); + for comment in &mut self.comments { + comment.adjustment_remove_coordinate( + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); + } + + // conditional styles + for conditional_styles in &mut self.conditional_formatting_collection { + conditional_styles + .get_sequence_of_references_mut() + .get_range_collection_mut() + .retain(|x| { + !(x.is_remove(root_col_num, offset_col_num, root_row_num, offset_row_num)) + }); + } + self.conditional_formatting_collection.retain(|x| { + !x.get_sequence_of_references() + .get_range_collection() + .is_empty() + }); + for conditional_styles in &mut self.conditional_formatting_collection { + for range in conditional_styles + .get_sequence_of_references_mut() + .get_range_collection_mut() + { + range.adjustment_remove_coordinate( root_col_num, offset_col_num, root_row_num, offset_row_num, ); } + } - // auto filter - let is_remove = match self.get_auto_filter() { - Some(v) => v.get_range().is_remove( - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ), - None => false, - }; - if is_remove { - self.remove_auto_filter(); + // merge cells + self.get_merge_cells_mut() + .retain(|x| !(x.is_remove(root_col_num, offset_col_num, root_row_num, offset_row_num))); + for merge_cell in self.get_merge_cells_mut() { + merge_cell.adjustment_remove_coordinate( + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); + } + + // auto filter + let is_remove = match self.get_auto_filter() { + Some(v) => { + v.get_range() + .is_remove(root_col_num, offset_col_num, root_row_num, offset_row_num) } - match self.get_auto_filter_mut() { - Some(v) => { - v.get_range_mut().adjustment_remove_coordinate( - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); - } - None => {} - }; + None => false, + }; + if is_remove { + self.remove_auto_filter(); } + if let Some(v) = self.get_auto_filter_mut() { + v.get_range_mut().adjustment_remove_coordinate( + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); + }; } /// (This method is crate only.) @@ -1190,28 +1184,30 @@ impl Worksheet { root_row_num: &u32, offset_row_num: &u32, ) { - if offset_col_num != &0 || offset_row_num != &0 { - // cell formula coordinate - let title = self.title.clone(); - self.get_cell_collection_crate_mut() - .adjustment_remove_formula_coordinate( - &title, - sheet_name, - root_col_num, - offset_col_num, - root_row_num, - offset_row_num, - ); + if (offset_col_num == &0 && offset_row_num == &0) { + return; + } - // chart - self.worksheet_drawing.adjustment_remove_coordinate( + // cell formula coordinate + let title = self.title.clone(); + self.get_cell_collection_crate_mut() + .adjustment_remove_formula_coordinate( + &title, sheet_name, root_col_num, offset_col_num, root_row_num, offset_row_num, ); - } + + // chart + self.worksheet_drawing.adjustment_remove_coordinate( + sheet_name, + root_col_num, + offset_col_num, + root_row_num, + offset_row_num, + ); } /// Get Code Name. @@ -1719,17 +1715,14 @@ impl Worksheet { pub(crate) fn get_pivot_cache_definition_collection(&self) -> Vec<&str> { let mut result: Vec<&str> = Vec::new(); - match &self.raw_data_of_worksheet { - Some(raw_data) => { - for relationships in raw_data.get_relationships_list() { - for row in relationships.get_relationship_list() { - if row.get_type() == PIVOT_CACHE_DEF_NS { - result.push(row.get_raw_file().get_file_target()); - } + if let Some(raw_data) = &self.raw_data_of_worksheet { + for relationships in raw_data.get_relationships_list() { + for row in relationships.get_relationship_list() { + if row.get_type() == PIVOT_CACHE_DEF_NS { + result.push(row.get_raw_file().get_file_target()); } } } - None => {} } result } @@ -1823,14 +1816,15 @@ impl Worksheet { .collect(); // Delete cell information as iterating through - let coordinate_list = get_coordinate_list(&range_upper); - for (col_num, row_num) in &coordinate_list { - self.cell_collection.remove(col_num, row_num); - self.cell_collection.remove( - &((*col_num as i32 + column) as u32), - &((*row_num as i32 + row) as u32), - ); - } + get_coordinate_list(&range_upper) + .iter() + .for_each(|(col_num, row_num)| { + self.cell_collection.remove(col_num, row_num); + self.cell_collection.remove( + &((*col_num as i32 + column) as u32), + &((*row_num as i32 + row) as u32), + ); + }); // repaste by setting cell values for cell in &mut copy_cells { diff --git a/src/writer/xlsx/vba_project_bin.rs b/src/writer/xlsx/vba_project_bin.rs index 48389831..327a67c4 100644 --- a/src/writer/xlsx/vba_project_bin.rs +++ b/src/writer/xlsx/vba_project_bin.rs @@ -9,9 +9,8 @@ pub(crate) fn write( spreadsheet: &Spreadsheet, writer_mng: &mut WriterManager, ) -> Result<(), XlsxError> { - match spreadsheet.get_has_macros() { - true => {} - false => return Ok(()), + if !spreadsheet.get_has_macros() { + return Ok(()); } let writer = spreadsheet.get_macros_code().unwrap(); writer_mng.add_bin(PKG_VBA_PROJECT, writer) diff --git a/src/writer/xlsx/vml_drawing.rs b/src/writer/xlsx/vml_drawing.rs index e9ba92b2..20065e8e 100644 --- a/src/writer/xlsx/vml_drawing.rs +++ b/src/writer/xlsx/vml_drawing.rs @@ -1,5 +1,6 @@ use super::driver::*; use super::XlsxError; +use helper::const_str::*; use quick_xml::Writer; use std::io; use structs::Worksheet; @@ -19,9 +20,9 @@ pub(crate) fn write( &mut writer, "xml", vec![ - ("xmlns:v", "urn:schemas-microsoft-com:vml"), - ("xmlns:o", "urn:schemas-microsoft-com:office:office"), - ("xmlns:x", "urn:schemas-microsoft-com:office:excel"), + ("xmlns:v", VML_NS), + ("xmlns:o", OFFICE_NS), + ("xmlns:x", EXCEL_NS), ], false, );