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

A few updates #118

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
56 changes: 56 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Pull Request Workflow
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review, labeled, unlabeled]
push:
branches:
- main

jobs:
test:
name: Elixir Tests
continue-on-error: ${{ matrix.experimental }}
env:
MIX_ENV: test
strategy:
fail-fast: true
matrix:
include:
- os: "ubuntu-latest"
otp: "26.2.5.3"
elixir: "1.17.3"
experimental: false
- os: "ubuntu-latest"
otp: "27.0.1"
elixir: "1.17.3"
experimental: false
- os: "ubuntu-latest"
otp: "27.1.1"
elixir: "1.17.3"
experimental: false
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: ${{matrix.otp}}
elixir-version: ${{matrix.elixir}}
- uses: actions/cache@v4
id: cache
with:
path: |
deps
_build
key: ${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}--mix-${{ hashFiles('**/mix.lock') }}
restore-keys: |
${{ runner.os }}-${{ steps.beam.outputs.elixir-version }}-${{ steps.beam.outputs.otp-version }}--mix-
- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: mix deps.get
- name: Compile
run: mix compile --warnings-as-errors
- name: Check code formatting
run: mix format --check-formatted
- name: Run Elixir tests
run: mix test

11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
import Config

# when extracting to file, files will be extracted
# in a sub directory in the `:extract_base_dir` directory.
Expand Down
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
50 changes: 30 additions & 20 deletions lib/xlsxir/convert_date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,35 @@ defmodule Xlsxir.ConvertDate do

## Parameters

- `serial` - ISO 8601 date format serial in `char_list` format (i.e. 4/30/75 as '27514')
- `serial` - ISO 8601 date format serial in `char_list` format (i.e. 4/30/75 as ~c"27514")

## 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
24 changes: 12 additions & 12 deletions lib/xlsxir/parse_style.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Xlsxir.ParseStyle do

@doc """
Sax event utilized by `Xlsxir.SaxParser.parse/2`. Takes a pattern and the current state of a struct and recursivly parses the
styles XML file, ultimately saving each parsed style type to the ETS process. The style types generated are `nil` for numbers and `'d'` for dates.
styles XML file, ultimately saving each parsed style type to the ETS process. The style types generated are `nil` for numbers and `~c"d"` for dates.

## Parameters

Expand All @@ -48,29 +48,29 @@ defmodule Xlsxir.ParseStyle do

## Example
Recursively sends style types generated from parsing the `xl/sharedStrings.xml` file to ETS process. The data can ultimately
be retreived from the ETS table (i.e. `:ets.lookup(tid, 0)` would return `nil` or `'d'` depending on each style type generated).
be retreived from the ETS table (i.e. `:ets.lookup(tid, 0)` would return `nil` or `~c"d"` depending on each style type generated).
"""
def sax_event_handler(:startDocument, _state) 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
8 changes: 4 additions & 4 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 Down
Loading