Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pyexcelerate/Worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ def set_cell_style(self, x, y, value):
self._parent.add_style(value)
if self.get_cell_value(x, y) is None:
self.set_cell_value(x, y, '')

def set_range_style(self, start, end, value):
_range = Range.Range(start, end, self)
margin_row = _range._start[0], _range._end[0]
margin_col = _range._start[1], _range._end[1]

if value._borders is not None:
for cell in _range:
r, c = cell.coordinate
if r in margin_row or c in margin_col:
self.set_cell_style(r, c, Style.Style(borders=value._borders))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is implemented as users would typically expect. If I set the borders to all four sides and then make a 2x2 range, wouldn't this add borders inside the range as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it wouldn't add borders to inner cells, but I found there are some more complicated situations that the function may not support, and I also should consider the funtion complexity. I will try a better way.


the 2x2 range example:

from pyexcelerate import Workbook, Style, Font, Alignment
from pyexcelerate.Border import Border
from pyexcelerate.Borders import Borders

data = [[1, 2, 3], [4, 5, 6], [7, 8, 9], ["a", "b", "c"]]
wb = Workbook()
ws = wb.new_sheet("test", data=data)
style = Style(
    font=Font(bold=True),
    borders=Borders(left=Border(), right=Border(), top=Border(style="double"), bottom=Border(style="double")),
    alignment=Alignment('center', 'center')
)

range = ("B2", "C3")
ws.range(*range).merge()
ws.set_range_style(*range, style)
wb.save("test.xlsx")

image

self.set_cell_style(*_range._start, value)

def get_row_style(self, row):
if row not in self._row_styles:
Expand Down