question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue when 'equal to' conditional formatting is used with strings

See original GitHub issue

Hi,

I am using XlsxWriter to write Excel files with conditional formatting. The problem I am encountering is that an equals sign is always appended to the value which gets inserted into the conditional formatting configuration in my Excel workbook. This causes text-based conditional formatting rules to not evaluate correctly. Numeric-based conditional formatting rules do not appear to be affected.

I am using Python version 3.6, XlsxWriter 0.9.9, and Excel 2016.

Here is some code that demonstrates the problem:

import xlsxwriter

workbook = xlsxwriter.Workbook('XLSX Writer Test.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write('A1', 'X')

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

worksheet.conditional_format('A1', {'type':     'cell',
                                    'criteria': 'equal to',
                                    'value':    'X',
                                    'format':   format1})
workbook.close()

After running the above code:

  1. Open the Excel file
  2. Select cell A1
  3. On the Home ribbon, click on Conditional Formatting
  4. Double-click on the conditional formatting rule
  5. Observe that the value (which was passed as ‘X’ in the code above) is recorded as ‘=X’, which causes the rule to not execute correctly

image

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcnamaracommented, Aug 8, 2018

Hi,

Thanks for the detailed report.

XlsxWriter doesn’t prepend a ‘=’. Excel does that in the interface.

The issue is that the sting X needs to be quoted as an Excel string "X", like this:

worksheet.conditional_format('A1', {'type':     'cell',
                                    'criteria': 'equal to',
                                    'value':    '"X"',
                                    'format':   format1})

After that the sample program should work as expected.

aa_image

I’ll leave this open as an issue (maybe with a changed title) since the module should probably fix this silently in the background.

Thanks for the input.

John

0reactions
jmcnamaracommented, Aug 27, 2018

Reverted due to #564

Read more comments on GitHub >

github_iconTop Results From Across the Web

Highlight cells containing string [Conditional formatting]
How to apply Conditional Formatting · Select cell range. · Go to tab "Home" on the ribbon if you are not already there....
Read more >
Video: Conditionally format text - Microsoft Support
Click HOME > Conditional Formatting > Highlight Cells Rules > Text that Contains. In the Text that Contains box, on the left, enter...
Read more >
Use conditional formatting to highlight information
On the Home tab, in the Style group, click the arrow next to Conditional Formatting, and then click Highlight Cells Rules. ... Select...
Read more >
Excel Conditional Formatting tutorial with examples - Ablebits
End-to-end tutorial will teach you how to use Conditional Formatting in Excel: apply preset rules and create new ones, copy existing ...
Read more >
How to Combine Conditional Formatting with an IF Statement
For example, if you want to apply conditional formatting using a condition that “If a cell value is greater than a set value,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found