Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 55 date style to recognized styles list #124

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
inputs: ["*.{ex,exs}", "{config,lib,test}/**/*.{ex,exs}"]
]
30 changes: 30 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Run Tests
on:
push:

jobs:
run_tests:
runs-on: ubuntu-latest
env:
MIX_ENV: test
steps:
- uses: actions/checkout@v4

- name: Install Erlang & Elixir
uses: erlef/setup-beam@v1
id: setup-beam
with:
otp-version: '26.2'
elixir-version: '1.17.2'

- name: Install mix dependencies
run: mix deps.get

- name: Check warnings
run: mix compile --warnings-as-errors

- name: Check formatting
run: mix format --check-formatted

- name: Run tests
run: mix test
8 changes: 1 addition & 7 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# when extracting to file, files will be extracted
# in a sub directory in the `:extract_base_dir` directory.
# config :xlsxir, extract_base_dir: "temp"
import Config
1 change: 0 additions & 1 deletion lib/xlsxir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule Xlsxir do
use Application

def start(_type, _args) do

children = [
%{id: Xlsxir.StateManager, start: {Xlsxir.StateManager, :start_link, []}, type: :worker}
]
Expand Down
48 changes: 29 additions & 19 deletions lib/xlsxir/convert_date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,31 @@ defmodule Xlsxir.ConvertDate do

## Example

iex> Xlsxir.ConvertDate.from_serial('27514')
iex> Xlsxir.ConvertDate.from_serial(~c'27514')
{1975, 4, 30}
"""
def from_serial(serial) do
f_serial = serial
|> convert_char_number
|> is_float
|> case do
false -> List.to_integer(serial)
true -> serial
|> List.to_float()
|> Float.floor
|> round
end
f_serial =
serial
|> convert_char_number
|> is_float
|> case do
false ->
List.to_integer(serial)

true ->
serial
|> List.to_float()
|> Float.floor()
|> round
end

# Convert to gregorian days and get date from that
gregorian = f_serial - 2 + # adjust two days for first and last day since base year
date_to_days({1900, 1, 1}) # Add days in base year 1900
# adjust two days for first and last day since base year
# Add days in base year 1900
gregorian =
f_serial - 2 +
date_to_days({1900, 1, 1})

gregorian
|> days_to_date
Expand All @@ -50,11 +57,14 @@ defmodule Xlsxir.ConvertDate do
str
|> String.match?(~r/[.eE]/)
|> case do
false -> List.to_integer(number)
true -> case Float.parse(str) do
{f, _} -> f
_ -> raise "Invalid Float"
end
end
false ->
List.to_integer(number)

true ->
case Float.parse(str) do
{f, _} -> f
_ -> raise "Invalid Float"
end
end
end
end
17 changes: 11 additions & 6 deletions lib/xlsxir/convert_datetime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ defmodule Xlsxir.ConvertDateTime do

## Example

iex> Xlsxir.ConvertDateTime.from_charlist('41261.6013888889')
iex> Xlsxir.ConvertDateTime.from_charlist(~c'41261.6013888889')
~N[2012-12-18 14:26:00]
"""
def from_charlist('0'), do: {0, 0, 0}
def from_charlist(~c"0"), do: {0, 0, 0}

def from_charlist(charlist) do
charlist
|> List.to_float
|> List.to_float()
|> from_float
end

def from_float(n) when is_float(n) do
n = if n > 59, do: n - 1, else: n # Lotus bug
# Lotus bug
n = if n > 59, do: n - 1, else: n
convert_from_serial(n)
end

Expand All @@ -36,6 +38,7 @@ defmodule Xlsxir.ConvertDateTime do

{hours, minutes, seconds}
end

defp convert_from_serial(n) when is_float(n) do
{whole_days, fractional_day} = split_float(n)
{hours, minutes, seconds} = convert_from_serial(fractional_day)
Expand All @@ -48,9 +51,11 @@ defmodule Xlsxir.ConvertDateTime do
end

defp split_float(f) do
whole = f
|> Float.floor
whole =
f
|> Float.floor()
|> round

{whole, f - whole}
end
end
25 changes: 15 additions & 10 deletions lib/xlsxir/parse_string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,27 @@ defmodule Xlsxir.ParseString do
%__MODULE__{tid: GenServer.call(Xlsxir.StateManager, :new_table)}
end

def sax_event_handler({:startElement,_,'si',_,_}, %__MODULE__{tid: tid, index: index}), do: %__MODULE__{tid: tid, index: index}
def sax_event_handler({:startElement, _, ~c"si", _, _}, %__MODULE__{tid: tid, index: index}),
do: %__MODULE__{tid: tid, index: index}

def sax_event_handler({:startElement,_,'family',_,_}, state) do
def sax_event_handler({:startElement, _, ~c"family", _, _}, state) do
%{state | family: true}
end

def sax_event_handler({:characters, value},
%__MODULE__{family_string: fam_str} = state) do
value = value |> to_string
%{state | family_string: fam_str <> value}
def sax_event_handler(
{:characters, value},
%__MODULE__{family_string: fam_str} = state
) do
value = value |> to_string
%{state | family_string: fam_str <> value}
end

def sax_event_handler({:endElement,_,'si',_},
%__MODULE__{family_string: fam_str, tid: tid, index: index} = state) do
:ets.insert(tid, {index, fam_str})
%{state | index: index + 1}
def sax_event_handler(
{:endElement, _, ~c"si", _},
%__MODULE__{family_string: fam_str, tid: tid, index: index} = state
) do
:ets.insert(tid, {index, fam_str})
%{state | index: index + 1}
end

