Skip to content

Commit c04b921

Browse files
Julian KernPowerKiKi
authored andcommitted
Column dimensions are read by Reader\Xlsx
Fixes #596 Fixes #616
1 parent 98c55b0 commit c04b921

File tree

2 files changed

+132
-67
lines changed

2 files changed

+132
-67
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 89 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -889,73 +889,7 @@ public function load($pFilename)
889889
}
890890
}
891891

892-
$columnsAttributes = [];
893-
$rowsAttributes = [];
894-
if (isset($xmlSheet->cols) && !$this->readDataOnly) {
895-
foreach ($xmlSheet->cols->col as $col) {
896-
for ($i = (int) ($col['min']); $i <= (int) ($col['max']); ++$i) {
897-
if ($col['style'] && !$this->readDataOnly) {
898-
$columnsAttributes[Coordinate::stringFromColumnIndex($i)]['xfIndex'] = (int) $col['style'];
899-
}
900-
if (self::boolean($col['hidden'])) {
901-
$columnsAttributes[Coordinate::stringFromColumnIndex($i)]['visible'] = false;
902-
}
903-
if (self::boolean($col['collapsed'])) {
904-
$columnsAttributes[Coordinate::stringFromColumnIndex($i)]['collapsed'] = true;
905-
}
906-
if ($col['outlineLevel'] > 0) {
907-
$columnsAttributes[Coordinate::stringFromColumnIndex($i)]['outlineLevel'] = (int) $col['outlineLevel'];
908-
}
909-
$columnsAttributes[Coordinate::stringFromColumnIndex($i)]['width'] = (float) $col['width'];
910-
911-
if ((int) ($col['max']) == 16384) {
912-
break;
913-
}
914-
}
915-
}
916-
}
917-
918-
if ($xmlSheet && $xmlSheet->sheetData && $xmlSheet->sheetData->row) {
919-
foreach ($xmlSheet->sheetData->row as $row) {
920-
if ($row['ht'] && !$this->readDataOnly) {
921-
$rowsAttributes[(int) $row['r']]['rowHeight'] = (float) $row['ht'];
922-
}
923-
if (self::boolean($row['hidden']) && !$this->readDataOnly) {
924-
$rowsAttributes[(int) $row['r']]['visible'] = false;
925-
}
926-
if (self::boolean($row['collapsed'])) {
927-
$rowsAttributes[(int) $row['r']]['collapsed'] = true;
928-
}
929-
if ($row['outlineLevel'] > 0) {
930-
$rowsAttributes[(int) $row['r']]['outlineLevel'] = (int) $row['outlineLevel'];
931-
}
932-
if ($row['s'] && !$this->readDataOnly) {
933-
$rowsAttributes[(int) $row['r']]['xfIndex'] = (int) $row['s'];
934-
}
935-
}
936-
}
937-
938-
// set columns/rows attributes
939-
$columnsAttributesSet = [];
940-
$rowsAttributesSet = [];
941-
foreach ($columnsAttributes as $coordColumn => $columnAttributes) {
942-
foreach ($rowsAttributes as $coordRow => $rowAttributes) {
943-
if ($this->getReadFilter() !== null) {
944-
if (!$this->getReadFilter()->readCell($coordColumn, $coordRow, $docSheet->getTitle())) {
945-
continue;
946-
}
947-
}
948-
949-
if (!isset($columnsAttributesSet[$coordColumn])) {
950-
$this->setColumnAttributes($docSheet, $coordColumn, $columnAttributes);
951-
$columnsAttributesSet[$coordColumn] = true;
952-
}
953-
if (!isset($rowsAttributesSet[$coordRow])) {
954-
$this->setRowAttributes($docSheet, $coordRow, $rowAttributes);
955-
$rowsAttributesSet[$coordRow] = true;
956-
}
957-
}
958-
}
892+
$this->readColumnsAndRowsAttributes($xmlSheet, $docSheet);
959893

