Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Booleans in Conditional Styles #2654

Merged
merged 2 commits into from
Mar 10, 2022
Merged

Conversation

oleibman
Copy link
Collaborator

@oleibman oleibman commented Mar 6, 2022

You can set up a conditional style to, say, apply to cells equal to boolean values. For such conditions, the Excel XML specifies TRUE or FALSE. It is noteworthy that false matches empty cells as well as FALSE, but not 0; similarly TRUE does not match 1. The Xlsx Writer just casts these values to string, which will not work properly. The Xlsx Reader treats the values as strings, so it won't work properly either. This PR corrects both. Also the doc blocks in Style/Conditional allow bool in some places, but not in others; these are corrected but no executable code is changed there.

This is:

- [x] a bugfix
- [ ] a new feature

Checklist:

Why this change is needed?

Sorry, something went wrong.

You can set up a conditional style to, say, apply to cells equal to boolean values. For such conditions, the Excel XML specifies `TRUE` or `FALSE`. It is noteworthy that false matches empty cells as well as FALSE, but not 0; similarly TRUE does not match 1. The Xlsx Writer just casts these values to string, which will not work properly. The Xlsx Reader treats the values as strings, so it won't work properly either. This PR corrects both. Also the doc blocks in Style/Conditional allow bool in some places, but not in others; these are corrected but no executable code is changed there.
if (count($cfRule->formula) >= 1) {
foreach ($cfRule->formula as $formulax) {
$formula = (string) $formulax;
if ($formula === 'TRUE') {
Copy link
Member

@MarkBaker MarkBaker Mar 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm longing for the day when we've reached a minimum level of PHP that we can do:

$objConditional->addCondition(
    match((string) $formulax) {
        'TRUE' => true,
        'FALSE' => false,
        default => $formulax
    }
);

So much easier to read than if/elseif/else.

Does this need to make allowance for locale variants in TRUE/FALSE strings (VRAI/FAUX or VERO/FALSE)? Ah! No; this is in the Reader, so it'll be in English

@oleibman oleibman merged commit bd2e7b6 into PHPOffice:master Mar 10, 2022
@oleibman oleibman deleted the boolcond branch April 18, 2022 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants