Skip to content

Commit

Permalink
Avoid NULL in String Function Call
Browse files Browse the repository at this point in the history
The overall problem is described in issue PHPOffice#3613. This PR represents only a partial solution. Very complicated formulas are resulting in calls to string functions using null arguments, which is deprecated in recent Php releases. In fact, there are many such deprecations for the spreadsheet in question. Eliminating the deprecations is easy. However, the result of the calculation is, for many cells, 0 rather than what Excel determines it should be. This can be overlooked to a certain extent, because Excel will recalculate when the spreadsheet is opened, so loading and saving the spreadsheet in question will result in a spreadsheet which looks okay when it is opened in Excel. Resolving the incorrect calculation in PhpSpreadsheet would be nice, so I'm leaving the issue open, but that looks too complicated for me to get a toehold.
  • Loading branch information
oleibman committed Jun 16, 2023
1 parent a2edbf8 commit 0a3d703
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static function ifCondition($condition): string
{
$condition = self::flattenSingleValue($condition);

if ($condition === '') {
if ($condition === '' || $condition === null) {
return '=""';
}
if (!is_string($condition) || !in_array($condition[0], ['>', '<', '='], true)) {
Expand Down
25 changes: 25 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3613Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class Issue3613Test extends AbstractFunctional
{
private static string $testbook = 'tests/data/Reader/XLSX/issue.3613.xlsx';

// Partial fix only. We will no longer throw exception on save.
// But calculation for cell value is 0, which is incorrect.
public function testIssue3613(): void
{
$reader = new Xlsx();
$spreadsheet = $reader->load(self::$testbook);
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
$spreadsheet->disconnectWorksheets();
$sheet = $reloadedSpreadsheet->getActiveSheet();
self::assertSame('=ROUND(MAX((O4-P4)*{0.03;0.1;0.2;0.25;0.3;0.35;0.45}-{0;2520;16920;31920;52920;85920;181920},0)-Q4,2)', $sheet->getCell('N4')->getValue());

$reloadedSpreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/XLSX/issue.3613.xlsx
Binary file not shown.

0 comments on commit 0a3d703

Please sign in to comment.