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

Update configuration and fix deprecation warnings related to charlists #133

Open
wants to merge 5 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: 2 additions & 2 deletions 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
# and its dependencies with the aid of the Config module.
import Config

# when extracting to file, files will be extracted
# in a sub directory in the `:extract_base_dir` directory.
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
25 changes: 13 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,23 @@ 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 +113,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 +130,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
38 changes: 19 additions & 19 deletions lib/xlsxir/parse_worksheet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ defmodule Xlsxir.ParseWorksheet do
end

def sax_event_handler(
{:startElement, _, 'row', _, _},
{:startElement, _, ~c"row", _, _},
%__MODULE__{tid: tid, max_rows: max_rows},
_excel,
_
) do
%__MODULE__{tid: tid, max_rows: max_rows}
end

def sax_event_handler({:startElement, _, 'c', _, xml_attr}, state, %{styles: styles_tid}, _) do
def sax_event_handler({:startElement, _, ~c"c", _, xml_attr}, state, %{styles: styles_tid}, _) do
a =
Enum.reduce(xml_attr, %{}, fn attr, acc ->
case attr do
{:attribute, 's', _, _, style} ->
{:attribute, ~c"s", _, _, style} ->
Map.put(acc, "s", find_styles(styles_tid, List.to_integer(style)))

{:attribute, key, _, _, ref} ->
Expand All @@ -80,19 +80,19 @@ defmodule Xlsxir.ParseWorksheet do
%{state | cell_ref: cell_ref, num_style: num_style, data_type: data_type}
end

def sax_event_handler({:startElement, _, 'f', _, _}, state, _, _) do
def sax_event_handler({:startElement, _, ~c"f", _, _}, state, _, _) do
%{state | value_type: :formula}
end

def sax_event_handler({:startElement, _, el, _, _}, state, _, _) when el in ['v', 't'] do
def sax_event_handler({:startElement, _, el, _, _}, state, _, _) when el in [~c"v", ~c"t"] do
%{state | value_type: :value}
end

def sax_event_handler({:endElement, _, el, _, _}, state, _, _) when el in ['f', 'v', 't'] do
def sax_event_handler({:endElement, _, el, _, _}, state, _, _) when el in [~c"f", ~c"v", ~c"t"] do
%{state | value_type: nil}
end

def sax_event_handler({:startElement, _, 'is', _, _}, state, _, _),
def sax_event_handler({:startElement, _, ~c"is", _, _}, state, _, _),
do: %{state | value_type: :value}

def sax_event_handler({:characters, value}, state, _, _) do
Expand All @@ -103,7 +103,7 @@ defmodule Xlsxir.ParseWorksheet do
end
end

def sax_event_handler({:endElement, _, 'c', _}, %__MODULE__{row: row} = state, excel, _) do
def sax_event_handler({:endElement, _, ~c"c", _}, %__MODULE__{row: row} = state, excel, _) do
cell_value = format_cell_value(excel, [state.data_type, state.num_style, state.value])
new_cell = [to_string(state.cell_ref), cell_value]

Expand All @@ -118,7 +118,7 @@ defmodule Xlsxir.ParseWorksheet do
end

def sax_event_handler(
{:endElement, _, 'row', _},
{:endElement, _, ~c"row", _},
%__MODULE__{tid: tid, max_rows: max_rows} = state,
_excel,
_
Expand Down Expand Up @@ -180,7 +180,7 @@ defmodule Xlsxir.ParseWorksheet do
acc + char - 65 + 1
end)

"#{column_from_index(col_index + 1, '')}#{line}"
"#{column_from_index(col_index + 1, ~c"")}#{line}"
end

def fill_empty_cells(from, from, _line, cells), do: Enum.reverse(cells)
Expand All @@ -202,22 +202,22 @@ defmodule Xlsxir.ParseWorksheet do
# Empty cell with assigned attribute
[_, _, ""] -> nil
# Type error
['e', _, e] -> List.to_string(e)
[~c"e", _, e] -> List.to_string(e)
# Type string
['s', _, i] -> find_string(strings_tid, List.to_integer(i))
[~c"s", _, i] -> find_string(strings_tid, List.to_integer(i))
# Type number
[nil, nil, n] -> convert_char_number(n)
['n', nil, n] -> convert_char_number(n)
[~c"n", nil, n] -> convert_char_number(n)
# ISO 8601 type date
[nil, 'd', d] -> convert_date_or_time(d)
['n', 'd', d] -> convert_date_or_time(d)
['d', 'd', d] -> convert_iso_date(d)
[nil, ~c"d", d] -> convert_date_or_time(d)
[~c"n", ~c"d", d] -> convert_date_or_time(d)
[~c"d", ~c"d", d] -> convert_iso_date(d)
# Type formula w/ string
['str', _, s] -> List.to_string(s)
[~c"str", _, s] -> List.to_string(s)
# Type boolean
['b', _, s] -> s == '1'
[~c"b", _, s] -> s == ~c"1"
# Type string
['inlineStr', _, s] -> List.to_string(s)
[~c"inlineStr", _, s] -> List.to_string(s)
# Unmapped type
_ -> raise "Unmapped attribute #{Enum.at(list, 0)}. Unable to process"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/xlsxir/stream_worksheet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Xlsxir.StreamWorksheet do
%ParseWorksheet{}
end

def sax_event_handler({:endElement, _, 'row', _}, state, _excel) do
def sax_event_handler({:endElement, _, ~c"row", _}, state, _excel) do
unless Enum.empty?(state.row) do
value = state.row |> Enum.reverse()

Expand Down
Loading