Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Style for Row or Column #4317

Merged
merged 5 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading