PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.34k stars 3.47k forks source link

Calculation error when switching IF 2nd and 3rd parameters #3790

Open NathanDid opened 1 year ago

NathanDid commented 1 year ago

This is:

What is the expected behavior?

The calcul of the formula =IF(FALSE, IF( LEFT( "0", 3 ) = "16-", 1, IF( OR( 1 = IF( "Le L" = "Le L", 6.89, 2 ), AND( 0.735 = 1, "0" <> "" ) ), "", 1 ) ), 3) should succeed and return 3.

What is the current behavior?

An exception is thrown exception [exc:PhpOffice\PhpSpreadsheet\Calculation\Exception("Worksheet!B5 -> internal error")] has been thrown.

What are the steps to reproduce?

<?php

require __DIR__ . '/vendor/autoload.php';

$formulas = [
    '=IF(FALSE, 3, IF( LEFT( "0", 3 ) = "16-", 1, IF( OR( 1 = IF( "Le L" = "Le L", 6.89, 2 ), AND( 0.735 = 1, "0" <> "" ) ), "", 1 ) ))', // THIS ONE WORKS
    '=IF(FALSE, IF( LEFT( "0", 3 ) = "16-", 1, IF( OR( 1 = IF( "Le L" = "Le L", 6.89, 2 ), AND( 0.735 = 1, "0" <> "" ) ), "", 1 ) ), 3)', // THIS NOT
];

$calculation = Calculation::getInstance();

foreach ($formulas as $formula) {
    $calculation->calculateFormula($formula);
}

What features do you think are causing the issue

Which versions of PhpSpreadsheet and PHP are affected?

php 7.4, phpoffice/phpspreadsheet 1.29.0

oleibman commented 2 months ago

Seems to be a duplicate of #3755. Leaving both open for the time being.