-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Column Widths, Especially for ODS (#3610)
Fix #3609. Reporter created a spreadsheet with non-adjacent columns having non-default widths. Ods Writer needs to generate entries for the missing columns with default width. The fix was fairly simple. Testing it was not. Ods Reader basically ignores all styles; they are complicated, declared in (at least) 2 places (content.xml and styles.xml). This change deals only with the problem as reported, in which the missing declarations should be in content.xml. If someone reports a real-world example of this involving styles.xml, I can look at that then. In the meantime, this toehold might serve as a template for adding other style processing to Ods Reader. Looking at other formats, processing of column widths was also missing from Html Reader, and is now added. Note that this will work only with inline Css declarations on the `col` tags, which can be generated by Html Writer using `setUseInlineCss(true)`. This creates a much larger file than one created without inline CSS. A general problem became evident when studying the code. Worksheet `columnDimensions` is an unsorted array. This is not a problem per se, but it can easily lead to unexpected results from a `getColumnDimensions` call. The code is changed to sort the array before returning it in `getColumnDimensions`. One existing test failed as a result of this change. It errorneously tested `getHighestColumn` instead of `getHighestDataColumn`, which caused a problem because the final column declaration included a `number-columns-repeated` attribute. The new result for `getHighestColumn` is correct, and the test is changed to use `getHighestDataColumn` instead, which was certainly its intent.
- Loading branch information
Showing
6 changed files
with
145 additions
and
10 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
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
65 changes: 65 additions & 0 deletions
65
tests/PhpSpreadsheetTests/Worksheet/ColumnDimension2Test.php
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,65 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Worksheet; | ||
|
||
use PhpOffice\PhpSpreadsheet\Helper\Dimension; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Writer\Html; | ||
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; | ||
|
||
class ColumnDimension2Test extends AbstractFunctional | ||
{ | ||
/** | ||
* @dataProvider providerType | ||
*/ | ||
public function testSetsAndDefaults(string $type): void | ||
{ | ||
$columns = ['J', 'A', 'F', 'M', 'N', 'T', 'S']; | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$expectedCm = 0.45; | ||
foreach ($columns as $column) { | ||
$sheet->getColumnDimension($column)->setWidth($expectedCm, 'cm'); | ||
} | ||
if ($type === 'Html') { | ||
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $type, null, [self::class, 'inlineCss']); | ||
} else { | ||
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $type); | ||
} | ||
$spreadsheet->disconnectWorksheets(); | ||
$sheet = $reloadedSpreadsheet->getActiveSheet(); | ||
for ($column = 'A'; $column !== 'Z'; ++$column) { | ||
if (in_array($column, $columns, true)) { | ||
self::assertEqualsWithDelta($expectedCm, $sheet->getColumnDimension($column)->getWidth(Dimension::UOM_CENTIMETERS), 1E-3, "column $column"); | ||
} elseif ($type === 'Xls' && $column <= 'T') { | ||
// Xls is a bit weird. Columns through max used | ||
// actually set a width in the spreadsheet file. | ||
// Columns above max used obviously do not. | ||
self::assertEqualsWithDelta(9.140625, $sheet->getColumnDimension($column)->getWidth(), 1E-3, "column $column"); | ||
} elseif ($type === 'Html' && $column <= 'T') { | ||
// Html is a lot like Xls. Columns through max used | ||
// actually set a width in the spreadsheet file. | ||
// Columns above max used obviously do not. | ||
self::assertEqualsWithDelta(7.998, $sheet->getColumnDimension($column)->getWidth(), 1E-3, "column $column"); | ||
} else { | ||
self::assertSame(-1.0, $sheet->getColumnDimension($column)->getWidth(), "column $column"); | ||
} | ||
} | ||
$reloadedSpreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public static function providerType(): array | ||
{ | ||
return [ | ||
['Xlsx'], | ||
['Xls'], | ||
['Ods'], | ||
['Html'], | ||
]; | ||
} | ||
|
||
public static function inlineCss(Html $writer): void | ||
{ | ||
$writer->setUseInlineCss(true); | ||
} | ||
} |