Skip to content
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

fix: always parse a string cell as string #472

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,16 @@ pub struct Sheet {

/// Row to use as header
/// By default, the first non-empty row is used as header
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Default, Clone, Copy)]
#[non_exhaustive]
pub enum HeaderRow {
/// First non-empty row
#[default]
FirstNonEmptyRow,
/// Index of the header row
Row(u32),
}

impl Default for HeaderRow {
fn default() -> Self {
HeaderRow::FirstNonEmptyRow
}
}

// FIXME `Reader` must only be seek `Seek` for `Xls::xls`. Because of the present API this limits
// the kinds of readers (other) data in formats can be read from.
/// A trait to share spreadsheets reader functions across different `FileType`s
Expand Down
13 changes: 2 additions & 11 deletions src/xlsx/cells_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,8 @@ fn read_v<'s>(
Ok(DataRef::DateTimeIso(v))
}
Some(b"str") => {
// see http://officeopenxml.com/SScontentOverview.php
// str - refers to formula cells
// * <c .. t='v' .. > indicates calculated value (this case)
// * <c .. t='f' .. > to the formula string (ignored case
// TODO: Fully support a Data::Formula representing both Formula string &
// last calculated value?
//
// NB: the result of a formula may not be a numeric value (=A3&" "&A4).
// We do try an initial parse as Float for utility, but fall back to a string
// representation if that fails
v.parse().map(DataRef::Float).or(Ok(DataRef::String(v)))
// string
Ok(DataRef::String(v))
}
Some(b"n") => {
// n - number
Expand Down
Binary file added tests/string-ref.xlsx
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2125,3 +2125,16 @@ fn test_no_header(#[case] header_row: HeaderRow, #[case] expected: &[[Data; 2]])
.unwrap();
range_eq!(range, expected);
}

#[test]
fn test_string_ref() {
let mut xlsx: Xlsx<_> = wb("string-ref.xlsx");
let expected_range = [
[String("col1".to_string())],
[String("-8086931554011838357".to_string())],
];
// first sheet
range_eq!(xlsx.worksheet_range_at(0).unwrap().unwrap(), expected_range);
// second sheet is the same with a cell reference to the first sheet
range_eq!(xlsx.worksheet_range_at(1).unwrap().unwrap(), expected_range);
}