Skip to content

Commit

Permalink
cond format: add support for bool type values
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Jan 27, 2025
1 parent ccb0d72 commit 403b50f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dev/docs/source/format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ properties that can be applied and the equivalent object method:
+------------+------------------+----------------------+------------------------------+
| | Right color | ``'right_color'`` | :func:`set_right_color()` |
+------------+------------------+----------------------+------------------------------+
| Misc. | Cell border | ``'quote_prefix'`` | :func:`set_quote_prefix()` |
| Other | Cell border | ``'quote_prefix'`` | :func:`set_quote_prefix()` |
+------------+------------------+----------------------+------------------------------+
| | Checkbox format | ``'checkbox'`` | :func:`set_checkbox()` |
+------------+------------------+----------------------+------------------------------+
Expand Down
31 changes: 31 additions & 0 deletions xlsxwriter/test/comparison/test_checkbox05.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,34 @@ def test_create_file_with_boolean_and_format(self):
workbook.close()

self.assertExcelEqual()

def test_conditional_format_with_boolean(self):
"""Subtest for conditional format value as a Python boolean."""

workbook = Workbook(self.got_filename)
worksheet = workbook.add_worksheet()

cell_format1 = workbook.add_format({"checkbox": True})

worksheet.write("E9", False, cell_format1)

cell_format2 = workbook.add_format(
{
"font_color": "#9C0006",
"bg_color": "#FFC7CE",
}
)

worksheet.conditional_format(
"E9",
{
"type": "cell",
"format": cell_format2,
"criteria": "equal to",
"value": False,
},
)

workbook.close()

self.assertExcelEqual()
5 changes: 5 additions & 0 deletions xlsxwriter/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2936,6 +2936,11 @@ def conditional_format(
if "criteria" in options and options["criteria"] in criteria_type:
options["criteria"] = criteria_type[options["criteria"]]

# Convert boolean values if required.
if "value" in options:
if isinstance(options["value"], bool):
options["value"] = str(options["value"]).upper()

# Convert date/times value if required.
if options["type"] in ("date", "time"):
options["type"] = "cellIs"
Expand Down

0 comments on commit 403b50f

Please sign in to comment.