From c226537fb46be727e994bf9abcf0fe819f7b7cfe Mon Sep 17 00:00:00 2001 From: Vinicius Moraes <67981656+ding-an-sich@users.noreply.github.com> Date: Sun, 9 Jun 2024 13:23:37 -0300 Subject: [PATCH] Handle date and datetime values encoded with numeric cell type (#33) --- lib/xlsx_reader/parsers/worksheet_parser.ex | 6 ++ test/compatibility_test.exs | 112 +++++++++++--------- 2 files changed, 69 insertions(+), 49 deletions(-) diff --git a/lib/xlsx_reader/parsers/worksheet_parser.ex b/lib/xlsx_reader/parsers/worksheet_parser.ex index 43846bc..bace11d 100644 --- a/lib/xlsx_reader/parsers/worksheet_parser.ex +++ b/lib/xlsx_reader/parsers/worksheet_parser.ex @@ -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() diff --git a/test/compatibility_test.exs b/test/compatibility_test.exs index eab03fe..a04c00d 100644 --- a/test/compatibility_test.exs +++ b/test/compatibility_test.exs @@ -2,6 +2,69 @@ 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( @@ -37,55 +100,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"))