Skip to content

Commit 8770071

Browse files
authored
Handle formulas without cached result (#42)
1 parent 411a48e commit 8770071

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

lib/xlsx_reader/parsers/worksheet_parser.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ defmodule XlsxReader.Parsers.WorksheetParser do
210210
end
211211

212212
defp store_formula(state, formula) do
213-
%{state | formula: formula}
213+
%{state | formula: formula, value: nil}
214214
end
215215

216216
defp store_shared_formula(state, index, formula) do
@@ -262,7 +262,6 @@ defmodule XlsxReader.Parsers.WorksheetParser do
262262
# If the <c> element has no text child node, we didn't receive any :characters event
263263
# and the current value still contains the placeholder used by the parser
264264
:expect_chars -> ""
265-
:expect_formula -> ""
266265
# Otherwise assume that the row contains an actual value
267266
value -> value
268267
end)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
3+
<sheetData>
4+
<row r="1" spans="1:2" x14ac:dyDescent="0.2">
5+
<c r="A1">
6+
<v>
7+
1
8+
</v>
9+
</c>
10+
<c r="B1">
11+
<f>
12+
SUM(A1:A3)
13+
</f>
14+
</c>
15+
</row>
16+
</sheetData>
17+
</worksheet>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
3+
<sheetData>
4+
<row r="1" spans="1:2" x14ac:dyDescent="0.2">
5+
<c r="A1">
6+
<v>
7+
1
8+
</v>
9+
</c>
10+
<c r="B1">
11+
<f t="shared" ref="" si="0">
12+
SUM(A1:A3)
13+
</f>
14+
</c>
15+
</row>
16+
<row r="2" spans="1:2" x14ac:dyDescent="0.2">
17+
<c r="A2">
18+
<v>
19+
2
20+
</v>
21+
</c>
22+
<c r="B2">
23+
<f t="shared" si="0" />
24+
</c>
25+
</row>
26+
<row r="3" spans="1:2" x14ac:dyDescent="0.2">
27+
<c r="A3">
28+
<v>
29+
3
30+
</v>
31+
</c>
32+
<c r="B3">
33+
<f t="shared" si="0" />
34+
</c>
35+
</row>
36+
</sheetData>
37+
</worksheet>

test/xlsx_reader/parsers/worksheet_parser_test.exs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ defmodule XlsxReader.Parsers.WorksheetParserTest do
144144
assert expected == sheets
145145
end
146146

147+
test "should return empty value when result of formulas is not set", %{workbook: workbook} do
148+
sheet_xml =
149+
TestFixtures.read!("xml/worksheetWithFormulasWithoutValue.xml")
150+
|> String.replace("\n", "")
151+
|> String.replace("\t", "")
152+
153+
expected = [
154+
[
155+
%Cell{value: "1", formula: nil, ref: "A1"},
156+
%Cell{value: "", formula: "SUM(A1:A3)", ref: "B1"}
157+
]
158+
]
159+
160+
assert {:ok, expected} == WorksheetParser.parse(sheet_xml, workbook, cell_data_format: :cell)
161+
end
162+
147163
test "should return shared formulas as part of Cell struct", %{workbook: workbook} do
148164
sheet_xml =
149165
TestFixtures.read!("xml/worksheetWithSharedFormulas.xml")
@@ -168,6 +184,36 @@ defmodule XlsxReader.Parsers.WorksheetParserTest do
168184
assert {:ok, expected} == WorksheetParser.parse(sheet_xml, workbook, cell_data_format: :cell)
169185
end
170186

187+
test "should return empty value when result of shared formulas is not set", %{
188+
workbook: workbook
189+
} do
190+
sheet_xml =
191+
TestFixtures.read!("xml/worksheetWithSharedFormulasWithoutValue.xml")
192+
|> String.replace("\n", "")
193+
|> String.replace("\t", "")
194+
195+
expected = [
196+
[
197+
%XlsxReader.Cell{value: "1", formula: nil, ref: "A1"},
198+
%XlsxReader.Cell{value: "", formula: "SUM(A1:A3)", ref: "B1"}
199+
],
200+
[
201+
%XlsxReader.Cell{value: "2", formula: nil, ref: "A2"},
202+
%XlsxReader.Cell{value: "", formula: "SUM(A1:A3)", ref: "B2"}
203+
],
204+
[
205+
%XlsxReader.Cell{value: "3", formula: nil, ref: "A3"},
206+
%XlsxReader.Cell{value: "", formula: "SUM(A1:A3)", ref: "B3"}
207+
]
208+
]
209+
210+
assert {:ok, expected} ==
211+
WorksheetParser.parse(sheet_xml, workbook,
212+
cell_data_format: :cell,
213+
type_conversion: true
214+
)
215+
end
216+
171217
test "should include or exclude hidden sheets based on an option" do
172218
filepath = TestFixtures.path("hidden_sheets.xlsx")
173219

0 commit comments

Comments
 (0)