Skip to content

Commit b547c2f

Browse files
committed
input: Don't fail when an .ods column header is empty
#378
1 parent 69e906b commit b547c2f

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Fixed
10+
11+
- Don't fail when an .ods column header is empty https://github.com/OpenDataServices/flatten-tool/issues/378
12+
913
## [0.16.0] - 2021-03-24
1014

1115
### Fixed

flattentool/input.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,10 @@ def get_sheet_lines(self, sheet_name):
882882
output_row = OrderedDict()
883883
for i, x in enumerate(row):
884884

885-
header = coli_to_header[i]
885+
try:
886+
header = coli_to_header[i]
887+
except KeyError:
888+
continue
886889
value = x
887890
if not header:
888891
# None means that the cell will be ignored
8.41 KB
Binary file not shown.
4.68 KB
Binary file not shown.

flattentool/tests/test_input_SpreadsheetInput.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,32 @@ def test_ods_input_formula(self):
312312
OrderedDict([("colC", 4), ("colD", 12)]),
313313
]
314314

315+
def test_xlsx_empty_column_header(self):
316+
xlsxinput = XLSXInput(
317+
input_name="flattentool/tests/fixtures/xlsx/empty_column_header.xlsx"
318+
)
319+
320+
xlsxinput.read_sheets()
321+
322+
assert list(xlsxinput.sub_sheet_names) == ["main"]
323+
assert list(xlsxinput.get_sheet_lines("main")) == [
324+
{"colA": "cell1", None: None},
325+
{"colA": "cell3", None: None},
326+
]
327+
328+
def test_ods_empty_column_header(self):
329+
odsinput = ODSInput(
330+
input_name="flattentool/tests/fixtures/ods/empty_column_header.ods"
331+
)
332+
333+
odsinput.read_sheets()
334+
335+
assert list(odsinput.sub_sheet_names) == ["main"]
336+
assert list(odsinput.get_sheet_lines("main")) == [
337+
{"colA": "cell1"},
338+
{"colA": "cell3"},
339+
]
340+
315341

316342
class TestInputFailure(object):
317343
def test_csv_no_directory(self):

flattentool/tests/test_unflatten.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,27 @@ def test_unflatten_org_xml_xlsx(tmpdir, input_format):
135135
open("flattentool/tests/fixtures/iati-org.xml").read()
136136
== tmpdir.join("output.xml").read()
137137
)
138+
139+
140+
@pytest.mark.parametrize("input_format", ["xlsx", "ods"])
141+
def test_unflatten_empty_column_header(tmpdir, input_format):
142+
unflatten(
143+
input_name="flattentool/tests/fixtures/{}/empty_column_header.{}".format(
144+
input_format, input_format
145+
),
146+
output_name=tmpdir.join("output.json").strpath,
147+
input_format=input_format,
148+
)
149+
assert (
150+
tmpdir.join("output.json").read()
151+
== """{
152+
"main": [
153+
{
154+
"colA": "cell1"
155+
},
156+
{
157+
"colA": "cell3"
158+
}
159+
]
160+
}"""
161+
)

0 commit comments

Comments
 (0)