I am trying to write a spreadsheet in Python which uses conditional formatting to find the largest of three cells in a row and give it a green fill. I have been able to do it for the comparison of two cells, but there is very little information about how to write a formula in the openpyxl documentation that I can see, and there is similarly very little on StackOverflow about the problem.
For two cells, which worked as desired, my code was the below:
sheet.conditional_formatting.add(f'G${row+iter_num}', CellIsRule(operator='greaterThan', \
formula=[f'H${row+iter_num}'], fill=greenfill))
sheet.conditional_formatting.add(f'H${row+iter_num}', CellIsRule(operator='greaterThan', \
formula=[f'G${row+iter_num}'], fill=greenfill))
The {row+iter_num} is required as this is used in a for loop.
To do this comparison for more cells, I tried changing the formula to include and:
sheet.conditional_formatting.add(f'G${row+iter_num}', CellIsRule(operator='greaterThan', \
formula=[f'H${row+iter_num}' and f'I${row+iter_num}'], fill=greenfill))
sheet.conditional_formatting.add(f'H${row+iter_num}', CellIsRule(operator='greaterThan', \
formula=[f'G${row+iter_num}' and f'I${row+iter_num}'], fill=greenfill))
sheet.conditional_formatting.add(f'I${row+iter_num}', CellIsRule(operator='greaterThanOrEqual', \
formula=[f'G${row+iter_num}' and f'H${row+iter_num}'], fill=greenfill))
I am not sure if using and is logical in this situation, but like I said there is very little in the documentation. I also cannot use any comparisons to find which of G, H and I are largest in Python as they are determined by excel functions. For the image above, the green cells for each row should be 200, 16, 82, 1890, 150 (second), 1025, 527, 392, 150 (second), there should only be one green cell per row.
