forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Handle REF Error as Part of Range
Fix PHPOffice#3453. User sets a valid formula (e.g. `=SUM(Sheet2!B1:Sheet2!B3)`), and then does something to invalidate the formula (e.g. delete Sheet2). Excel changes the formula to `SUM(#REF!:#REF!)` when the spreadsheet is saved; apparently someone thought this was a good idea. But PhpSpreadsheet (a) used to throw an Exception when it evaluated the formula, and (b) now gives a result of `0` when evaluating the formula. Neither is ideal. It would be better to propagate the `#REF!` error. It is likely that more tests are needed, which is why I will keep this in draft status for a bit.
- Loading branch information
Showing
3 changed files
with
68 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests; | ||
|
||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RefRangeTest extends TestCase | ||
{ | ||
/** | ||
* @param int|string $expectedResult | ||
* | ||
* @dataProvider providerRefRange | ||
*/ | ||
public function testRefRange($expectedResult, string $rangeString): void | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$sheet->getCell('A1')->setValue("=SUM($rangeString)"); | ||
self::assertSame($expectedResult, $sheet->getCell('A1')->getCalculatedValue()); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public function providerRefRange(): array | ||
{ | ||
return [ | ||
'normal range' => [0, 'B1:B2'], | ||
'ref as end of range' => ['#REF!', 'B1:#REF!'], | ||
'ref as start of range' => ['#REF!', '#REF!:B2'], | ||
'ref as both parts of range' => ['#REF!', '#REF!:#REF!'], | ||
'using indirect for ref' => ['#REF!', 'B1:INDIRECT("XYZ")'], | ||
]; | ||
} | ||
|
||
public function testRefRangeRead(): void | ||
{ | ||
$reader = new Xlsx(); | ||
$spreadsheet = $reader->load('tests/data/Reader/XLSX/issue.3453.xlsx'); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
self::assertSame(0, $sheet->getCell('H1')->getCalculatedValue()); | ||
self::assertSame('#REF!', $sheet->getCell('H2')->getCalculatedValue()); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
} |
Binary file not shown.