My code currently does this, I’m wondering if there is a shorter way to write this?
JavaScript
x
5
1
sheet.cell(row=i, column=3).fill = PatternFill(start_color='ff0000', end_color='ff0000', fill_type="solid")
2
sheet.cell(row=i, column=4).fill = PatternFill(start_color='ff0000', end_color='ff0000', fill_type="solid")
3
sheet.cell(row=i, column=5).fill = PatternFill(start_color='ff0000', end_color='ff0000', fill_type="solid")
4
sheet.cell(row=i, column=6).fill = PatternFill(start_color='ff0000', end_color='ff0000', fill_type="solid")
5
Advertisement
Answer
A simple loop maybe?
JavaScript
1
3
1
for r in range(3, 7):
2
sheet.cell(row = i, column = r).fill = PatternFill(start_color= 'ff0000', end_color= 'ff0000', fill_type = "solid")
3
You could also move that configuration outside the loop if you’d like, that may make configuration easier:
JavaScript
1
5
1
pfKwargs = {'start_color': 'ff0000', 'end_color': 'ff0000', 'fill_type': 'solid'}
2
3
for r in range(3, 7):
4
sheet.cell(row = i, column = r).fill = PatternFill(**pfKwargs)
5
Or perhaps move it into a function:
JavaScript
1
11
11
1
def addFills(sheet, start = 3, count = 4, start_color='ff0000', end_color='ff0000', fill_type="solid")
2
for r in range(start, start + count):
3
sheet.cell(row = i, column = r).fill = PatternFill(
4
start_color=start_color,
5
end_color=end_color,
6
fill_type=fill_type)
7
8
addFills(sheet) # does the same as the above.
9
addFills(sheet, 1, 200) # does the same as the above, starting at 1 and ending at 201
10
addFills(sheet, end_color='00ff00') # Same as above only green end color.
11