Skip to content

Commit f33a440

Browse files
authored
Merge pull request #4317 from oleibman/crstyle
Get Style for Row or Column
2 parents f98fd23 + a72b206 commit f33a440

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
99

1010
### Added
1111

12+
- Methods to get style for row or column. [PR #4317](https://github.com/PHPOffice/PhpSpreadsheet/pull/4317)
1213
- Method for duplicating worksheet in spreadsheet. [PR #4315](https://github.com/PHPOffice/PhpSpreadsheet/pull/4315)
1314

1415
### Changed

docs/topics/recipes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,7 @@ loop. This is much faster compared to looping through cells and styling
926926
them individually.
927927

928928
**Tip** If you are styling entire row(s) or column(s), e.g. getStyle('A:A'), it is recommended to use applyFromArray as described below rather than setting the styles individually as described above.
929+
Also, starting with release 3.9.0, you should use getRowStyle or getColumnStyle to get the style for an entire row or column.
929930

930931
There is also an alternative manner to set styles. The following code
931932
sets a cell's style to font bold, alignment right, top border thin and a

src/PhpSpreadsheet/Spreadsheet.php

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

1104+
public function getCellXfByIndexOrNull(?int $cellStyleIndex): ?Style
1105+
{
1106+
return ($cellStyleIndex === null) ? null : ($this->cellXfCollection[$cellStyleIndex] ?? null);
1107+
}
1108+
11041109
/**
11051110
* Get cellXf by hash code.
11061111
*

src/PhpSpreadsheet/Worksheet/Worksheet.php

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

1338+
public function getRowStyle(int $row): ?Style
1339+
{
1340+
return $this->parent?->getCellXfByIndexOrNull(
1341+
($this->rowDimensions[$row] ?? null)?->getXfIndex()
1342+
);
1343+
}
1344+
13381345
public function rowDimensionExists(int $row): bool
13391346
{
13401347
return isset($this->rowDimensions[$row]);
@@ -1378,6 +1385,13 @@ public function getColumnDimensionByColumn(int $columnIndex): ColumnDimension
13781385
return $this->getColumnDimension(Coordinate::stringFromColumnIndex($columnIndex));
13791386
}
13801387

1388+
public function getColumnStyle(string $column): ?Style
1389+
{
1390+
return $this->parent?->getCellXfByIndexOrNull(
1391+
($this->columnDimensions[$column] ?? null)?->getXfIndex()
1392+
);
1393+
}
1394+
13811395
/**
13821396
* Get styles.
13831397
*
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)