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.
Add Ability to Ignore Cell Errors in Excel
Fix PHPOffice#1141, which had been closed as stale, but which I have reopened. Excel will show cells with certain "errors" with a green triangle in the upper left. The suggestion in the issue to use quotePrefix to suppress the numberStoredAsText error is ineffective. In Excel, the user can turn this indicator off for individual cells. Cells where this is turned off can be detected at read time, and PhpSpreadsheet will now process those. In addition, the user can explicitly set the ignored error as in Excel. ```php $cell->setIgnoredErrorNumberStoredAsText(true); ``` There are a number of different errors that can be ignored in this fashion. This PR implements `numberStoredAsText` (which is likely to be by far the most useful one), `formula`, `twoDigitTextYear`, and `evalError`, all of which are demonstrated in the new test spreadsheet. There are several others for which I am not able to create good examples; I have not implemented those, but they can be easily added if needed (`calculatedColumn`, `emptyCellReference`, `formulaRange`, `listDataValidation`, and `unlockedFormula`).
- Loading branch information
Showing
5 changed files
with
222 additions
and
1 deletion.
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
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
58 changes: 58 additions & 0 deletions
58
tests/PhpSpreadsheetTests/Reader/Xlsx/IgnoredErrorTest.php
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,58 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; | ||
|
||
use PhpOffice\PhpSpreadsheet\Cell\DataType; | ||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; | ||
|
||
class IgnoredErrorTest extends AbstractFunctional | ||
{ | ||
private const FILENAME = 'tests/data/Reader/XLSX/ignoreerror.xlsx'; | ||
|
||
public function testIgnoredError(): void | ||
{ | ||
$reader = new Xlsx(); | ||
$originalSpreadsheet = $reader->load(self::FILENAME); | ||
$spreadsheet = $this->writeAndReload($originalSpreadsheet, 'Xlsx'); | ||
$originalSpreadsheet->disconnectWorksheets(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
self::assertFalse($sheet->getCell('A1')->getIgnoredErrorNumberStoredAsText()); | ||
self::assertTrue($sheet->getCell('A2')->getIgnoredErrorNumberStoredAsText()); | ||
self::assertFalse($sheet->getCell('H2')->getIgnoredErrorNumberStoredAsText()); | ||
self::assertTrue($sheet->getCell('H3')->getIgnoredErrorNumberStoredAsText()); | ||
self::assertFalse($sheet->getCell('I2')->getIgnoredErrorNumberStoredAsText()); | ||
self::assertTrue($sheet->getCell('I3')->getIgnoredErrorNumberStoredAsText()); | ||
|
||
self::assertFalse($sheet->getCell('H3')->getIgnoredErrorFormula()); | ||
self::assertFalse($sheet->getCell('D2')->getIgnoredErrorFormula()); | ||
self::assertTrue($sheet->getCell('D3')->getIgnoredErrorFormula()); | ||
|
||
self::assertFalse($sheet->getCell('A11')->getIgnoredErrorTwoDigitTextYear()); | ||
self::assertTrue($sheet->getCell('A12')->getIgnoredErrorTwoDigitTextYear()); | ||
|
||
self::assertFalse($sheet->getCell('C12')->getIgnoredErrorEvalError()); | ||
self::assertTrue($sheet->getCell('C11')->getIgnoredErrorEvalError()); | ||
|
||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public function testSetIgnoredError(): void | ||
{ | ||
$originalSpreadsheet = new Spreadsheet(); | ||
$originalSheet = $originalSpreadsheet->getActiveSheet(); | ||
$originalSheet->getCell('A1')->setValueExplicit('0', DataType::TYPE_STRING); | ||
$originalSheet->getCell('A2')->setValueExplicit('1', DataType::TYPE_STRING); | ||
$originalSheet->getStyle('A1:A2')->setQuotePrefix(true); | ||
$originalSheet->getCell('A2')->setIgnoredErrorNumberStoredAsText(true); | ||
$spreadsheet = $this->writeAndReload($originalSpreadsheet, 'Xlsx'); | ||
$originalSpreadsheet->disconnectWorksheets(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
self::assertSame('0', $sheet->getCell('A1')->getValue()); | ||
self::assertSame('1', $sheet->getCell('A2')->getValue()); | ||
self::assertFalse($sheet->getCell('A1')->getIgnoredErrorNumberStoredAsText()); | ||
self::assertTrue($sheet->getCell('A2')->getIgnoredErrorNumberStoredAsText()); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
} |
Binary file not shown.