Commit 83044c6a authored by Baudouin Giard's avatar Baudouin Giard
Browse files

Fix reading rows with skipped reference attribute

Some Excel files generated with third-party libraries such as SpreadsheetGear - there could be other, skip the "r" attribute on "c" tags within rows. Excel seems to put this attribute on every cell, but I couldn't find any evidence that the standard commands it.

openpyxl is not expecting these attributes to be skipped, so the result is that we end up with an invalid parsed row.

To fix this, when we get coordinates, make sure col_counter reflects them so the reader knows which cell it has been reading.

--HG--
branch : 3.0
parent 5c4a9070abaf
Pipeline #15604 passed with stage
in 2 minutes and 37 seconds
......@@ -175,7 +175,6 @@ class WorkSheetParser(object):
def parse_cell(self, element):
data_type = element.get('t', 'n')
coordinate = element.get('r')
self.col_counter += 1
style_id = element.get('s', 0)
if style_id:
style_id = int(style_id)
......@@ -188,7 +187,9 @@ class WorkSheetParser(object):
if coordinate:
row, column = coordinate_to_tuple(coordinate)
else:
row, column = self.row_counter, self.col_counter
row, column = self.row_counter, self.col_counter + 1
self.col_counter = column
if not self.data_only and element.find(FORMULA_TAG) is not None:
data_type = 'f'
......
......@@ -534,6 +534,37 @@ class TestWorksheetParser:
assert expected_cell == cell
def test_row_and_cell_skipping_coordinates(self, WorkSheetParser):
parser = WorkSheetParser
src = """
<row xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<c>
<v>1</v>
</c>
<c r="D1">
<v>2</v>
</c>
<c>
<v>3</v>
</c>
<c r="G1">
<v>4</v>
</c>
</row>
"""
element = fromstring(src)
_, cells = parser.parse_row(element)
expected = [
{'column': 1, 'row': 1, 'data_type': 'n', 'value': 1, 'style_id': 0},
{'column': 4, 'row': 1, 'data_type': 'n', 'value': 2, 'style_id': 0},
{'column': 5, 'row': 1, 'data_type': 'n', 'value': 3, 'style_id': 0},
{'column': 7, 'row': 1, 'data_type': 'n', 'value': 4, 'style_id': 0},
]
assert len(cells) == len(expected)
for expected_cell, cell in zip(expected, cells):
assert expected_cell == cell
def test_second_row_cell_index_without_coordinates(self, WorkSheetParser):
parser = WorkSheetParser
src = """
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment