Skip to content

Commit

Permalink
fix: treat #REF! cells as nulls (#244)
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Peschke <[email protected]>
  • Loading branch information
lukapeschke authored Jul 1, 2024
1 parent 0b41321 commit 0224f8f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Binary file modified python/tests/fixtures/sheet-with-na.xlsx
Binary file not shown.
14 changes: 14 additions & 0 deletions python/tests/test_fastexcel.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,20 @@ def test_sheet_with_na():
pl_assert_frame_equal(sheet.to_polars(), pl.DataFrame(expected))


def test_sheet_with_ref():
"""Test reading a sheet with #REF! cells. For now, we consider them as null"""
excel_reader = fastexcel.read_excel(path_for_fixture("sheet-with-na.xlsx"))
sheet = excel_reader.load_sheet("Broken refs")

assert sheet.name == "Broken refs"
assert sheet.height == sheet.total_height == 2
assert sheet.width == 1

expected = {"numbers": [1.0, None]}
pd_assert_frame_equal(sheet.to_pandas(), pd.DataFrame(expected))
pl_assert_frame_equal(sheet.to_polars(), pl.DataFrame(expected))


@pytest.mark.parametrize("excel_file", ["sheet-null-strings.xlsx", "sheet-null-strings-empty.xlsx"])
def test_null_strings(excel_file: str):
excel_reader = fastexcel.read_excel(path_for_fixture(excel_file))
Expand Down
6 changes: 4 additions & 2 deletions src/types/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ fn get_cell_dtype<DT: CellType + Debug + DataType>(
Ok(DType::Null)
} else if cell.is_error() {
match cell.get_error() {
// considering cells with #N/A! as null
Some(CellErrorType::NA | CellErrorType::Value | CellErrorType::Null) => Ok(DType::Null),
// considering cells with #N/A! or #REF! as null
Some(
CellErrorType::NA | CellErrorType::Value | CellErrorType::Null | CellErrorType::Ref,
) => Ok(DType::Null),
Some(err) => Err(FastExcelErrorKind::CalamineCellError(err.to_owned()).into()),
None => Err(FastExcelErrorKind::Internal(format!(
"cell is an error but get_error returned None: {cell:?}"
Expand Down

0 comments on commit 0224f8f

Please sign in to comment.