Skip to content

Xlsx Namespace Handling of Drawings, RowAndColumnAttributes, MergeCells #3137

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

Merged
merged 3 commits into from
Nov 1, 2022
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
13 changes: 13 additions & 0 deletions samples/Basic/27_Images_Xlsx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;

require __DIR__ . '/../Header.php';

// Read from Xlsx (.xls) template
$helper->log('Load Xlsx template file');
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load(__DIR__ . '/../templates/27template.xlsx');

// Save
$helper->write($spreadsheet, __FILE__);
Binary file added samples/templates/27template.xlsx
Binary file not shown.
27 changes: 19 additions & 8 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$sheetViewOptions = new SheetViewOptions($docSheet, $xmlSheet);
$sheetViewOptions->load($this->getReadDataOnly(), $this->styleReader);

(new ColumnAndRowAttributes($docSheet, $xmlSheet))
(new ColumnAndRowAttributes($docSheet, $xmlSheetNS))
->load($this->getReadFilter(), $this->getReadDataOnly());
}

Expand Down Expand Up @@ -913,11 +913,13 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$this->readTables($xmlSheet, $docSheet, $dir, $fileWorksheet, $zip);
}

if ($xmlSheet && $xmlSheet->mergeCells && $xmlSheet->mergeCells->mergeCell && !$this->readDataOnly) {
foreach ($xmlSheet->mergeCells->mergeCell as $mergeCell) {
$mergeRef = (string) $mergeCell['ref'];
if ($xmlSheetNS && $xmlSheetNS->mergeCells && $xmlSheetNS->mergeCells->mergeCell && !$this->readDataOnly) {
foreach ($xmlSheetNS->mergeCells->mergeCell as $mergeCellx) {
/** @scrutinizer ignore-call */
$mergeCell = $mergeCellx->attributes();
$mergeRef = (string) ($mergeCell['ref'] ?? '');
if (strpos($mergeRef, ':') !== false) {
$docSheet->mergeCells((string) $mergeCell['ref'], Worksheet::MERGE_CELL_CONTENT_HIDE);
$docSheet->mergeCells($mergeRef, Worksheet::MERGE_CELL_CONTENT_HIDE);
}
}
}
Expand Down Expand Up @@ -1232,6 +1234,9 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
if (substr($drawingFilename, 0, 7) === 'xl//xl/') {
$drawingFilename = substr($drawingFilename, 4);
}
if (substr($drawingFilename, 0, 8) === '/xl//xl/') {
$drawingFilename = substr($drawingFilename, 5);
}
if ($zip->locateName($drawingFilename)) {
$relsWorksheet = $this->loadZipNoNamespace($drawingFilename, Namespaces::RELATIONSHIPS);
$drawings = [];
Expand All @@ -1246,10 +1251,10 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
}
}

if ($xmlSheet->drawing && !$this->readDataOnly) {
if ($xmlSheetNS->drawing && !$this->readDataOnly) {
$unparsedDrawings = [];
$fileDrawing = null;
foreach ($xmlSheet->drawing as $drawing) {
foreach ($xmlSheetNS->drawing as $drawing) {
$drawingRelId = (string) self::getArrayItem(self::getAttributes($drawing, $xmlNamespaceBase), 'id');
$fileDrawing = $drawings[$drawingRelId];
$drawingFilename = dirname($fileDrawing) . '/_rels/' . basename($fileDrawing) . '.rels';
Expand All @@ -1264,7 +1269,13 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$hyperlinks[(string) $ele['Id']] = (string) $ele['Target'];
}
if ($eleType === "$xmlNamespaceBase/image") {
$images[(string) $ele['Id']] = self::dirAdd($fileDrawing, $ele['Target']);
$eleTarget = (string) $ele['Target'];
if (substr($eleTarget, 0, 4) === '/xl/') {
$eleTarget = substr($eleTarget, 1);
$images[(string) $ele['Id']] = $eleTarget;
} else {
$images[(string) $ele['Id']] = self::dirAdd($fileDrawing, $eleTarget);
}
} elseif ($eleType === "$xmlNamespaceBase/chart") {
if ($this->includeCharts) {
$eleTarget = (string) $ele['Target'];
Expand Down
87 changes: 49 additions & 38 deletions src/PhpSpreadsheet/Reader/Xlsx/ColumnAndRowAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,39 +137,46 @@ private function readColumnAttributes(SimpleXMLElement $worksheetCols, bool $rea
{
$columnAttributes = [];

foreach ($worksheetCols->col as $column) {
$startColumn = Coordinate::stringFromColumnIndex((int) $column['min']);
$endColumn = Coordinate::stringFromColumnIndex((int) $column['max']);
++$endColumn;
for ($columnAddress = $startColumn; $columnAddress !== $endColumn; ++$columnAddress) {
$columnAttributes[$columnAddress] = $this->readColumnRangeAttributes($column, $readDataOnly);

if ((int) ($column['max']) == 16384) {
break;
foreach ($worksheetCols->col as $columnx) {
/** @scrutinizer ignore-call */
$column = $columnx->attributes();
if ($column !== null) {
$startColumn = Coordinate::stringFromColumnIndex((int) $column['min']);
$endColumn = Coordinate::stringFromColumnIndex((int) $column['max']);
++$endColumn;
for ($columnAddress = $startColumn; $columnAddress !== $endColumn; ++$columnAddress) {
$columnAttributes[$columnAddress] = $this->readColumnRangeAttributes($column, $readDataOnly);

if ((int) ($column['max']) == 16384) {
break;
}
}
}
}

return $columnAttributes;
}

private function readColumnRangeAttributes(SimpleXMLElement $column, bool $readDataOnly): array
private function readColumnRangeAttributes(?SimpleXMLElement $column, bool $readDataOnly): array
{
$columnAttributes = [];

if ($column['style'] && !$readDataOnly) {
$columnAttributes['xfIndex'] = (int) $column['style'];
}
if (self::boolean($column['hidden'])) {
$columnAttributes['visible'] = false;
}
if (self::boolean($column['collapsed'])) {
$columnAttributes['collapsed'] = true;
}
if (((int) $column['outlineLevel']) > 0) {
$columnAttributes['outlineLevel'] = (int) $column['outlineLevel'];
if ($column !== null) {
if (isset($column['style']) && !$readDataOnly) {
$columnAttributes['xfIndex'] = (int) $column['style'];
}
if (isset($column['hidden']) && self::boolean($column['hidden'])) {
$columnAttributes['visible'] = false;
}
if (isset($column['collapsed']) && self::boolean($column['collapsed'])) {
$columnAttributes['collapsed'] = true;
}
if (isset($column['outlineLevel']) && ((int) $column['outlineLevel']) > 0) {
$columnAttributes['outlineLevel'] = (int) $column['outlineLevel'];
}
if (isset($column['width'])) {
$columnAttributes['width'] = (float) $column['width'];
}
}
$columnAttributes['width'] = (float) $column['width'];

return $columnAttributes;
}
Expand All @@ -189,21 +196,25 @@ private function readRowAttributes(SimpleXMLElement $worksheetRow, bool $readDat
{
$rowAttributes = [];

foreach ($worksheetRow as $row) {
if ($row['ht'] && !$readDataOnly) {
$rowAttributes[(int) $row['r']]['rowHeight'] = (float) $row['ht'];
}
if (self::boolean($row['hidden'])) {
$rowAttributes[(int) $row['r']]['visible'] = false;
}
if (self::boolean($row['collapsed'])) {
$rowAttributes[(int) $row['r']]['collapsed'] = true;
}
if ((int) $row['outlineLevel'] > 0) {
$rowAttributes[(int) $row['r']]['outlineLevel'] = (int) $row['outlineLevel'];
}
if ($row['s'] && !$readDataOnly) {
$rowAttributes[(int) $row['r']]['xfIndex'] = (int) $row['s'];
foreach ($worksheetRow as $rowx) {
/** @scrutinizer ignore-call */
$row = $rowx->attributes();
if ($row !== null) {
if (isset($row['ht']) && !$readDataOnly) {
$rowAttributes[(int) $row['r']]['rowHeight'] = (float) $row['ht'];
}
if (isset($row['hidden']) && self::boolean($row['hidden'])) {
$rowAttributes[(int) $row['r']]['visible'] = false;
}
if (isset($row['collapsed']) && self::boolean($row['collapsed'])) {
$rowAttributes[(int) $row['r']]['collapsed'] = true;
}
if (isset($row['outlineLevel']) && (int) $row['outlineLevel'] > 0) {
$rowAttributes[(int) $row['r']]['outlineLevel'] = (int) $row['outlineLevel'];
}
if (isset($row['s']) && !$readDataOnly) {
$rowAttributes[(int) $row['r']]['xfIndex'] = (int) $row['s'];
}
}
}

Expand Down
163 changes: 163 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue1482Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;

class Issue1482Test extends \PHPUnit\Framework\TestCase
{
/**
* @var string
*/
private static $testbook = 'tests/data/Reader/XLSX/issue.1482.xlsx';

public function testPreliminaries(): void
{
$file = 'zip://';
$file .= self::$testbook;
$file .= '#xl/worksheets/sheet.xml';
$data = file_get_contents($file);
// confirm that file contains expected namespaced xml tag
if ($data === false) {
self::fail('Unable to read file');
} else {
self::assertStringContainsString('<x:drawing r:id="rId1" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" />', $data);
}
}

public function testLoadXlsx(): void
{
$reader = new Xlsx();
$spreadsheet = $reader->load(self::$testbook);
$sheet = $spreadsheet->getSheet(0);
$expectedColWidths = [
0.95,
7.45,
0.6,
4.4,
5.91,
3.92,
7.71,
2.12,
2.56,
6.94,
9.6,
1.82,
3.01,
8.63,
10.37,
2.09,
];
$columnDimensions = $sheet->getColumnDimensions();
$actualColWidths = [];
foreach ($columnDimensions as $columnDimension) {
$actualColWidths[] = $columnDimension->getWidth();
}
self::assertSame($expectedColWidths, $actualColWidths);
$expectedRowHeights = [
14.0,
7.0,
14.0,
80.0,
234.0,
63.0,
13.0,
19.0,
18.0,
18.0,
18.0,
18.0,
18.0,
18.0,
18.0,
18.0,
];
$rowHeights = $sheet->getRowDimensions();
$actualRowHeights = [];
foreach ($rowHeights as $rowDimension) {
$actualRowHeights[] = $rowDimension->getRowHeight();
}
self::assertSame($expectedRowHeights, $actualRowHeights);
$expectedMergeCells = [
'B1:E1',
'F1:G1',
'J1:L1',
'M1:N1',
'B3:E3',
'F3:G3',
'B5:O5',
'A7:C7',
'D7:M7',
'A8:B8',
'C8:D8',
'E8:F8',
'G8:H8',
'I8:J8',
'L8:P8',
'A9:B9',
'C9:D9',
'E9:F9',
'G9:H9',
'I9:J9',
'L9:P9',
'A10:B10',
'C10:D10',
'E10:F10',
'G10:H10',
'I10:J10',
'L10:P10',
'A11:B11',
'C11:D11',
'E11:F11',
'G11:H11',
'I11:J11',
'L11:P11',
'A12:B12',
'C12:D12',
'E12:F12',
'G12:H12',
'I12:J12',
'L12:P12',
'A13:B13',
'C13:D13',
'E13:F13',
'G13:H13',
'I13:J13',
'L13:P13',
'A14:B14',
'C14:D14',
'E14:F14',
'G14:H14',
'I14:J14',
'L14:P14',
'A15:B15',
'C15:D15',
'E15:F15',
'G15:H15',
'I15:J15',
'L15:P15',
'A16:B16',
'C16:D16',
'E16:F16',
'G16:H16',
'I16:J16',
'L16:P16',
];
self::assertSame($expectedMergeCells, array_keys($sheet->getMergeCells()));

$drawingCollection = $sheet->getDrawingCollection();
self::assertCount(1, $drawingCollection);
$drawing = $drawingCollection[0];
if ($drawing === null) {
self::fail('Unexpected null instead of drawing');
} else {
self::assertSame('B5', $drawing->getCoordinates());
self::assertSame('', $drawing->getCoordinates2());
self::assertFalse($drawing->getResizeProportional());
self::assertSame(600, $drawing->getWidth());
self::assertSame(312, $drawing->getHeight());
}
// no name, no description, no type
$spreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/XLSX/issue.1482.xlsx
Binary file not shown.