Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore cell errors in Excel (e.g. "Number Stored as Text") #1141

Closed
sgarner opened this issue Aug 18, 2019 · 4 comments · Fixed by #3508
Closed

Ignore cell errors in Excel (e.g. "Number Stored as Text") #1141

sgarner opened this issue Aug 18, 2019 · 4 comments · Fixed by #3508

Comments

@sgarner
Copy link

sgarner commented Aug 18, 2019

This is:

- [ ] a bug report
- [x] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

When a cell contains a value that looks like a number but is intentionally formatted as text (sometimes this is desirable because the values might not always be purely numeric and you don't want Excel to reformat them), and written to an XLSX file, Excel displays a warning beside the cell when viewing the worksheet.

Excel has an option to "Ignore" the error, which appears to be a flag that is then written to the cell. It would be great if this functionality could be exposed as part of the PhpSpreadsheet API.

image

For reference, there is a JavaScript project for writing XLSX files called SheetJS which has this feature, here is how they do it.

What is the current behavior?

The warning is visible to users next to Text formatted cells that Excel thinks are numbers, and there's nothing I can do about it. Users might choose to "Convert to Number" and create formatting problems (including data corruption if exported to CSV).

What are the steps to reproduce?

N/A

@MarkBaker
Copy link
Member

Setting a style of quotePrefix for those cells should resolve the issue

@stale
Copy link

stale bot commented Dec 8, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label Dec 8, 2019
@stale stale bot closed this as completed Dec 15, 2019
@LarryBarker
Copy link

@MarkBaker I'm having similar issues but a different warning, that "The formula in this cell differs from the formula in this area of the spreadsheet." Are you aware of any way to disable these warnings entirely? Or a way to resolve them?

I have a fairly complex and customized export that is seeing these errors more and more and my client has required me to fi the "little green triangles." Here is a sample:

image

What's frustrating/confusing is that these errors don't seem to be consistent. For example, if you look at the first section, the Loan Amount and Initial Funding columns have no issues. However, the next section is displaying errors, despite the cell values following the exact same pattern, with the exception the first row has $0, and the next has a different value.

Then if you look at the second section, the Spread column, has a warning because 4.25% is not the same as 4.50% so there is a warning.

I have tried multiple combinations of styling, number formatting, etc. and cannot seem to eliminate these warnings. Here is a code sample for the screen shot:

 // Apply special formatting for spread percentage
$sheet->setCellValue('J' . $i, $row->spreadPercent ? '=TEXT(' . $row->spreadPercent . ', "0.00%")' : '')
->getStyle('J' . $i)
->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_RIGHT);
$sheet->getStyle('J' . $i)->setQuotePrefix(true)
->getNumberFormat()
->setFormatCode(NumberFormat::FORMAT_PERCENTAGE_00);

 // Apply special formatting for loan origination fee percentage
$sheet->setCellValue('K' . $i, $row->originationFee ? '=TEXT('.$row->originationFee . ', "0.00%")' : '')
->getStyle('K' . $i)->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER);
$sheet->getStyle('K' . $i)->setQuotePrefix(true)
->getNumberFormat()
->setFormatCode(NumberFormat::FORMAT_PERCENTAGE_00);

Any feedback or input is greatly appreciated!

@oleibman
Copy link
Collaborator

oleibman commented Apr 3, 2023

Reopening; I will submit a PR later today to address this. The suggestion above to use quotePrefix to eliminate the problem is not effective.

@oleibman oleibman reopened this Apr 3, 2023
@stale stale bot removed stale labels Apr 3, 2023
oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Apr 3, 2023
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`).
oleibman added a commit that referenced this issue Apr 9, 2023
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

4 participants