960894
if ($xmlSheet && $xmlSheet->sheetData && $xmlSheet->sheetData->row) {
961895
$cIndex = 1; // Cell Start from 1
@@ -2575,4 +2509,92 @@ private function castXsdBooleanToBool($xsdBoolean)
25752509

25762510
return (bool) $xsdBoolean;
25772511
}
2512+
2513+
/**
2514+
* Read columns and rows attributes from XML and set them on the worksheet.
2515+
*
2516+
* @param SimpleXMLElement $xmlSheet
2517+
* @param Worksheet $docSheet
2518+
*/
2519+
private function readColumnsAndRowsAttributes(SimpleXMLElement $xmlSheet, Worksheet $docSheet)
2520+
{
2521+
$columnsAttributes = [];
2522+
$rowsAttributes = [];
2523+
if (isset($xmlSheet->cols) && !$this->readDataOnly) {
2524+
foreach ($xmlSheet->cols->col as $col) {
2525+
for ($i = (int) ($col['min']); $i <= (int) ($col['max']); ++$i) {
2526+
if ($col['style'] && !$this->readDataOnly) {
2527+
$columnsAttributes[Coordinate::stringFromColumnIndex($i)]['xfIndex'] = (int) $col['style'];
2528+
}
2529+
if (self::boolean($col['hidden'])) {
2530+
$columnsAttributes[Coordinate::stringFromColumnIndex($i)]['visible'] = false;
2531+
}
2532+
if (self::boolean($col['collapsed'])) {
2533+
$columnsAttributes[Coordinate::stringFromColumnIndex($i)]['collapsed'] = true;
2534+
}
2535+
if ($col['outlineLevel'] > 0) {
2536+
$columnsAttributes[Coordinate::stringFromColumnIndex($i)]['outlineLevel'] = (int) $col['outlineLevel'];
2537+
}
2538+
$columnsAttributes[Coordinate::stringFromColumnIndex($i)]['width'] = (float) $col['width'];
2539+
2540+
if ((int) ($col['max']) == 16384) {
2541+
break;
2542+
}
2543+
}
2544+
}
2545+
}
2546+
2547+
if ($xmlSheet && $xmlSheet->sheetData && $xmlSheet->sheetData->row) {
2548+
foreach ($xmlSheet->sheetData->row as $row) {
2549+
if ($row['ht'] && !$this->readDataOnly) {
2550+
$rowsAttributes[(int) $row['r']]['rowHeight'] = (float) $row['ht'];
2551+
}
2552+
if (self::boolean($row['hidden']) && !$this->readDataOnly) {
2553+
$rowsAttributes[(int) $row['r']]['visible'] = false;
2554+
}
2555+
if (self::boolean($row['collapsed'])) {
2556+
$rowsAttributes[(int) $row['r']]['collapsed'] = true;
2557+
}
2558+
if ($row['outlineLevel'] > 0) {
2559+
$rowsAttributes[(int) $row['r']]['outlineLevel'] = (int) $row['outlineLevel'];
2560+
}
2561+
if ($row['s'] && !$this->readDataOnly) {
2562+
$rowsAttributes[(int) $row['r']]['xfIndex'] = (int) $row['s'];
2563+
}
2564+
}
2565+
}
2566+
2567+
// set columns/rows attributes
2568+
$columnsAttributesSet = [];
2569+
$rowsAttributesSet = [];
2570+
foreach ($columnsAttributes as $coordColumn => $columnAttributes) {
2571+
foreach ($rowsAttributes as $coordRow => $rowAttributes) {
2572+
if ($this->getReadFilter() !== null) {
2573+
if (!$this->getReadFilter()->readCell($coordColumn, $coordRow, $docSheet->getTitle())) {
2574+
continue 2;
2575+
}
2576+
}
2577+
}
2578+
2579+
if (!isset($columnsAttributesSet[$coordColumn])) {
2580+
$this->setColumnAttributes($docSheet, $coordColumn, $columnAttributes);
2581+
$columnsAttributesSet[$coordColumn] = true;
2582+
}
2583+
}
2584+
2585+
foreach ($rowsAttributes as $coordRow => $rowAttributes) {
2586+
foreach ($columnsAttributes as $coordColumn => $columnAttributes) {
2587+
if ($this->getReadFilter() !== null) {
2588+
if (!$this->getReadFilter()->readCell($coordColumn, $coordRow, $docSheet->getTitle())) {
2589+
continue 2;
2590+
}
2591+
}
2592+
}
2593+
2594+
if (!isset($rowsAttributesSet[$coordRow])) {
2595+
$this->setRowAttributes($docSheet, $coordRow, $rowAttributes);
2596+
$rowsAttributesSet[$coordRow] = true;
2597+
}
2598+
}
2599+
}
25782600
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Functional;
4+
5+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6+
7+
class ColumnWidthTest extends AbstractFunctional
8+
{
9+
public function providerFormats()
10+
{
11+
return [
12+
['Xlsx'],
13+
];
14+
}
15+
16+
/**
17+
* @dataProvider providerFormats
18+
*
19+
* @param $format
20+
*/
21+
public function testReadColumnWidth($format)
22+
{
23+
// create new sheet with column width
24+
$spreadsheet = new Spreadsheet();
25+
$sheet = $spreadsheet->getActiveSheet();
26+
$sheet->setCellValue('A1', 'Hello World !');
27+
$sheet->getColumnDimension('A')->setWidth(20);
28+
$this->assertColumn($spreadsheet);
29+
30+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
31+
$this->assertColumn($reloadedSpreadsheet);
32+
}
33+
34+
private function assertColumn(Spreadsheet $spreadsheet)
35+
{
36+
$sheet = $spreadsheet->getActiveSheet();
37+
$columnDimensions = $sheet->getColumnDimensions();
38+
39+
self::assertArrayHasKey('A', $columnDimensions);
40+
$column = array_shift($columnDimensions);
41+
self::assertEquals(20, $column->getWidth());
42+
}
43+
}

0 commit comments

Comments
 (0)