-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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 (#3508)
* Add Ability to Ignore Cell Errors in Excel Fix #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`). * Scrutinizer A new change, a new Scrutinizer false positive. * Move Ignored Errors to Own Class In response to comments from @MarkBaker, implement ignoredError as a new class. This simplifies Cell by requiring only 1 new method, rather than 8+. This requires a slightly more complicated syntax. ```php $cell->getIgnoredErrors()->setNumberScoredAsText(true); ``` Mark had also suggested that there might be a pre-existing regexp for processing the cells/cellranges when reading the sqref attribute. Those in Calculation are too complicated (read "non-performant") for this piece of code; the one in Coordinates is slightly less complicated than Calculation, but still more complicated than the one I'm using, and doesn't handle ranges.
- Loading branch information
Showing
6 changed files
with
252 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheet\Cell; | ||
|
||
class IgnoredErrors | ||
{ | ||
/** @var bool */ | ||
private $numberStoredAsText = false; | ||
|
||
/** @var bool */ | ||
private $formula = false; | ||
|
||
/** @var bool */ | ||
private $twoDigitTextYear = false; | ||
|
||
/** @var bool */ | ||
private $evalError = false; | ||
|
||
public function setNumberStoredAsText(bool $value): self | ||
{ | ||
$this->numberStoredAsText = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function getNumberStoredAsText(): bool | ||
{ | ||
return $this->numberStoredAsText; | ||
} | ||
|
||
public function setFormula(bool $value): self | ||
{ | ||
$this->formula = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function getFormula(): bool | ||
{ | ||
return $this->formula; | ||
} | ||
|
||
public function setTwoDigitTextYear(bool $value): self | ||
{ | ||
$this->twoDigitTextYear = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function getTwoDigitTextYear(): bool | ||
{ | ||
return $this->twoDigitTextYear; | ||
} | ||
|
||
public function setEvalError(bool $value): self | ||
{ | ||
$this->evalError = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function getEvalError(): bool | ||
{ | ||
return $this->evalError; | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
<?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')->getIgnoredErrors()->getNumberStoredAsText()); | ||
self::assertTrue($sheet->getCell('A2')->getIgnoredErrors()->getNumberStoredAsText()); | ||
self::assertFalse($sheet->getCell('A3')->getIgnoredErrors()->getNumberStoredAsText()); | ||
self::assertTrue($sheet->getCell('A4')->getIgnoredErrors()->getNumberStoredAsText()); | ||
self::assertFalse($sheet->getCell('H2')->getIgnoredErrors()->getNumberStoredAsText()); | ||
self::assertTrue($sheet->getCell('H3')->getIgnoredErrors()->getNumberStoredAsText()); | ||
self::assertFalse($sheet->getCell('I2')->getIgnoredErrors()->getNumberStoredAsText()); | ||
self::assertTrue($sheet->getCell('I3')->getIgnoredErrors()->getNumberStoredAsText()); | ||
|
||
self::assertFalse($sheet->getCell('H3')->getIgnoredErrors()->getFormula()); | ||
self::assertFalse($sheet->getCell('D2')->getIgnoredErrors()->getFormula()); | ||
self::assertTrue($sheet->getCell('D3')->getIgnoredErrors()->getFormula()); | ||
|
||
self::assertFalse($sheet->getCell('A11')->getIgnoredErrors()->getTwoDigitTextYear()); | ||
self::assertTrue($sheet->getCell('A12')->getIgnoredErrors()->getTwoDigitTextYear()); | ||
|
||
self::assertFalse($sheet->getCell('C12')->getIgnoredErrors()->getEvalError()); | ||
self::assertTrue($sheet->getCell('C11')->getIgnoredErrors()->getEvalError()); | ||
|
||
$sheetLast = $spreadsheet->getSheetByNameOrThrow('Last'); | ||
self::assertFalse($sheetLast->getCell('D2')->getIgnoredErrors()->getFormula()); | ||
self::assertFalse($sheetLast->getCell('D3')->getIgnoredErrors()->getFormula(), 'prior sheet ignoredErrors shouldn\'t bleed'); | ||
self::assertFalse($sheetLast->getCell('A1')->getIgnoredErrors()->getNumberStoredAsText()); | ||
self::assertFalse($sheetLast->getCell('A2')->getIgnoredErrors()->getNumberStoredAsText()); | ||
self::assertTrue($sheetLast->getCell('A3')->getIgnoredErrors()->getNumberStoredAsText()); | ||
self::assertFalse($sheetLast->getCell('A4')->getIgnoredErrors()->getNumberStoredAsText(), 'prior sheet numberStoredAsText shouldn\'t bleed'); | ||
|
||
$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')->getIgnoredErrors()->setNumberStoredAsText(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')->getIgnoredErrors()->getNumberStoredAsText()); | ||
self::assertTrue($sheet->getCell('A2')->getIgnoredErrors()->getNumberStoredAsText()); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
} |
Binary file not shown.