Skip to content

Commit

Permalink
Handle date and datetime values encoded with numeric cell type (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ding-an-sich authored and xavier committed Jun 9, 2024
1 parent eab0cf3 commit a17fab9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 49 deletions.
6 changes: 6 additions & 0 deletions lib/xlsx_reader/parsers/worksheet_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ defmodule XlsxReader.Parsers.WorksheetParser do

# Numbers

{"n", :date, value} ->
value |> Conversion.to_date(state.workbook.base_date) |> handle_conversion_error()

{"n", type, value} when type in [:time, :date_time] ->
value |> Conversion.to_date_time(state.workbook.base_date) |> handle_conversion_error()

{"n", _, value} ->
value |> Conversion.to_number(state.number_type) |> handle_conversion_error()

Expand Down
118 changes: 69 additions & 49 deletions test/compatibility_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,75 @@
defmodule CompatibilityTest do
use ExUnit.Case

describe "elixlsx" do
test "file generated by elixlsx" do
test_row = [
"string1",
"",
nil,
:empty,
"string1",
"string2",
123,
true,
false
]

workbook = %Elixlsx.Workbook{
sheets: [
%Elixlsx.Sheet{
name: "sheet1",
rows: [
test_row
]
},
%Elixlsx.Sheet{name: "sheet2", rows: []}
]
}

assert {:ok, {_filename, zip_binary}} = Elixlsx.write_to_memory(workbook, "test.xlsx")

assert {:ok, package} = XlsxReader.open(zip_binary, source: :binary)

assert ["sheet1", "sheet2"] = XlsxReader.sheet_names(package)

assert {:ok,
[
[
"string1",
"",
nil,
nil,
"string1",
"string2",
123.0,
true,
false
]
]} = XlsxReader.sheet(package, "sheet1", blank_value: nil)

assert {:ok, []} = XlsxReader.sheet(package, "sheet2")
end

test "sheets with dates and datetimes" do
s1 =
Elixlsx.Sheet.with_name("1")
|> Elixlsx.Sheet.set_cell("A1", {{2015, 11, 30}, {21, 20, 38}}, datetime: true)

s2 =
Elixlsx.Sheet.with_name("2")
|> Elixlsx.Sheet.set_cell("A1", {{2015, 11, 30}, {21, 20, 38}}, yyyymmdd: true)

wk = %Elixlsx.Workbook{sheets: [s1, s2]}

{:ok, {_filename, bin}} = Elixlsx.write_to_memory(wk, "test.xlsx")
{:ok, package} = XlsxReader.open(bin, source: :binary)
{:ok, sheets} = XlsxReader.sheets(package)

assert [{"1", [[~N[2015-11-30 21:20:38]]]}, {"2", [[~D[2015-11-30]]]}] = sheets
end
end

test "google_spreadsheet.xlsx" do
assert {:ok, package} =
XlsxReader.open(
Expand Down Expand Up @@ -37,55 +106,6 @@ defmodule CompatibilityTest do
]} = XlsxReader.sheet(package, "merged")
end

test "file generated by elixlsx" do
test_row = [
"string1",
"",
nil,
:empty,
"string1",
"string2",
123,
true,
false
]

workbook = %Elixlsx.Workbook{
sheets: [
%Elixlsx.Sheet{
name: "sheet1",
rows: [
test_row
]
},
%Elixlsx.Sheet{name: "sheet2", rows: []}
]
}

assert {:ok, {_filename, zip_binary}} = Elixlsx.write_to_memory(workbook, "test.xlsx")

assert {:ok, package} = XlsxReader.open(zip_binary, source: :binary)

assert ["sheet1", "sheet2"] = XlsxReader.sheet_names(package)

assert {:ok,
[
[
"string1",
"",
nil,
nil,
"string1",
"string2",
123.0,
true,
false
]
]} = XlsxReader.sheet(package, "sheet1", blank_value: nil)

assert {:ok, []} = XlsxReader.sheet(package, "sheet2")
end

test "file with omitted row elements" do
assert {:ok, package} = XlsxReader.open(TestFixtures.path("omitted_row.xlsx"))

Expand Down

0 comments on commit a17fab9

Please sign in to comment.