Skip to content

Commit

Permalink
feat(api): add support for XLS files for the API target
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin committed Nov 23, 2024
1 parent 9504e76 commit 5808111
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
Binary file added dev/fixtures/xls_fixture.xls
Binary file not shown.
77 changes: 74 additions & 3 deletions src/trackers/parsers/xls_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ impl XlsParser {
data: Vec<Vec<String>>,
}

let mut workbook = calamine::Xlsx::new(BufReader::new(Cursor::new(content)))?;
let worksheets = calamine::Xlsx::new(BufReader::new(Cursor::new(content)))
.map(|mut workbook| workbook.worksheets())
.or_else(|_| {
calamine::Xls::new(BufReader::new(Cursor::new(content)))
.map(|mut workbook| workbook.worksheets())
})?;

let mut sheets = vec![];
for (sheet_name, range) in workbook.worksheets() {
for (sheet_name, range) in worksheets {
let mut sheet_rows = vec![];
for row in range.rows() {
let mut sheet_cells = vec![];
Expand Down Expand Up @@ -86,7 +91,7 @@ mod tests {
}

#[test]
fn parse() -> anyhow::Result<()> {
fn parse_xlsx() -> anyhow::Result<()> {
let fixture = load_fixture("xlsx_fixture.xlsx")?;
let parsed_data = XlsParser::parse(&fixture)?;

Expand Down Expand Up @@ -150,4 +155,70 @@ mod tests {

Ok(())
}

#[test]
fn parse_xls() -> anyhow::Result<()> {
let fixture = load_fixture("xls_fixture.xls")?;
let parsed_data = XlsParser::parse(&fixture)?;

assert_json_snapshot!(
serde_json::from_slice::<serde_json::Value>(&parsed_data)?,
@r###"
[
{
"name": "Sheet N1",
"data": [
[
"Header N1",
"Header N2",
""
],
[
"Some string",
"100500",
""
],
[
"500100",
"Some string 2",
"100"
],
[
"",
"",
"Another string"
]
]
},
{
"name": "Sheet N2",
"data": [
[
"Header N3",
"Header N4",
""
],
[
"Some string 3",
"100500",
""
],
[
"600200",
"Some string 4",
"200"
],
[
"",
"",
"Another string 2"
]
]
}
]
"###
);

Ok(())
}
}

0 comments on commit 5808111

Please sign in to comment.