Skip to content

Commit

Permalink
Merge pull request #472 from PrettyWood/string-cells
Browse files Browse the repository at this point in the history
fix: always parse a string cell as string
  • Loading branch information
tafia authored Oct 30, 2024
2 parents c806996 + 0acc969 commit cf86fa7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
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);
}

0 comments on commit cf86fa7

Please sign in to comment.