Skip to content

Commit 49a04bd

Browse files
authored
Use Column Style When Row Dimension Exists Without Style (#3688)
* Use Column Style When Row Dimension Exists Without Style Fix #3534. For new cells, style is set to Row Dimension Style when Row Dimension exists (even if Row Dimension Style does not exist), else Column Dimension Style when Column Dimension exists. However it should be set to Row Dimension Style when Row Dimension exists *and* Row Dimension Style exists, else ... * Address Issue 3533 If cell is defined in xml, it uses neither row nor column style. If style is omitted in cell definition, default style is used. * Update CHANGELOG.md
1 parent 06c4aff commit 49a04bd

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
4545
- Inconsistent String Handling for Sum Functions. [Issue #3652](https://github.com/PHPOffice/PhpSpreadsheet/issues/3652) [PR #3653](https://github.com/PHPOffice/PhpSpreadsheet/pull/3653)
4646
- Recomputation of Relative Addresses in Defined Names. [Issue #3661](https://github.com/PHPOffice/PhpSpreadsheet/issues/3661) [PR #3673](https://github.com/PHPOffice/PhpSpreadsheet/pull/3673)
4747
- Writer Xls Characters Outside BMP (emojis). [Issue #642](https://github.com/PHPOffice/PhpSpreadsheet/issues/642) [PR #3696](https://github.com/PHPOffice/PhpSpreadsheet/pull/3696)
48+
- Xlsx Reader Improve Handling of Row and Column Styles. [Issue #3533](https://github.com/PHPOffice/PhpSpreadsheet/issues/3533) [Issue #3534](https://github.com/PHPOffice/PhpSpreadsheet/issues/3534) [PR #3688](https://github.com/PHPOffice/PhpSpreadsheet/pull/3688)
4849

4950
## 1.29.0 - 2023-06-15
5051

src/PhpSpreadsheet/Reader/Xlsx.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -955,14 +955,17 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
955955
}
956956

957957
// Style information?
958-
if ($cAttr['s'] && !$this->readDataOnly) {
958+
if (!$this->readDataOnly) {
959+
$holdSelected = $docSheet->getSelectedCells();
960+
$cAttrS = (int) ($cAttr['s'] ?? 0);
959961
// no style index means 0, it seems
960-
$cell->setXfIndex(isset($styles[(int) ($cAttr['s'])]) ?
961-
(int) ($cAttr['s']) : 0);
962+
$cell->setXfIndex(isset($styles[$cAttrS]) ?
963+
$cAttrS : 0);
962964
// issue 3495
963965
if ($cell->getDataType() === DataType::TYPE_FORMULA) {
964966
$cell->getStyle()->setQuotePrefix(false);
965967
}
968+
$docSheet->setSelectedCells($holdSelected);
966969
}
967970
}
968971
++$rowIndex;

src/PhpSpreadsheet/Worksheet/Worksheet.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1436,13 +1436,16 @@ public function createNewCell($coordinate): Cell
14361436
$rowDimension = $this->rowDimensions[$row] ?? null;
14371437
$columnDimension = $this->columnDimensions[$columnString] ?? null;
14381438

1439+
$xfSet = false;
14391440
if ($rowDimension !== null) {
14401441
$rowXf = (int) $rowDimension->getXfIndex();
14411442
if ($rowXf > 0) {
14421443
// then there is a row dimension with explicit style, assign it to the cell
14431444
$cell->setXfIndex($rowXf);
1445+
$xfSet = true;
14441446
}
1445-
} elseif ($columnDimension !== null) {
1447+
}
1448+
if (!$xfSet && $columnDimension !== null) {
14461449
$colXf = (int) $columnDimension->getXfIndex();
14471450
if ($colXf > 0) {
14481451
// then there is a column dimension, assign it to the cell
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
6+
use PhpOffice\PhpSpreadsheet\Style\Fill;
7+
8+
class Issue3534Test extends \PHPUnit\Framework\TestCase
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private static $testbook = 'tests/data/Reader/XLSX/issue.3534.xlsx';
14+
15+
public function testReadColumnStyles(): void
16+
{
17+
$reader = new XlsxReader();
18+
$spreadsheet = $reader->load(self::$testbook);
19+
$sheet = $spreadsheet->getActiveSheet();
20+
21+
self::assertSame(['B4', 'C4'], $sheet->getCellCollection()->getCoordinates(), 'explicitly defined despite no value because differ from row style');
22+
self::assertSame(1, $sheet->getColumnDimension('A')->getXfIndex());
23+
self::assertNull($sheet->getRowDimension(1)->getXfIndex());
24+
25+
$sheet->getCell('A1')->setValue('a1');
26+
self::assertSame(['B4', 'C4', 'A1'], $sheet->getCellCollection()->getCoordinates());
27+
self::assertSame(1, $sheet->getCell('A1')->getXfIndex(), 'no row style so apply column style');
28+
29+
$sheet->getCell('A4')->setValue('a4');
30+
$sheet->getCell('C1')->setValue('c1');
31+
self::assertSame('ED7D31', $sheet->getStyle('A1')->getFill()->getStartColor()->getRgb(), 'no row style so apply column style');
32+
self::assertSame('FFC000', $sheet->getStyle('A4')->getFill()->getStartColor()->getRgb(), 'style for row is applied');
33+
self::assertSame('9DC3E6', $sheet->getStyle('C1')->getFill()->getStartColor()->getRgb(), 'no row style so apply column style');
34+
self::assertSame('9DC3E6', $sheet->getStyle('C4')->getFill()->getStartColor()->getRgb(), 'style was already set in xml');
35+
self::assertSame(Fill::FILL_NONE, $sheet->getStyle('B4')->getFill()->getFillType(), 'style implicitly default in xml');
36+
$spreadsheet->disconnectWorksheets();
37+
}
38+
}
9.28 KB
Binary file not shown.

0 commit comments

Comments
 (0)