Skip to content

Commit

Permalink
fix(ods): support consecutive repeated empty cells
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Aug 10, 2024
1 parent a90e877 commit c52bf05
Show file tree
Hide file tree
Showing 6 changed files with 33 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"
sha256 = "1.3"

Expand Down
6 changes: 4 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,12 +445,13 @@ 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);
}
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 @@ -1786,3 +1787,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 c52bf05

Please sign in to comment.