Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
DESKTOP-U434MT0\hiro committed Oct 18, 2024
1 parent 3d6e3ee commit 09c8027
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/structs/border.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ impl Border {
)
}

// When opened in software such as Excel, it is visually blank.
pub(crate) fn is_visually_empty(&self) -> bool {
self.style.get_value() == &BorderStyleValues::None
}

pub(crate) fn set_attributes<R: std::io::BufRead>(
&mut self,
reader: &mut Reader<R>,
Expand Down
11 changes: 11 additions & 0 deletions src/structs/borders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,17 @@ impl Borders {
)
}

// When opened in software such as Excel, it is visually blank.
pub(crate) fn is_visually_empty(&self) -> bool {
self.left_border.is_visually_empty()
|| self.right_border.is_visually_empty()
|| self.top_border.is_visually_empty()
|| self.bottom_border.is_visually_empty()
|| self.diagonal_border.is_visually_empty()
|| self.vertical_border.is_visually_empty()
|| self.horizontal_border.is_visually_empty()
}

pub(crate) fn set_attributes<R: std::io::BufRead>(
&mut self,
reader: &mut Reader<R>,
Expand Down
7 changes: 7 additions & 0 deletions src/structs/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ impl Cell {
result
}

// When opened in software such as Excel, it is visually blank.
pub(crate) fn is_visually_empty(&self) -> bool {
self.cell_value.is_visually_empty()
&& self.style.is_visually_empty()
&& self.hyperlink.is_none()
}

pub(crate) fn set_obj(&mut self, cell: Self) -> &mut Self {
self.cell_value = cell.cell_value;
self.style = cell.style;
Expand Down
5 changes: 5 additions & 0 deletions src/structs/cell_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ impl CellValue {
pub(crate) fn is_formula_empty(&self) -> bool {
!self.is_formula()
}

// When opened in software such as Excel, it is visually blank.
pub(crate) fn is_visually_empty(&self) -> bool {
self.get_value() == "" && self.is_formula_empty()
}
}
impl AdjustmentCoordinateWith2Sheet for CellValue {
fn adjustment_insert_coordinate_with_2sheet(
Expand Down
5 changes: 5 additions & 0 deletions src/structs/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ impl Color {
)
}

// When opened in software such as Excel, it is visually blank.
pub(crate) fn is_visually_empty(&self) -> bool {
!self.has_value()
}

pub(crate) fn set_attributes<R: std::io::BufRead>(
&mut self,
reader: &mut Reader<R>,
Expand Down
9 changes: 9 additions & 0 deletions src/structs/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ impl Fill {
)
}

// When opened in software such as Excel, it is visually blank.
pub(crate) fn is_visually_empty(&self) -> bool {
!(self
.pattern_fill
.as_ref()
.is_some_and(|x| !x.is_visually_empty())
|| self.gradient_fill.as_ref().is_some())
}

pub(crate) fn set_attributes<R: std::io::BufRead>(
&mut self,
reader: &mut Reader<R>,
Expand Down
13 changes: 13 additions & 0 deletions src/structs/pattern_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ impl PatternFill {
)
}

// When opened in software such as Excel, it is visually blank.
pub(crate) fn is_visually_empty(&self) -> bool {
!(self.pattern_type.get_value() != &PatternValues::None
|| self
.foreground_color
.as_ref()
.is_some_and(|x| !x.is_visually_empty())
|| self
.background_color
.as_ref()
.is_some_and(|x| x.is_visually_empty()))
}

pub(crate) fn set_attributes<R: std::io::BufRead>(
&mut self,
reader: &mut Reader<R>,
Expand Down
9 changes: 9 additions & 0 deletions src/structs/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ impl Style {
|| self.protection.is_some())
}

// When opened in software such as Excel, it is visually blank.
pub(crate) fn is_visually_empty(&self) -> bool {
!(self.fill.as_ref().is_some_and(|x| !x.is_visually_empty())
|| self
.borders
.as_ref()
.is_some_and(|x| !x.is_visually_empty()))
}

pub fn get_default_value() -> Self {
let mut def = Self::default();
def.set_font(Font::get_default_value());
Expand Down
31 changes: 31 additions & 0 deletions src/structs/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,37 @@ impl Worksheet {

self
}

/// Remove invisible garbage data.
/// Doing so may reduce file size.
/// Processing may take some time.
pub fn cleanup(&mut self) {
let (_, max_row) = self.get_highest_column_and_row();
for row in (1..(max_row + 1)).rev() {
if self.row_dimensions.get_row_dimension(&row).is_some() {
let mut indexes: Vec<(u32, u32)> = Vec::new();
{
let cells: Vec<&Cell> = self.cell_collection.get_collection_by_row(&row);
for cell in cells {
if !cell.is_visually_empty() {
return;
}
indexes.push((
cell.get_coordinate().get_row_num().clone(),
cell.get_coordinate().get_col_num().clone(),
));
}
}

self.row_dimensions
.get_row_dimensions_to_hashmap_mut()
.remove(&row);
for (i_row, i_col) in indexes {
self.cell_collection.remove(&i_col, &i_row);
}
}
}
}
}
impl AdjustmentCoordinate for Worksheet {
fn adjustment_insert_coordinate(
Expand Down
11 changes: 11 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1841,3 +1841,14 @@ fn issue_232() {
let path = std::path::Path::new("./tests/result_files/issue_232.xlsx");
let _ = umya_spreadsheet::writer::xlsx::write(&book, path);
}

#[test]
fn issue_233() {
let path = std::path::Path::new("./tests/test_files/issue_233.xlsx");
let mut book = umya_spreadsheet::reader::xlsx::read(path).unwrap();

// book.get_sheet_mut(&0).unwrap().cleanup();

let path = std::path::Path::new("./tests/result_files/issue_233.xlsx");
let _ = umya_spreadsheet::writer::xlsx::write(&book, path);
}
Binary file added tests/test_files/issue_233.xlsx
Binary file not shown.

0 comments on commit 09c8027

Please sign in to comment.