-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add option to set header row #453
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4b5bf6d
feat: add option to keep first empty rows for xlsx
PrettyWood f22d177
Merge branch 'master' into feat/keep-first-empty-rows
PrettyWood b27b0aa
refactor: switch to `header_row`
PrettyWood dba5b60
refactor: implement properly header_row for xlsx
PrettyWood 8b1a77f
feat: add header_row to ods file
PrettyWood 25635b9
chore: simplify
PrettyWood ff35b2c
refactor: impl with associated type
PrettyWood dd33174
refactor: switch to matching on reference
PrettyWood 65d4c2d
refactor: use mutable ref
PrettyWood 72e74b0
add example in README
PrettyWood 23f4c26
feat: add helper method
PrettyWood d56c7f9
clean
PrettyWood d4df81a
fix: doc
PrettyWood 0d63b39
chore: options are not exported anymore
PrettyWood b6f91e0
refactor: switch to enum for header row
PrettyWood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,10 @@ use calamine::Data::{Bool, DateTime, DateTimeIso, DurationIso, Empty, Error, Flo | |
use calamine::{ | ||
open_workbook, open_workbook_auto, DataRef, DataType, Dimensions, ExcelDateTime, | ||
ExcelDateTimeType, Ods, Range, Reader, ReaderRef, Sheet, SheetType, SheetVisible, Xls, Xlsb, | ||
Xlsx, | ||
Xlsx, XlsxOptions, | ||
}; | ||
use calamine::{CellErrorType::*, Data}; | ||
use rstest::rstest; | ||
use std::collections::BTreeSet; | ||
use std::fs::File; | ||
use std::io::{BufReader, Cursor}; | ||
|
@@ -1786,3 +1787,39 @@ fn test_ref_xlsb() { | |
] | ||
); | ||
} | ||
|
||
#[rstest] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't know about rstest. It seems neat. |
||
#[case("keep-empty-rows.xlsx", false, (2, 0), (9, 3), &[Empty, Empty, String("Note 1".to_string()), Empty], 32)] | ||
#[case("keep-empty-rows.xlsx", true, (0, 0), (9, 3), &[Empty, Empty, Empty, Empty], 40)] | ||
#[case("temperature.xlsx", false, (0, 0), (2, 1), &[String("label".to_string()), String("value".to_string())], 6)] | ||
#[case("temperature.xlsx", true, (0, 0), (2, 1), &[String("label".to_string()), String("value".to_string())], 6)] | ||
#[case("temperature-in-middle.xlsx", false, (3, 1), (5, 2), &[String("label".to_string()), String("value".to_string())], 6)] | ||
#[case("temperature-in-middle.xlsx", true, (0, 1), (5, 2), &[Empty, Empty], 12)] | ||
fn keep_first_empty_rows_xlsx( | ||
#[case] fixture_path: &str, | ||
#[case] keep_first_empty_rows: bool, | ||
#[case] expected_start: (u32, u32), | ||
#[case] expected_end: (u32, u32), | ||
#[case] expected_first_row: &[Data], | ||
#[case] expected_total_cells: usize, | ||
) { | ||
let excel: Xlsx<_> = wb(fixture_path); | ||
assert_eq!( | ||
excel.sheets_metadata(), | ||
&[Sheet { | ||
name: "Sheet1".to_string(), | ||
typ: SheetType::WorkSheet, | ||
visible: SheetVisible::Visible | ||
},] | ||
); | ||
|
||
// By default empty cells are skipped so the first row is skipped | ||
let range = excel | ||
.with_options(XlsxOptions::new().with_keep_first_empty_rows(keep_first_empty_rows)) | ||
.worksheet_range("Sheet1") | ||
.unwrap(); | ||
assert_eq!(range.start(), Some(expected_start)); | ||
assert_eq!(range.end(), Some(expected_end)); | ||
assert_eq!(range.rows().next().unwrap(), expected_first_row,); | ||
assert_eq!(range.cells().count(), expected_total_cells); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a more generic approach would be to have the option to set header row?
By default
None
means that it'll skip all empty rows (like today) and then we could havewith_header_row(self, row: u32)
?I understand that >90% of use cases are the first row but it still feels better and not necessarily more complicated to give the option to the users?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect will do that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done