Commit f6a04828 authored by eirikur@Eirikurs-MacBook-Pro.local's avatar eirikur@Eirikurs-MacBook-Pro.local
Browse files

Change how the top-left cell of a merged range is found

--HG--
branch : fix-mergedcell-hyperlink-read-only
parent c2df7930dec7
......@@ -378,14 +378,18 @@ class WorksheetReader(object):
pass
else:
cell = self.ws[link.ref]
cell = self._change_to_top_left_cell_if_merged(cell)
if isinstance(cell, MergedCell):
cell = self.normalize_merged_cell_link(cell.coordinate)
cell.hyperlink = link
def _change_to_top_left_cell_if_merged(self, cell):
if not isinstance(cell, MergedCell):
return cell
r = self.ws.merged_cells.find_range_containing(cell.coordinate)
return self.ws[r.top_left_coord]
def normalize_merged_cell_link(self, coord):
"""
Returns the appropriate cell to which a hyperlink, which references a merged cell at the specified coordinates,
should be bound.
"""
for rng in self.ws.merged_cells:
if coord in rng:
return self.ws.cell(*rng.top[0])
def bind_col_dimensions(self):
for col, cd in self.parser.column_dimensions.items():
......
......@@ -91,12 +91,6 @@ class CellRange(Serialisable):
max_row=self.max_row
)
@property
def top_left_coord(self):
"""
Coordinate of the cell in the top left corner of the range
"""
return get_column_letter(self.min_col) + str(self.min_row)
@property
def rows(self):
......@@ -505,13 +499,3 @@ class MultiCellRange(Strict):
for r in self.ranges:
n.ranges.append(copy(r))
return n
def find_range_containing(self, coord):
"""
Finds and returns the first range that contains the cell with the specified coordinates.
Returns None if no range is found.
"""
for r in self.ranges:
if coord in r:
return r
return None
......@@ -800,6 +800,21 @@ class TestWorksheetReader:
assert ws.merged_cells == "G18:H18 G23:H24 A18:B18"
@pytest.mark.parametrize(argnames="input_coordinate,expected_coordinate",
argvalues=[("H18", "G18"), ("G18", "G18"), ("I18", None), ("H23", "G23")])
def test_normalize_merged_cell_link(self, PrimedWorksheetReader, input_coordinate, expected_coordinate):
reader = PrimedWorksheetReader
reader.bind_cells()
reader.bind_merged_cells()
normalized = reader.normalize_merged_cell_link(input_coordinate)
if expected_coordinate is None:
assert normalized is None
else:
assert normalized.coordinate == expected_coordinate
def test_external_hyperlinks(self, PrimedWorksheetReader):
reader = PrimedWorksheetReader
reader.bind_cells()
......
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