Skip to content

Commit

Permalink
Use Column Style When Row Dimension Exists Without Style
Browse files Browse the repository at this point in the history
Fix PHPOffice#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 ...
  • Loading branch information
oleibman committed Aug 28, 2023
1 parent 4971801 commit fd4d9b0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1436,13 +1436,16 @@ public function createNewCell($coordinate): Cell
$rowDimension = $this->rowDimensions[$row] ?? null;
$columnDimension = $this->columnDimensions[$columnString] ?? null;

$xfSet = false;
if ($rowDimension !== null) {
$rowXf = (int) $rowDimension->getXfIndex();
if ($rowXf > 0) {
// then there is a row dimension with explicit style, assign it to the cell
$cell->setXfIndex($rowXf);
$xfSet = true;
}
} elseif ($columnDimension !== null) {
}
if (!$xfSet && $columnDimension !== null) {
$colXf = (int) $columnDimension->getXfIndex();
if ($colXf > 0) {
// then there is a column dimension, assign it to the cell
Expand Down
36 changes: 36 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3534Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;

class Issue3534Test extends \PHPUnit\Framework\TestCase
{
/**
* @var string
*/
private static $testbook = 'tests/data/Reader/XLSX/issue.3534.xlsx';

public function testReadColumnStyles(): void
{
$reader = new XlsxReader();
$spreadsheet = $reader->load(self::$testbook);
$sheet = $spreadsheet->getActiveSheet();

self::assertSame(['C4'], $sheet->getCellCollection()->getCoordinates(), 'explicitly defined despite no value because colC style conflicts with row4');
self::assertSame(1, $sheet->getColumnDimension('A')->getXfIndex());
self::assertNull($sheet->getRowDimension(1)->getXfIndex());

$sheet->getCell('A1')->setValue('a1');
self::assertSame(['C4', 'A1'], $sheet->getCellCollection()->getCoordinates());
self::assertSame(1, $sheet->getCell('A1')->getXfIndex(), 'no row style so apply column style');

$sheet->getCell('A4')->setValue('a4');
$sheet->getCell('C1')->setValue('c1');
self::assertSame('ED7D31', $sheet->getStyle('A1')->getFill()->getStartColor()->getRgb(), 'no row style so apply column style');
self::assertSame('FFC000', $sheet->getStyle('A4')->getFill()->getStartColor()->getRgb(), 'style for row is applied');
self::assertSame('9DC3E6', $sheet->getStyle('C1')->getFill()->getStartColor()->getRgb(), 'no row style so apply column style');
self::assertSame('9DC3E6', $sheet->getStyle('C4')->getFill()->getStartColor()->getRgb(), 'style was already set in xml');
$spreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/XLSX/issue.3534.xlsx
Binary file not shown.

0 comments on commit fd4d9b0

Please sign in to comment.