How do you add conditional formatting to a Google sheet using gspread_formatting that colors a cell after comparing it to another cell?

73 views Asked by At

Related to this question about a fixed threshold but instead, I want each row in column B to be compared to the same row in column A and turn red if B>A.

I tried something like this, except that the second argument in BooleanCondition only accepts lists of hard-coded values, not grid ranges.

from gspread_formatting import *
wks = GOOGLE_WORKSHEET

rule = ConditionalFormatRule(
    ranges=[GridRange.from_a1_range('B2:B26', wks)],
    booleanRule=BooleanRule(
        condition=BooleanCondition('NUMBER_GREATER', [GridRange.from_a1_range('A2:A26', wks).startRowIndex]),
        format=CellFormat(textFormat=textFormat(bold=True), backgroundColor=Color(1,0,0))
    )
1

There are 1 answers

0
TheMaster On

Change condition to a CUSTOM_FORMULA:

condition=BooleanCondition('CUSTOM_FORMULA', ["=B2>A2"])