Skip to content

Commit

Permalink
Add Ability to Ignore Cell Errors in Excel (#3508)
Browse files Browse the repository at this point in the history
* 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
oleibman authored Apr 9, 2023
1 parent 5ef48e9 commit aab1614
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/PhpSpreadsheet/Cell/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class Cell
*/
private $formulaAttributes;

/** @var IgnoredErrors */
private $ignoredErrors;

/**
* Update the cell into the cell collection.
*
Expand Down Expand Up @@ -119,6 +122,7 @@ public function __construct($value, ?string $dataType, Worksheet $worksheet)
} elseif (self::getValueBinder()->bindValue($this, $value) === false) {
throw new Exception('Value could not be bound to cell.');
}
$this->ignoredErrors = new IgnoredErrors();
}

/**
Expand Down Expand Up @@ -796,4 +800,9 @@ public function __toString()
{
return (string) $this->getValue();
}

public function getIgnoredErrors(): IgnoredErrors
{
return $this->ignoredErrors;
}
}
66 changes: 66 additions & 0 deletions src/PhpSpreadsheet/Cell/IgnoredErrors.php
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;
}
}
50 changes: 50 additions & 0 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,12 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
++$cIndex;
}
}
if ($xmlSheetNS && $xmlSheetNS->ignoredErrors) {
foreach ($xmlSheetNS->ignoredErrors->ignoredError as $ignoredErrorx) {
$ignoredError = self::testSimpleXml($ignoredErrorx);
$this->processIgnoredErrors($ignoredError, $docSheet);
}
}

if (!$this->readDataOnly && $xmlSheetNS && $xmlSheetNS->sheetProtection) {
$protAttr = $xmlSheetNS->sheetProtection->attributes() ?? [];
Expand Down Expand Up @@ -2263,4 +2269,48 @@ private static function extractPalette(?SimpleXMLElement $sxml): array

return $array;
}

private function processIgnoredErrors(SimpleXMLElement $xml, Worksheet $sheet): void
{
$attributes = self::getAttributes($xml);
$sqref = (string) ($attributes['sqref'] ?? '');
$numberStoredAsText = (string) ($attributes['numberStoredAsText'] ?? '');
$formula = (string) ($attributes['formula'] ?? '');
$twoDigitTextYear = (string) ($attributes['twoDigitTextYear'] ?? '');
$evalError = (string) ($attributes['evalError'] ?? '');
if (!empty($sqref)) {
$explodedSqref = explode(' ', $sqref);
$pattern1 = '/^([A-Z]{1,3})([0-9]{1,7})(:([A-Z]{1,3})([0-9]{1,7}))?$/';
foreach ($explodedSqref as $sqref1) {
if (preg_match($pattern1, $sqref1, $matches) === 1) {
$firstRow = $matches[2];
$firstCol = $matches[1];
if (array_key_exists(3, $matches)) {
$lastCol = $matches[4];
$lastRow = $matches[5];
} else {
$lastCol = $firstCol;
$lastRow = $firstRow;
}
++$lastCol;
for ($row = $firstRow; $row <= $lastRow; ++$row) {
for ($col = $firstCol; $col !== $lastCol; ++$col) {
if ($numberStoredAsText === '1') {
$sheet->getCell("$col$row")->getIgnoredErrors()->setNumberStoredAsText(true);
}
if ($formula === '1') {
$sheet->getCell("$col$row")->getIgnoredErrors()->setFormula(true);
}
if ($twoDigitTextYear === '1') {
$sheet->getCell("$col$row")->getIgnoredErrors()->setTwoDigitTextYear(true);
}
if ($evalError === '1') {
$sheet->getCell("$col$row")->getIgnoredErrors()->setEvalError(true);
}
}
}
}
}
}
}
}
60 changes: 59 additions & 1 deletion src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@

class Worksheet extends WriterPart
{
/** @var string */
private $numberStoredAsText = '';

/** @var string */
private $formula = '';

/** @var string */
private $twoDigitTextYear = '';

/** @var string */
private $evalError = '';

/**
* Write worksheet to XML format.
*
Expand All @@ -28,6 +40,10 @@ class Worksheet extends WriterPart
*/
public function writeWorksheet(PhpspreadsheetWorksheet $worksheet, $stringTable = [], $includeCharts = false)
{
$this->numberStoredAsText = '';
$this->formula = '';
$this->twoDigitTextYear = '';
$this->evalError = '';
// Create XML writer
$objWriter = null;
if ($this->getParentWriter()->getUseDiskCaching()) {
Expand Down Expand Up @@ -118,6 +134,9 @@ public function writeWorksheet(PhpspreadsheetWorksheet $worksheet, $stringTable
// AlternateContent
$this->writeAlternateContent($objWriter, $worksheet);

// IgnoredErrors
$this->writeIgnoredErrors($objWriter);

// Table
$this->writeTable($objWriter, $worksheet);

Expand All @@ -131,6 +150,32 @@ public function writeWorksheet(PhpspreadsheetWorksheet $worksheet, $stringTable
return $objWriter->getData();
}

private function writeIgnoredError(XMLWriter $objWriter, bool &$started, string $attr, string $cells): void
{
if ($cells !== '') {
if (!$started) {
$objWriter->startElement('ignoredErrors');
$started = true;
}
$objWriter->startElement('ignoredError');
$objWriter->writeAttribute('sqref', substr($cells, 1));
$objWriter->writeAttribute($attr, '1');
$objWriter->endElement();
}
}

private function writeIgnoredErrors(XMLWriter $objWriter): void
{
$started = false;
$this->writeIgnoredError($objWriter, $started, 'numberStoredAsText', $this->numberStoredAsText);
$this->writeIgnoredError($objWriter, $started, 'formula', $this->formula);
$this->writeIgnoredError($objWriter, $started, 'twoDigitTextYear', $this->twoDigitTextYear);
$this->writeIgnoredError($objWriter, $started, 'evalError', $this->evalError);
if ($started) {
$objWriter->endElement();
}
}

/**
* Write SheetPr.
*/
Expand Down Expand Up @@ -1134,7 +1179,20 @@ private function writeSheetData(XMLWriter $objWriter, PhpspreadsheetWorksheet $w
array_pop($columnsInRow);
foreach ($columnsInRow as $column) {
// Write cell
$this->writeCell($objWriter, $worksheet, "{$column}{$currentRow}", $aFlippedStringTable);
$coord = "$column$currentRow";
if ($worksheet->getCell($coord)->getIgnoredErrors()->getNumberStoredAsText()) {
$this->numberStoredAsText .= " $coord";
}
if ($worksheet->getCell($coord)->getIgnoredErrors()->getFormula()) {
$this->formula .= " $coord";
}
if ($worksheet->getCell($coord)->getIgnoredErrors()->getTwoDigitTextYear()) {
$this->twoDigitTextYear .= " $coord";
}
if ($worksheet->getCell($coord)->getIgnoredErrors()->getEvalError()) {
$this->evalError .= " $coord";
}
$this->writeCell($objWriter, $worksheet, $coord, $aFlippedStringTable);
}
}

Expand Down
68 changes: 68 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/IgnoredErrorTest.php
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 added tests/data/Reader/XLSX/ignoreerror.xlsx
Binary file not shown.

0 comments on commit aab1614

Please sign in to comment.