Skip to content

Instantly share code, notes, and snippets.

@TimoFreiberg
Created May 15, 2019 15:54
Show Gist options
  • Save TimoFreiberg/3cfc0163d5b1e5b3e4fab11f948b5299 to your computer and use it in GitHub Desktop.
Save TimoFreiberg/3cfc0163d5b1e5b3e4fab11f948b5299 to your computer and use it in GitHub Desktop.
garbage
ignore_this_header
header1 header2
val1 "a,b,c,d,e"
val2 "x,y,z , 10129"
/// 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