Skip to content

Commit

Permalink
Fix last row missed issue and infinite loop problem
Browse files Browse the repository at this point in the history
  • Loading branch information
suexcxine committed Jul 27, 2021
1 parent 090c96e commit dad2f2f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/exoffice/parser/excel_2003.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule Exoffice.Parser.Excel2003 do
"""
def count_rows(pid) do
Xlsxir.get_multi_info(pid, :rows)
:ets.info(pid, :size)
end

@doc """
Expand Down
16 changes: 12 additions & 4 deletions lib/exoffice/parser/excel_2003/loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ defmodule Exoffice.Parser.Excel2003.Loader do
loader <- get_stream(ole),
{stream, _pos, excel} <- parse(loader, 0, %Excel2003{data_size: byte_size(loader.data)}),
pids = parse_sheets(loader, excel, sheet) do
Enum.map(pids, fn {status, pid, _} -> {status, pid} end)
Enum.map(pids, fn {status, pid, _} ->
sort_ets_table_by_row(pid)
{status, pid}
end)
else
{:error, reason} -> {:error, reason}
end
Expand Down Expand Up @@ -193,7 +196,7 @@ defmodule Exoffice.Parser.Excel2003.Loader do
# add cell
case :ets.match(pid, {row, :"$1"}) do
[[cells]] ->
:ets.insert(pid, {row, cells ++ [[column_string <> to_string(row), value]]})
:ets.insert(pid, {row, [[column_string <> to_string(row), value]] ++ cells})

_ ->
:ets.insert(pid, {row, [[column_string <> to_string(row), value]]})
Expand All @@ -218,7 +221,7 @@ defmodule Exoffice.Parser.Excel2003.Loader do
# add cell
case :ets.match(pid, {row, :"$1"}) do
[[cells]] ->
:ets.insert(pid, {row, cells ++ [[column_string <> to_string(row), value]]})
:ets.insert(pid, {row, [[column_string <> to_string(row), value]] ++ cells})

_ ->
:ets.insert(pid, {row, [[column_string <> to_string(row), value]]})
Expand All @@ -241,7 +244,7 @@ defmodule Exoffice.Parser.Excel2003.Loader do
# add cell
case :ets.match(pid, {row, :"$1"}) do
[[cells]] ->
:ets.insert(pid, {row, cells ++ [[column_string <> to_string(row), nil]]})
:ets.insert(pid, {row, [[column_string <> to_string(row), nil]] ++ cells})

_ ->
:ets.insert(pid, {row, [[column_string <> to_string(row), nil]]})
Expand Down Expand Up @@ -733,4 +736,9 @@ defmodule Exoffice.Parser.Excel2003.Loader do
new_pos = pos + length + 4
apply(__MODULE__, fun, if(pid, do: [loader, new_pos, excel, pid], else: [loader, new_pos, excel]))
end

def sort_ets_table_by_row(tid) do
rows = :ets.tab2list(tid) |> Enum.map(fn {row, cells} -> {row, Enum.sort(cells)} end)
:ets.insert(tid, rows)
end
end

0 comments on commit dad2f2f

Please sign in to comment.