Skip to content

Commit

Permalink
Merge pull request #456 from PrettyWood/fix/455
Browse files Browse the repository at this point in the history
fix(ods): support consecutive repeated empty cells
  • Loading branch information
tafia authored Sep 16, 2024
2 parents b699e3c + daa664f commit 003620b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Cargo.lock
fuzz.xlsx
.idea
nyc.rs
.DS_Store
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ chrono = { version = "0.4", features = [
[dev-dependencies]
glob = "0.3"
env_logger = "0.11"
rstest = { version = "0.21.0", default-features = false }
serde_derive = "1.0"
sha2 = "0.10.8"

Expand Down
7 changes: 5 additions & 2 deletions src/ods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ fn get_range<T: Default + Clone + PartialEq>(
let mut new_cells = Vec::with_capacity(cells_len);
let empty_cells = vec![T::default(); col_max + 1];
let mut empty_row_repeats = 0;
let mut consecutive_empty_rows = 0;
for (w, row_repeats) in cols
.windows(2)
.skip(row_min)
Expand All @@ -444,16 +445,18 @@ fn get_range<T: Default + Clone + PartialEq>(
let row_repeats = *row_repeats;

if is_empty_row(row) {
empty_row_repeats = row_repeats;
empty_row_repeats += row_repeats;
consecutive_empty_rows += 1;
continue;
}

if empty_row_repeats > 0 {
row_max = row_max + empty_row_repeats - 1;
row_max = row_max + empty_row_repeats - consecutive_empty_rows;
for _ in 0..empty_row_repeats {
new_cells.extend_from_slice(&empty_cells);
}
empty_row_repeats = 0;
consecutive_empty_rows = 0;
};

if row_repeats > 1 {
Expand Down
Binary file added tests/multi-empty.ods
Binary file not shown.
Binary file added tests/single-empty.ods
Binary file not shown.
27 changes: 27 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use calamine::{
Xlsx,
};
use calamine::{CellErrorType::*, Data};
use rstest::rstest;
use std::collections::BTreeSet;
use std::fs::File;
use std::io::{BufReader, Cursor};
Expand Down Expand Up @@ -1795,3 +1796,29 @@ fn test_ref_xlsb() {
]
);
}

#[rstest]
#[case("single-empty.ods")]
#[case("multi-empty.ods")]
fn issue_repeated_empty(#[case] fixture_path: &str) {
let mut ods: Ods<_> = wb(fixture_path);
let range = ods.worksheet_range_at(0).unwrap().unwrap();
range_eq!(
range,
[
[String("StringCol".to_string())],
[String("bbb".to_string())],
[String("ccc".to_string())],
[String("ddd".to_string())],
[String("eee".to_string())],
[Empty],
[Empty],
[Empty],
[Empty],
[Empty],
[Empty],
[Empty],
[String("zzz".to_string())],
]
);
}

0 comments on commit 003620b

Please sign in to comment.