forked from jsonkenl/xlsxir
-
Notifications
You must be signed in to change notification settings - Fork 2
/
xlsxir.patch
53 lines (51 loc) · 2.11 KB
/
xlsxir.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
diff --git a/lib/xlsxir/parse_workbook.ex b/lib/xlsxir/parse_workbook.ex
index c620350..233b8fb 100644
--- a/lib/xlsxir/parse_workbook.ex
+++ b/lib/xlsxir/parse_workbook.ex
@@ -37,8 +37,11 @@ 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})
+ state.sheets
+ |> Enum.reverse()
+ |> Enum.with_index(1)
+ |> Enum.each(fn {%{name: name}, index} ->
+ :ets.insert(tid, {index, name})
end)
state
diff --git a/test/test_data/swapped-sheet-order.xlsx b/test/test_data/swapped-sheet-order.xlsx
new file mode 100644
index 0000000..a694433
Binary files /dev/null and b/test/test_data/swapped-sheet-order.xlsx differ
diff --git a/test/xlsxir_test.exs b/test/xlsxir_test.exs
index 3090e76..69eaacf 100644
--- a/test/xlsxir_test.exs
+++ b/test/xlsxir_test.exs
@@ -157,4 +157,26 @@ defmodule XlsxirTest do
assert map["B2"] == "pre 2008"
assert map["B3"] == "https://msdn.microsoft.com/en-us/library/office/gg278314.aspx"
end
+
+ test "swapped order of sheets still correctly identifies sheet names" do
+ # The order of <sheet> tags in the workbook.xml will match the "sheet(index+1).xml" file names
+ # of the worksheets. (see also %Xlsxir.XmlFile{name: "sheet#.xml"}
+ # Previously the framework relied on the rid or sheetId values to identify the order
+ # sheetId especially is unreliable for this because its numbers are created like a
+ # sequential id in a database and moving sheets or deleting sheets can lead to gaps
+ # and non-sequential values.
+ [{:ok, pid_sheet_2}, {:ok, pid_sheet_1}] = multi_extract("./test/test_data/swapped-sheet-order.xlsx")
+ on_exit(fn ->
+ close(pid_sheet_1)
+ close(pid_sheet_2)
+ end)
+ map_sheet_2 = get_map(pid_sheet_2)
+ map_sheet_1 = get_map(pid_sheet_1)
+
+ assert map_sheet_2["A1"] == "SHEET2CELL"
+ assert map_sheet_1["A1"] == "SHEET1CELL"
+
+ assert get_info(pid_sheet_2, :name) == "Sheet2"
+ assert get_info(pid_sheet_1, :name) == "Sheet1"
+ end
end