Skip to content

Commit 263a4a4

Browse files
authored
Avoid NULL in String Function Call (#3617)
The overall problem is described in issue #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.
1 parent f1d90aa commit 263a4a4

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/PhpSpreadsheet/Calculation/Functions.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static function ifCondition($condition): string
153153
{
154154
$condition = self::flattenSingleValue($condition);
155155

156-
if ($condition === '') {
156+
if ($condition === '' || $condition === null) {
157157
return '=""';
158158
}
159159
if (!is_string($condition) || !in_array($condition[0], ['>', '<', '='], true)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
6+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
7+
8+
class Issue3613Test extends AbstractFunctional
9+
{
10+
private static string $testbook = 'tests/data/Reader/XLSX/issue.3613.xlsx';
11+
12+
// Partial fix only. We will no longer throw exception on save.
13+
// But calculation for cell value is 0, which is incorrect.
14+
public function testIssue3613(): void
15+
{
16+
$reader = new Xlsx();
17+
$spreadsheet = $reader->load(self::$testbook);
18+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
19+
$spreadsheet->disconnectWorksheets();
20+
$sheet = $reloadedSpreadsheet->getActiveSheet();
21+
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());
22+
23+
$reloadedSpreadsheet->disconnectWorksheets();
24+
}
25+
}
51.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)