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

Empty Arg for Function Embedded in False Side of IF #3875

Closed
1 of 8 tasks
oleibman opened this issue Jan 25, 2024 · 1 comment · Fixed by #3879
Closed
1 of 8 tasks

Empty Arg for Function Embedded in False Side of IF #3875

oleibman opened this issue Jan 25, 2024 · 1 comment · Fixed by #3879

Comments

@oleibman
Copy link
Collaborator

This is:

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

What is the expected behavior?

See below

What is the current behavior?

See below

What are the steps to reproduce?

An empty argument passed to a function on the False side of an IF function will cause an "Internal Error" when evaluated. I initially thought this might be due to PR #3868, but this also happened before the PR. In addition, that PR affected only 4 functions, and this problem will arise when an argument is omitted from any function.

Disabling branch pruning eliminates the problem.

$sheet->setCellValue('B1', 2);
$sheet->setCellValue('A1','=if(1=B1,min(3,2,,4),99)');

A1 will be correctly calculated as 99 without pruning, but will receive an internal error with pruning.

There is no problem if no argument is omitted.

$sheet->setCellValue('B1', 2);
$sheet->setCellValue('A1','=if(1=B1,min(3,2,1,4),99)');

A1 will be correctly calculated as 99 with or without pruning.

There is no problem if an argument is omitted on the TRUE side.

$sheet->setCellValue('B1', 1);
$sheet->setCellValue('A1','=if(1=B1,min(3,2,,4),99)');

A1 will be correctly calculated as 0 with or without pruning.

What features do you think are causing the issue

  • Reader
  • Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • Charts
  • AutoFilter
  • Form Elements

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

All

Which versions of PhpSpreadsheet and PHP are affected?

All

@oleibman
Copy link
Collaborator Author

I think I have found the problem and a possible fix. Expect something within the next few days.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Jan 27, 2024
Fix PHPOffice#3875. Even better, fix PHPOffice#2146, which has been open for 2.5 years.

Empty arguments are improperly placed on the stack; in particular, they are added without `onlyIf` and `onlyIfNot` attributes.This results in problems described in 3875.

IF has a somewhat unexpected design. In Excel, `IF(false, valueIfTrue)` evaluates as `false`, but `IF(false, valueIfTrue,)` evaluates as 0. This means that IF empty arguments should be handled in the same manner as MIN/MAX/MINA/MAXA, but you need to be careful to distinguish empty from omitted.

Also note that IF requires 2 operands - `IF(true)` is an error, but `IF(true,)` evaluates to 0.
@oleibman oleibman mentioned this issue Jan 27, 2024
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant