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

Adding 5-8 for Excel non-standard currency formats #116

Open
wants to merge 4 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
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
4 changes: 4 additions & 0 deletions lib/xlsxir/parse_style.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ defmodule Xlsxir.ParseStyle do
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
Expand Down
2 changes: 1 addition & 1 deletion lib/xlsxir/parse_worksheet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ defmodule Xlsxir.ParseWorksheet do

def sax_event_handler(_, state, _, _), do: state

defp fill_nil(rows) do
def fill_nil(rows) do
Enum.reduce(rows, {[], nil}, fn [ref, val], {values, previous} ->
line = ~r/\d+$/ |> Regex.run(ref) |> List.first()

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 @@ -36,7 +36,7 @@ defmodule Xlsxir.StreamWorksheet do

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

# Wait for parent process to ask for the next row
receive do
Expand Down
12 changes: 11 additions & 1 deletion test/stream_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ defmodule StreamTest do
# second run will hang on missing fs resources (before fix) and hang (default 60s)
assert {:ok, _} = Task.yield( Task.async( fn() -> s |> Stream.run() end ), 2000)
# third run because reasons
assert {:ok, _} = Task.yield( Task.async( fn() -> s |> Stream.run() end ), 2000)
end

test "empty cells are filled with nil" do
s = stream_list(path(), 9)

assert s |> Enum.map(& &1) == [
[1, nil, 1, nil, 1, nil, nil, 1],
[nil, 1, nil, nil, 1, nil, 1],
[nil, nil, nil, nil, nil, 1, nil, nil, nil, 1],
[1, 1, nil, 1]
]
end
end
Binary file added test/test_data/currency_styles.xlsx
Binary file not shown.
20 changes: 20 additions & 0 deletions test/xlsxir_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule XlsxirTest do
def path(), do: "./test/test_data/test.xlsx"
def rb_path(), do: "./test/test_data/red_black.xlsx"
def missing_styles_path(), do: "./test/test_data/missing_styles.xlsx"
def currency_styles_path(), do: "./test/test_data/currency_styles.xlsx"

test "second worksheet is parsed with index argument of 1" do
{:ok, pid} = extract(path(), 1)
Expand Down Expand Up @@ -126,6 +127,25 @@ defmodule XlsxirTest do
close(pid)
end

test "parses successfully even with columns that have currency styles defined" do
{:ok, pid} = extract(currency_styles_path(), 0)

expected_rows = [
["Style 5", "Style 6", "Style 7", "Style 8"],
[
"$#,##0_);($#,##0)",
"$#,##0_);[Red]($#,##0)",
"$#,##0.00_);($#,##0.00)",
"$#,##0.00_);[Red]($#,##0.00)"
],
[1234.5678, 2345.6789, 3456.7890, 4567.8901],
[-1234.5678, -2345.6789, -3456.7890, -4567.8901]
]

assert get_list(pid) == expected_rows
close(pid)
end

test "parses cells with cell metadata successfully" do
{:ok, pid} = multi_extract("./test/test_data/cell-metadata.xlsx", 0)
assert get_list(pid) == [["hello"]]
Expand Down