Skip to content

Commit 4b3b0d4

Browse files
committed
Get Style for Row or Column
This is the second recent occasion where style for whole row or column is behaving unexpectedly (see issue #4285). For the earlier issue, a documentation update was made to show the preferred method of styling. Setting styles for row(s) or column(s) in that way will work just fine, however retrieving the style doesn't yield the expected result. Although that problem can be overcome with existing code, simpler methods are needed, and this PR adds methods getRowStyle and getColumnStyle to Worksheet. I will continue to investigate why this problem and the one from 4285 have unexpected results.
1 parent fb757cf commit 4b3b0d4

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

src/PhpSpreadsheet/Spreadsheet.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,11 @@ public function getCellXfByIndex(int $cellStyleIndex): Style
10801080
return $this->cellXfCollection[$cellStyleIndex];
10811081
}
10821082

1083+
public function getCellXfByIndexOrNull(?int $cellStyleIndex): ?Style
1084+
{
1085+
return ($cellStyleIndex === null) ? null : ($this->cellXfCollection[$cellStyleIndex] ?? null);
1086+
}
1087+
10831088
/**
10841089
* Get cellXf by hash code.
10851090
*

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,13 @@ public function getRowDimension(int $row): RowDimension
13331333
return $this->rowDimensions[$row];
13341334
}
13351335

1336+
public function getRowStyle(int $row): ?Style
1337+
{
1338+
return $this->parent?->getCellXfByIndexOrNull(
1339+
($this->rowDimensions[$row] ?? null)?->getXfIndex()
1340+
);
1341+
}
1342+
13361343
public function rowDimensionExists(int $row): bool
13371344
{
13381345
return isset($this->rowDimensions[$row]);
@@ -1376,6 +1383,13 @@ public function getColumnDimensionByColumn(int $columnIndex): ColumnDimension
13761383
return $this->getColumnDimension(Coordinate::stringFromColumnIndex($columnIndex));
13771384
}
13781385

1386+
public function getColumnStyle(string $column): ?Style
1387+
{
1388+
return $this->parent?->getCellXfByIndexOrNull(
1389+
($this->columnDimensions[$column] ?? null)?->getXfIndex()
1390+
);
1391+
}
1392+
13791393
/**
13801394
* Get styles.
13811395
*
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ColumnRowStyleTest extends TestCase
11+
{
12+
public function testColumnStyle(): void
13+
{
14+
$spreadsheet = new Spreadsheet();
15+
$sheet = $spreadsheet->getActiveSheet();
16+
$columnStyle = $sheet->getStyle('B:C');
17+
$columnStyle->applyFromArray([
18+
'font' => ['name' => 'Who knows'],
19+
]);
20+
self::assertSame(
21+
'Who knows',
22+
$sheet->getColumnStyle('B')?->getFont()->getName()
23+
);
24+
self::assertSame(
25+
'Who knows',
26+
$sheet->getColumnStyle('C')?->getFont()->getName()
27+
);
28+
self::assertNull(
29+
$sheet->getColumnStyle('A')?->getFont()->getName()
30+
);
31+
self::assertNull(
32+
$sheet->getColumnStyle('D')?->getFont()->getName()
33+
);
34+
35+
$spreadsheet->disconnectWorksheets();
36+
}
37+
38+
public function testRowStyle(): void
39+
{
40+
$spreadsheet = new Spreadsheet();
41+
$sheet = $spreadsheet->getActiveSheet();
42+
$rowStyle = $sheet->getStyle('2:3');
43+
$rowStyle->applyFromArray([
44+
'font' => ['name' => 'Who knows'],
45+
]);
46+
self::assertSame(
47+
'Who knows',
48+
$sheet->getRowStyle(2)?->getFont()->getName()
49+
);
50+
self::assertSame(
51+
'Who knows',
52+
$sheet->getRowStyle(3)?->getFont()->getName()
53+
);
54+
self::assertNull(
55+
$sheet->getRowStyle(1)?->getFont()->getName()
56+
);
57+
self::assertNull(
58+
$sheet->getRowStyle(4)?->getFont()->getName()
59+
);
60+
61+
$spreadsheet->disconnectWorksheets();
62+
}
63+
}

0 commit comments

Comments
 (0)