Skip to content

Commit 6409af4

Browse files
committed
When a Worksheet has an AutoFilter, make allowance for Center Alignment in the filter heading when calculating AutoFit column widths.
1 parent 4e2bca1 commit 6409af4

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3535
- Calculation Engine doesn't evaluate Defined Name when default cell A1 is quote-prefixed [Issue #3335](https://github.com/PHPOffice/PhpSpreadsheet/issues/3335) [PR #3336](https://github.com/PHPOffice/PhpSpreadsheet/pull/3336)
3636
- XLSX Writer - Array Formulas do not include function prefix [Issue #3337](https://github.com/PHPOffice/PhpSpreadsheet/issues/3337) [PR #3338](https://github.com/PHPOffice/PhpSpreadsheet/pull/3338)
3737
- Permit Max Column for Row Breaks [Issue #3143](https://github.com/PHPOffice/PhpSpreadsheet/issues/3143) [PR #3345](https://github.com/PHPOffice/PhpSpreadsheet/pull/3345)
38-
- AutoSize Columns should allow for dropdown icon when AutoFilter is for a Table [Issue #3356](https://github.com/PHPOffice/PhpSpreadsheet/issues/3356) [PR #3358](https://github.com/PHPOffice/PhpSpreadsheet/pull/3358)
38+
- AutoSize Columns should allow for dropdown icon when AutoFilter is for a Table [Issue #3356](https://github.com/PHPOffice/PhpSpreadsheet/issues/3356) [PR #3358](https://github.com/PHPOffice/PhpSpreadsheet/pull/3358) and for Center Alignment of Headers [Issue #3395](https://github.com/PHPOffice/PhpSpreadsheet/issues/3395) [PR #3399](https://github.com/PHPOffice/PhpSpreadsheet/pull/3399)
3939
- Decimal Precision for Scientific Number Format Mask [Issue #3381](https://github.com/PHPOffice/PhpSpreadsheet/issues/3381) [PR #3382](https://github.com/PHPOffice/PhpSpreadsheet/pull/3382)
4040
- Xls Writer Parser Handle Boolean Literals as Function Arguments [Issue #3369](https://github.com/PHPOffice/PhpSpreadsheet/issues/3369) [PR #3391](https://github.com/PHPOffice/PhpSpreadsheet/pull/3391)
4141
- Conditional Formatting Improvements for Xlsx [Issue #3370](https://github.com/PHPOffice/PhpSpreadsheet/issues/3370) [Issue #3202](https://github.com/PHPOffice/PhpSpreadsheet/issues/3302) [PR #3372](https://github.com/PHPOffice/PhpSpreadsheet/pull/3372)

src/PhpSpreadsheet/Worksheet/Worksheet.php

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PhpOffice\PhpSpreadsheet\RichText\RichText;
2626
use PhpOffice\PhpSpreadsheet\Shared;
2727
use PhpOffice\PhpSpreadsheet\Spreadsheet;
28+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
2829
use PhpOffice\PhpSpreadsheet\Style\Color;
2930
use PhpOffice\PhpSpreadsheet\Style\Conditional;
3031
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
@@ -781,6 +782,7 @@ public function calculateColumnWidths()
781782
}
782783

783784
$indentAdjustment = $cell->getStyle()->getAlignment()->getIndent();
785+
$indentAdjustment += (int) ($cell->getStyle()->getAlignment()->getHorizontal() === Alignment::HORIZONTAL_CENTER);
784786

785787
// Calculated value
786788
// To formatted string

tests/PhpSpreadsheetTests/Worksheet/AutoSizeTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
44

55
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
67
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
78
use PhpOffice\PhpSpreadsheet\Worksheet\Table\TableStyle;
89
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
@@ -102,4 +103,15 @@ public function testTableWithoutHiddenHeadersAutoSize(): void
102103

103104
self::assertSame(['A' => 5.856, 'B' => 9.283, 'C' => 16.425, 'D' => 6.998], $columnSizes);
104105
}
106+
107+
public function testTableWithAutoFilterCenterHeaders(): void
108+
{
109+
$this->setTable();
110+
$this->worksheet->getStyle('A1:D1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
111+
112+
$this->worksheet->calculateColumnWidths();
113+
$columnSizes = $this->readColumnSizes();
114+
115+
self::assertSame(['A' => 10.569, 'B' => 13.997, 'C' => 16.425, 'D' => 11.711], $columnSizes);
116+
}
105117
}

0 commit comments

Comments
 (0)