def sax_event_handler(_, state), do: state
Expand Down
22 changes: 11 additions & 11 deletions lib/xlsxir/parse_style.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Xlsxir.ParseStyle do
69,
70
]
@date [14, 15, 16, 17, 18, 19, 20, 21, 22, 27, 30, 36, 45, 46, 47, 50, 57]
@date [14, 15, 16, 17, 18, 19, 20, 21, 22, 27, 30, 36, 45, 46, 47, 50, 55, 57]

defstruct custom_style: %{}, cellxfs: false, index: 0, tid: nil, num_fmt_ids: []

Expand All @@ -54,23 +54,23 @@ defmodule Xlsxir.ParseStyle do
%__MODULE__{tid: GenServer.call(Xlsxir.StateManager, :new_table)}
end

def sax_event_handler({:startElement, _, 'cellXfs', _, _}, state) do
def sax_event_handler({:startElement, _, ~c"cellXfs", _, _}, state) do
%{state | cellxfs: true}
end

def sax_event_handler({:endElement, _, 'cellXfs', _}, state) do
def sax_event_handler({:endElement, _, ~c"cellXfs", _}, state) do
%{state | cellxfs: false}
end

def sax_event_handler(
{:startElement, _, 'xf', _, xml_attr},
{:startElement, _, ~c"xf", _, xml_attr},
%__MODULE__{num_fmt_ids: num_fmt_ids} = state
) do
if state.cellxfs do
xml_attr
|> Enum.filter(fn attr ->
case attr do
{:attribute, 'numFmtId', _, _, _} -> true
{:attribute, ~c"numFmtId", _, _, _} -> true
_ -> false
end
end)
Expand All @@ -79,22 +79,22 @@ defmodule Xlsxir.ParseStyle do
%{state | num_fmt_ids: num_fmt_ids ++ [id]}

_ ->
%{state | num_fmt_ids: num_fmt_ids ++ ['0']}
%{state | num_fmt_ids: num_fmt_ids ++ [~c"0"]}
end
else
state
end
end

def sax_event_handler(
{:startElement, _, 'numFmt', _, xml_attr},
{:startElement, _, ~c"numFmt", _, xml_attr},
%__MODULE__{custom_style: custom_style} = state
) do
temp =
Enum.reduce(xml_attr, %{}, fn attr, acc ->
case attr do
{:attribute, 'numFmtId', _, _, id} -> Map.put(acc, :id, id)
{:attribute, 'formatCode', _, _, cd} -> Map.put(acc, :cd, cd)
{:attribute, ~c"numFmtId", _, _, id} -> Map.put(acc, :id, id)
{:attribute, ~c"formatCode", _, _, cd} -> Map.put(acc, :cd, cd)
_ -> nil
end
end)
Expand All @@ -112,7 +112,7 @@ defmodule Xlsxir.ParseStyle do
Enum.reduce(num_fmt_ids, 0, fn style_type, acc ->
case List.to_integer(style_type) do
i when i in @num -> :ets.insert(tid, {index + acc, nil})
i when i in @date -> :ets.insert(tid, {index + acc, 'd'})
i when i in @date -> :ets.insert(tid, {index + acc, ~c"d"})
_ -> add_custom_style(tid, style_type, custom_type, index + acc)
end

Expand All @@ -129,7 +129,7 @@ defmodule Xlsxir.ParseStyle do
|> Enum.reduce(%{}, fn {k, v}, acc ->
cond do
String.match?(to_string(v), ~r/\bred\b/i) -> Map.put_new(acc, k, nil)
String.match?(to_string(v), ~r/[dhmsy]/i) -> Map.put_new(acc, k, 'd')
String.match?(to_string(v), ~r/[dhmsy]/i) -> Map.put_new(acc, k, ~c"d")
true -> Map.put_new(acc, k, nil)
end
end)
Expand Down
12 changes: 6 additions & 6 deletions lib/xlsxir/parse_workbook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ defmodule Xlsxir.ParseWorkbook do
%__MODULE__{tid: GenServer.call(Xlsxir.StateManager, :new_table)}
end

def sax_event_handler({:startElement, _, 'sheet', _, xml_attrs}, state) do
def sax_event_handler({:startElement, _, ~c"sheet", _, xml_attrs}, state) do
sheet =
Enum.reduce(xml_attrs, %{name: nil, sheet_id: nil, rid: nil}, fn attr, sheet ->
case attr do
{:attribute, 'name', _, _, name} ->
{:attribute, ~c"name", _, _, name} ->
%{sheet | name: name |> to_string}

{:attribute, 'sheetId', _, _, sheet_id} ->
{:attribute, ~c"sheetId", _, _, sheet_id} ->
{sheet_id, _} = sheet_id |> to_string |> Integer.parse()
%{sheet | sheet_id: sheet_id}

{:attribute, 'id', _, _, rid} ->
{:attribute, ~c"id", _, _, rid} ->
"rId" <> rid = rid |> to_string
{rid, _} = Integer.parse(rid)
%{sheet | rid: rid}
Expand All @@ -37,8 +37,8 @@ defmodule Xlsxir.ParseWorkbook do
end

def sax_event_handler(:endDocument, %__MODULE__{tid: tid} = state) do
Enum.map(state.sheets, fn %{sheet_id: sheet_id, name: name} ->
:ets.insert(tid, {sheet_id, name})
Enum.map(state.sheets, fn %{rid: rid, name: name} ->
:ets.insert(tid, {rid, name})
end)

state
Expand Down
Loading