-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3358 from PHPOffice/Issue-3356_AutoFit-for-Table-…
…AutoFilter Allow for Table AutoFilter dropdown icon for AutoFit column sizing
Showing
6 changed files
with
192 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheet\Worksheet; | ||
|
||
use PhpOffice\PhpSpreadsheet\Cell\CellAddress; | ||
use PhpOffice\PhpSpreadsheet\Cell\CellRange; | ||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; | ||
|
||
class AutoFit | ||
{ | ||
protected Worksheet $worksheet; | ||
|
||
public function __construct(Worksheet $worksheet) | ||
{ | ||
$this->worksheet = $worksheet; | ||
} | ||
|
||
public function getAutoFilterIndentRanges(): array | ||
{ | ||
$autoFilterIndentRanges = []; | ||
$autoFilterIndentRanges[] = $this->getAutoFilterIndentRange($this->worksheet->getAutoFilter()); | ||
|
||
foreach ($this->worksheet->getTableCollection() as $table) { | ||
/** @var Table $table */ | ||
if ($table->getShowHeaderRow() === true && $table->getAllowFilter() === true) { | ||
$autoFilter = $table->getAutoFilter(); | ||
if ($autoFilter !== null) { | ||
$autoFilterIndentRanges[] = $this->getAutoFilterIndentRange($autoFilter); | ||
} | ||
} | ||
} | ||
|
||
return array_filter($autoFilterIndentRanges); | ||
} | ||
|
||
private function getAutoFilterIndentRange(AutoFilter $autoFilter): ?string | ||
{ | ||
$autoFilterRange = $autoFilter->getRange(); | ||
$autoFilterIndentRange = null; | ||
|
||
if (!empty($autoFilterRange)) { | ||
$autoFilterRangeBoundaries = Coordinate::rangeBoundaries($autoFilterRange); | ||
$autoFilterIndentRange = (string) new CellRange( | ||
CellAddress::fromColumnAndRow($autoFilterRangeBoundaries[0][0], $autoFilterRangeBoundaries[0][1]), | ||
CellAddress::fromColumnAndRow($autoFilterRangeBoundaries[1][0], $autoFilterRangeBoundaries[0][1]) | ||
); | ||
} | ||
|
||
return $autoFilterIndentRange; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Worksheet; | ||
|
||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Worksheet\Table; | ||
use PhpOffice\PhpSpreadsheet\Worksheet\Table\TableStyle; | ||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class AutoSizeTest extends TestCase | ||
{ | ||
protected Spreadsheet $spreadsheet; | ||
|
||
protected Worksheet $worksheet; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$spreadsheet = new Spreadsheet(); | ||
$this->worksheet = $spreadsheet->getActiveSheet(); | ||
|
||
$this->worksheet->setCellValue('A1', 'YEAR') | ||
->setCellValue('B1', 'QUARTER') | ||
->setCellValue('C1', 'COUNTRY') | ||
->setCellValue('D1', 'SALES'); | ||
$dataArray = [ | ||
['10', 'Q1', 'United States', 790], | ||
['10', 'Q2', 'United States', 730], | ||
['10', 'Q3', 'United States', 860], | ||
['10', 'Q4', 'United States', 850], | ||
]; | ||
|
||
$this->worksheet->fromArray($dataArray, null, 'A2'); | ||
|
||
$toColumn = $this->worksheet->getHighestColumn(); | ||
++$toColumn; | ||
for ($i = 'A'; $i !== $toColumn; ++$i) { | ||
$this->worksheet->getColumnDimension($i)->setAutoSize(true); | ||
} | ||
} | ||
|
||
protected function setTable(): Table | ||
{ | ||
$table = new Table('A1:D5', 'Sales_Data'); | ||
$tableStyle = new TableStyle(); | ||
$tableStyle->setTheme(TableStyle::TABLE_STYLE_MEDIUM2); | ||
$table->setStyle($tableStyle); | ||
$this->worksheet->addTable($table); | ||
|
||
return $table; | ||
} | ||
|
||
protected function readColumnSizes(): array | ||
{ | ||
$columnSizes = []; | ||
$toColumn = $this->worksheet->getHighestColumn(); | ||
++$toColumn; | ||
for ($column = 'A'; $column !== $toColumn; ++$column) { | ||
$columnSizes[$column] = $this->worksheet->getColumnDimension($column)->getWidth(); | ||
} | ||
|
||
return $columnSizes; | ||
} | ||
|
||
public function testStandardAutoSize(): void | ||
{ | ||
$this->worksheet->calculateColumnWidths(); | ||
$columnSizes = $this->readColumnSizes(); | ||
|
||
self::assertSame(['A' => 5.856, 'B' => 9.283, 'C' => 16.425, 'D' => 6.998], $columnSizes); | ||
} | ||
|
||
public function testAutoFilterAutoSize(): void | ||
{ | ||
$this->worksheet->setAutoFilter('A1:D5'); | ||
|
||
$this->worksheet->calculateColumnWidths(); | ||
$columnSizes = $this->readColumnSizes(); | ||
|
||
self::assertSame(['A' => 8.141, 'B' => 11.569, 'C' => 16.425, 'D' => 9.283], $columnSizes); | ||
} | ||
|
||
public function testTableWithAutoFilterAutoSize(): void | ||
{ | ||
$this->setTable(); | ||
|
||
$this->worksheet->calculateColumnWidths(); | ||
$columnSizes = $this->readColumnSizes(); | ||
|
||
self::assertSame(['A' => 8.141, 'B' => 11.569, 'C' => 16.425, 'D' => 9.283], $columnSizes); | ||
} | ||
|
||
public function testTableWithoutHiddenHeadersAutoSize(): void | ||
{ | ||
$table = $this->setTable(); | ||
$table->setShowHeaderRow(false); | ||
|
||
$this->worksheet->calculateColumnWidths(); | ||
$columnSizes = $this->readColumnSizes(); | ||
|
||
self::assertSame(['A' => 5.856, 'B' => 9.283, 'C' => 16.425, 'D' => 6.998], $columnSizes); | ||
} | ||
} |