Skip to content

Applying Styles on Named Ranges fails #2279

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

Closed
bjalt opened this issue Aug 26, 2021 · 3 comments
Closed

Applying Styles on Named Ranges fails #2279

bjalt opened this issue Aug 26, 2021 · 3 comments

Comments

@bjalt
Copy link

bjalt commented Aug 26, 2021

This is:

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

What is the expected behavior?

The styles can be applied using named ranges.

What is the current behavior?

Exception thrown in /src/PhpSpreadsheet/Cell/Coordinate.php on line 42 with message Invalid cell coordinate

What are the steps to reproduce?

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', Date::stringToExcel((new DateTime())->format('Y-m-t')));
$spreadsheet->addNamedRange( new \PhpOffice\PhpSpreadsheet\NamedRange('EXAMPLE', $sheet, '=$A$1') );

$sheet->getStyle('EXAMPLE')->getNumberFormat()->setFormatCode('yyyy-mm-dd');

Which versions of PhpSpreadsheet and PHP are affected?

I tried with 1.18 but I suspect that the change in 1.15 and the new possibilities with Named Ranges are the cause of the issue.

@oleibman
Copy link
Collaborator

The getStyle method does not support named ranges, and I don't think the 1.15 change affected that. You can certainly make a case that it should support them, or at least throw an exception if they remain unsupported. But there are complications; for example, what should happen if the named range is on a different sheet? Also, I don't think Excel offers an option to style a named range; you can effectively do that by selecting all the cells in the range and styling them, but you can equally well do that in your application (by specifying the actual coordinates rather than the defined name).

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Aug 29, 2021
See issue PHPOffice#2279. User requests an enhancement so that you can set a Style on a Named Range. The attempt is failing because setting the style causes a call to setSelectedCells, which does not account for Named Ranges. Although not related to the issue, it is worth noting that setSelectedCells does nothing to attempt to validate its input.

The request seems reasonable, even if it is probably more than Excel itself offers. I have added code to setSelectedCells to recognize Named Ranges (if and only if they are defined on the sheet in question). It will throw an exception if the string passed as coordinates cannot be parsed as a range of cells or an appropriate Named Range, e.e.g. a Named Range on a different sheet, a non-existent named range, named formulas, formulas, use of sheet name qualifiers (even for the same sheet). Tests are, of course, added for all of those and for the original issue. The code in setSelectedCells is tested in a very large number of cases in the test suite, none of which showed any problems after this change.
@bjalt
Copy link
Author

bjalt commented Aug 30, 2021

@oleibman Thank you for your input. I've implemented the selection of all cells in the the range. Throwing an exception would a good addition.

oleibman added a commit that referenced this issue Sep 11, 2021
* Validate Input to SetSelectedCells

See issue #2279. User requests an enhancement so that you can set a Style on a Named Range. The attempt is failing because setting the style causes a call to setSelectedCells, which does not account for Named Ranges. Although not related to the issue, it is worth noting that setSelectedCells does nothing to attempt to validate its input.

The request seems reasonable, even if it is probably more than Excel itself offers. I have added code to setSelectedCells to recognize Named Ranges (if and only if they are defined on the sheet in question). It will throw an exception if the string passed as coordinates cannot be parsed as a range of cells or an appropriate Named Range, e.e.g. a Named Range on a different sheet, a non-existent named range, named formulas, formulas, use of sheet name qualifiers (even for the same sheet). Tests are, of course, added for all of those and for the original issue. The code in setSelectedCells is tested in a very large number of cases in the test suite, none of which showed any problems after this change.

* Scrutinizer

2 minor (non-fatal) corrections, including 1 where Phpstan and Scrutinizer have a different idea about return values from preg_replace.
@PowerKiKi
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants