Created
May 15, 2019 15:54
-
-
Save TimoFreiberg/3cfc0163d5b1e5b3e4fab11f948b5299 to your computer and use it in GitHub Desktop.
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
garbage | |
ignore_this_header | |
header1 header2 | |
val1 "a,b,c,d,e" | |
val2 "x,y,z , 10129" |
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
/// Given excel sheet contents and a vec of expected header strings, returns a range starting from the header row. | |
/// If no row containing the expected header strings was found, returns Err. | |
fn skip_to_header_row( | |
range: Range<DataType>, | |
expected_headers: Vec<&str>, | |
) -> Result<Range<DataType>, Error> { | |
match range.rows().enumerate().find(|(i, row)| { | |
println!("i: {}", i); | |
println!("row: {:?}", row); | |
expected_headers | |
.iter() | |
.all(|h| row.iter().any(|cell: &DataType| &cell.to_string() == h)) | |
}) { | |
Some((ix, _)) => skip_rows(range, ix as u32), | |
None => Err(Error::new(format!( | |
"Couldn't find header row with expected headers: {:?}", | |
expected_headers | |
))), | |
} | |
} | |
fn skip_rows(range: Range<DataType>, n: u32) -> Result<Range<DataType>, Error> { | |
if range.is_empty() { | |
return Err(Error::new("Range was empty")); | |
} | |
let start = range.start().unwrap(); | |
let end = range.end().unwrap(); | |
Ok(range.range((start.0 + n, start.1), end)) | |
} | |
// Usage: | |
let range = skip_to_header_row(range, vec!["header1", "header2"])?; | |
let parsed = RangeDeserializerBuilder::new() | |
.has_headers(true) | |
.from_range(&range)?; | |
// prints: | |
i: 0 | |
row: [String("garbage"), Empty] | |
i: 1 | |
row: [Empty, Empty] | |
i: 2 | |
row: [Empty, Empty] | |
i: 3 | |
row: [Empty, Empty] | |
i: 4 | |
row: [String("ignore_this_header"), Empty] | |
i: 5 | |
row: [String("header1"), String("header2")] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment