Skip to content

Commit

Permalink
Reset all set attributes for every new cell
Browse files Browse the repository at this point in the history
If a cell element was missing some attribute, we would retain the attributes of the previous cell.
  • Loading branch information
xavier committed Jun 26, 2024
1 parent 8d557b5 commit 8ec935e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/xlsx_reader/parsers/worksheet_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,19 @@ defmodule XlsxReader.Parsers.WorksheetParser do

## Cell format handling

@cell_attributes_mapping %{
"r" => :cell_ref,
"s" => :cell_style,
"t" => :cell_type
}

defp extract_cell_attributes(attributes) do
Utils.map_attributes(attributes, @cell_attributes_mapping)
# Initialize current cell attributes
Utils.map_attributes(
attributes,
%{
"r" => :cell_ref,
"s" => :cell_style,
"t" => :cell_type
},
# Make we start from a blank slate to prevent reusing the previous cell data
# if one of the attribute is missing
%{cell_ref: nil, cell_style: nil, cell_type: nil}
)
end

defp format_cell_data(state) do
Expand Down
39 changes: 39 additions & 0 deletions test/compatibility_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,43 @@ defmodule CompatibilityTest do
assert {:ok, [["", ""], ["", "b2"]]} = XlsxReader.sheet(package, "Sheet1", empty_rows: true)
assert {:ok, [["", "b2"]]} = XlsxReader.sheet(package, "Sheet1", empty_rows: false)
end

test "cells with missing attributes" do

Check failure on line 116 in test/compatibility_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.3.2.6 / Elixir 1.15.6

test cells with missing attributes (CompatibilityTest)

Check failure on line 116 in test/compatibility_test.exs

View workflow job for this annotation

GitHub Actions / OTP 24.3.4.13 / Elixir 1.15.6

test cells with missing attributes (CompatibilityTest)
# The worksheet has a mix of "s", "t" and no attributes
assert {:ok, package} = XlsxReader.open(TestFixtures.path("cells_missing_attributes.xlsx"))

assert {
:ok,
[
[
"Number",
"Name",
"Effective Date",
"ID Number",
"Operator",
"County",
"State",
"Product",
"Description",
"Acres"
],
[
"Number",
"Name",
# Date with s="5" is converted
~D[2024-07-01],
# This cell has no attributes at all, it's treated as a string
# and not converted to a date because all cell attributes are
# properly reset
"5575789630",
"Operator",
"County",
"State",
"Product",
"Description",
"Acres"
]
]
} = XlsxReader.sheet(package, "Sheet 1")
end
end

0 comments on commit 8ec935e

Please sign in to comment.