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

COUNTIF() does not properly parse comparison operators #526

Closed
billblume opened this issue Jun 2, 2018 · 3 comments
Closed

COUNTIF() does not properly parse comparison operators #526

billblume opened this issue Jun 2, 2018 · 3 comments

Comments

@billblume
Copy link
Contributor

This is:

- [x] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

COUNTIF() should properly parse comparison expressions in the selection criteria parameter.

What is the current behavior?

PhpSpreadsheet is generating the error Formula Error: Unexpected operator '<' when it tries to evaluate the function:

COUNTIF(F12,"<>< Please Select >")

The underlying issue is that method Calculations\Functions::ifCondition() is not properly parsing the conditional operator in the front of the selection parameter. For example, it thinks that the selection parameter above means:

 <>< "PLEASE SELECT >"

when it should mean

<> "< PLEASE SELECT >"

This problem is due to an overly aggressive regular expression that matches any length of '<', '>', and '=' characters, when it should be limited to just '=', '<', '>', '<>', '<=', or '>=', (see section 18.17.2.2 in ECMA 376 5th Edition Part 1 for all allowable comparison operators in Open Office Spreadsheet XML formulas.)

Other statistical functions that use ifCondition() also probably suffer from this issue. The MAXIF(), MINIF(), and AVERAGEIF() functions use ifCondition() as well.

What are the steps to reproduce?

Calling Calculations\Functions::ifCondition() on formula COUNTIF(F12,"<>< Please Select >") will generate an exception with message Formula Error: Unexpected operator '<'

Which versions of PhpSpreadsheet and PHP are affected?

This occurs in 1.2.1 of PhpSpreadsheet. I haven't checked earlier versions. I did not see this in PhpExcel.

@billblume
Copy link
Contributor Author

FYI. I am working on a PR for this.

billblume added a commit to billblume/PhpSpreadsheet that referenced this issue Jun 2, 2018
Fix for issue PHPOffice#526.  Conditional operators
in the selection parameter of COUNTIF functions were not being
parsed properly, causing evaluations of formulae with such
functions to sometimes fail.
billblume added a commit to billblume/PhpSpreadsheet that referenced this issue Jun 2, 2018
Fix for issue PHPOffice#526.  Conditional operators
in the selection parameter of COUNTIF functions were not being
parsed properly, causing evaluations of formulae with such
functions to sometimes fail.

(Note: This is a recommit of this fix on a cleaner branch.)
billblume added a commit to billblume/PhpSpreadsheet that referenced this issue Jun 2, 2018
Fix for issue PHPOffice#526.  Conditional operators
in the selection parameter of COUNTIF functions were not being
parsed properly, causing evaluations of formulae with such
functions to sometimes fail.

(Note: This is a recommit of this fix on a cleaner branch.)
@LucasBonafe
Copy link

This command line is returning several errors, even though I have already reviewed in Excel:
->setCellValue($F[2][1],"=COUNTIFS($F[3][0];\"<\"&$F[1][1];$F[3][0];\">\"&$F[1][0])")

The result should be:
=COUNTIFS(Y7:Y99;"<"&AL7;Y7:Y99;">"&AJ7)

If someone can help me,
the error is on line 546: xlsx.php

Thanks in advance.

@billblume
Copy link
Contributor Author

billblume commented Jun 6, 2018 via email

billblume added a commit to billblume/PhpSpreadsheet that referenced this issue Jun 12, 2018
Conditional operators in the selection parameter of COUNTIF
functions were not being parsed properly, causing evaluations
of formulae with such functions to sometimes fail.

Fixes PHPOffice#526
Closes PHPOffice#528
billblume added a commit to billblume/PhpSpreadsheet that referenced this issue Jun 13, 2018
Conditional operators in the selection parameter of COUNTIF
functions were not being parsed properly, causing evaluations
of formulae with such functions to sometimes fail.

Fixes PHPOffice#526
Closes PHPOffice#528
Dfred pushed a commit to Dfred/PhpSpreadsheet that referenced this issue Nov 20, 2018
Conditional operators in the selection parameter of COUNTIF
functions were not being parsed properly, causing evaluations
of formulae with such functions to sometimes fail.

Fixes PHPOffice#526
Closes PHPOffice#528
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants