Skip to content

Commit

Permalink
Merge pull request #4317 from oleibman/crstyle
Browse files Browse the repository at this point in the history
Get Style for Row or Column
  • Loading branch information
oleibman authored Jan 20, 2025
2 parents f98fd23 + a72b206 commit f33a440
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Added

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

### Changed
Expand Down
1 change: 1 addition & 0 deletions docs/topics/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ loop. This is much faster compared to looping through cells and styling
them individually.

**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.
Also, starting with release 3.9.0, you should use getRowStyle or getColumnStyle to get the style for an entire row or column.

There is also an alternative manner to set styles. The following code
sets a cell's style to font bold, alignment right, top border thin and a
Expand Down
5 changes: 5 additions & 0 deletions src/PhpSpreadsheet/Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,11 @@ public function getCellXfByIndex(int $cellStyleIndex): Style
return $this->cellXfCollection[$cellStyleIndex];
}

public function getCellXfByIndexOrNull(?int $cellStyleIndex): ?Style
{
return ($cellStyleIndex === null) ? null : ($this->cellXfCollection[$cellStyleIndex] ?? null);
}

/**
* Get cellXf by hash code.
*
Expand Down
14 changes: 14 additions & 0 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,13 @@ public function getRowDimension(int $row): RowDimension
return $this->rowDimensions[$row];
}

public function getRowStyle(int $row): ?Style
{
return $this->parent?->getCellXfByIndexOrNull(
($this->rowDimensions[$row] ?? null)?->getXfIndex()
);
}

public function rowDimensionExists(int $row): bool
{
return isset($this->rowDimensions[$row]);
Expand Down Expand Up @@ -1378,6 +1385,13 @@ public function getColumnDimensionByColumn(int $columnIndex): ColumnDimension
return $this->getColumnDimension(Coordinate::stringFromColumnIndex($columnIndex));
}

public function getColumnStyle(string $column): ?Style
{
return $this->parent?->getCellXfByIndexOrNull(
($this->columnDimensions[$column] ?? null)?->getXfIndex()
);
}

/**
* Get styles.
*
Expand Down
63 changes: 63 additions & 0 deletions tests/PhpSpreadsheetTests/Worksheet/ColumnRowStyleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Worksheet;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;

class ColumnRowStyleTest extends TestCase
{
public function testColumnStyle(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$columnStyle = $sheet->getStyle('B:C');
$columnStyle->applyFromArray([
'font' => ['name' => 'Who knows'],
]);
self::assertSame(
'Who knows',
$sheet->getColumnStyle('B')?->getFont()->getName()
);
self::assertSame(
'Who knows',
$sheet->getColumnStyle('C')?->getFont()->getName()
);
self::assertNull(
$sheet->getColumnStyle('A')?->getFont()->getName()
);
self::assertNull(
$sheet->getColumnStyle('D')?->getFont()->getName()
);

$spreadsheet->disconnectWorksheets();
}

public function testRowStyle(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$rowStyle = $sheet->getStyle('2:3');
$rowStyle->applyFromArray([
'font' => ['name' => 'Who knows'],
]);
self::assertSame(
'Who knows',
$sheet->getRowStyle(2)?->getFont()->getName()
);
self::assertSame(
'Who knows',
$sheet->getRowStyle(3)?->getFont()->getName()
);
self::assertNull(
$sheet->getRowStyle(1)?->getFont()->getName()
);
self::assertNull(
$sheet->getRowStyle(4)?->getFont()->getName()
);

$spreadsheet->disconnectWorksheets();
}
}

0 comments on commit f33a440

Please sign in to comment.