Skip to content

Commit 34dd0a9

Browse files
author
MarkBaker
committed
Read ChartSheets with the Xlsx Reader if includeCharts is enabled; although we're reading it as a normal worksheet that simply contains a chart at cell A1, xOffset 0, yOffset 0, with no data
And Handle ChartSheets in the Xlsx Writer
1 parent 092ddbd commit 34dd0a9

File tree

4 files changed

+79
-20
lines changed

4 files changed

+79
-20
lines changed

src/PhpSpreadsheet/Chart/Chart.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class Chart
124124
*
125125
* @var string
126126
*/
127-
private $bottomRightCellRef = 'A1';
127+
private $bottomRightCellRef = '';
128128

129129
/**
130130
* Bottom-Right X-Offset.
@@ -524,7 +524,7 @@ public function getTopLeftYOffset()
524524
*
525525
* @return $this
526526
*/
527-
public function setBottomRightPosition($cell, $xOffset = null, $yOffset = null)
527+
public function setBottomRightPosition($cell = '', $xOffset = null, $yOffset = null)
528528
{
529529
$this->bottomRightCellRef = $cell;
530530
if ($xOffset !== null) {

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,12 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
512512
case Namespaces::PURL_WORKSHEET:
513513
$worksheets[(string) $ele['Id']] = $ele['Target'];
514514

515+
break;
516+
case Namespaces::CHARTSHEET:
517+
if ($this->includeCharts === true) {
518+
$worksheets[(string) $ele['Id']] = $ele['Target'];
519+
}
520+
515521
break;
516522
// a vbaProject ? (: some macros)
517523
case Namespaces::VBA:
@@ -690,6 +696,13 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
690696
continue;
691697
}
692698

699+
$sheetReferenceId = (string) self::getArrayItem(self::getAttributes($eleSheet, $xmlNamespaceBase), 'id');
700+
if (isset($worksheets[$sheetReferenceId]) === false) {
701+
++$countSkippedSheets;
702+
$mapSheetId[$oldSheetId] = null;
703+
704+
continue;
705+
}
693706
// Map old sheet id in original workbook to new sheet id.
694707
// They will differ if loadSheetsOnly() is being used
695708
$mapSheetId[$oldSheetId] = $oldSheetId - $countSkippedSheets;
@@ -701,7 +714,8 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
701714
// and we're simply bringing the worksheet name in line with the formula, not the
702715
// reverse
703716
$docSheet->setTitle((string) $eleSheetAttr['name'], false, false);
704-
$fileWorksheet = (string) $worksheets[(string) self::getArrayItem(self::getAttributes($eleSheet, $xmlNamespaceBase), 'id')];
717+
718+
$fileWorksheet = (string) $worksheets[$sheetReferenceId];
705719
$xmlSheet = $this->loadZipNoNamespace("$dir/$fileWorksheet", $mainNS);
706720
$xmlSheetNS = $this->loadZip("$dir/$fileWorksheet", $mainNS);
707721

@@ -1195,6 +1209,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
11951209
$drawings[(string) $ele['Id']] = self::dirAdd("$dir/$fileWorksheet", $ele['Target']);
11961210
}
11971211
}
1212+
11981213
if ($xmlSheet->drawing && !$this->readDataOnly) {
11991214
$unparsedDrawings = [];
12001215
$fileDrawing = null;
@@ -1203,6 +1218,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
12031218
$fileDrawing = $drawings[$drawingRelId];
12041219
$drawingFilename = dirname($fileDrawing) . '/_rels/' . basename($fileDrawing) . '.rels';
12051220
$relsDrawing = $this->loadZipNoNamespace($drawingFilename, $xmlNamespaceBase);
1221+
12061222
$images = [];
12071223
$hyperlinks = [];
12081224
if ($relsDrawing && $relsDrawing->Relationship) {
@@ -1223,6 +1239,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
12231239
}
12241240
}
12251241
}
1242+
12261243
$xmlDrawing = $this->loadZipNoNamespace($fileDrawing, '');
12271244
$xmlDrawingChildren = $xmlDrawing->children(Namespaces::SPREADSHEET_DRAWING);
12281245

@@ -1401,6 +1418,27 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
14011418
}
14021419
}
14031420
}
1421+
if ($xmlDrawingChildren->absoluteAnchor) {
1422+
foreach ($xmlDrawingChildren->absoluteAnchor as $absoluteAnchor) {
1423+
if (($this->includeCharts) && ($absoluteAnchor->graphicFrame)) {
1424+
$graphic = $absoluteAnchor->graphicFrame->children(Namespaces::DRAWINGML)->graphic;
1425+
/** @var SimpleXMLElement $chartRef */
1426+
$chartRef = $graphic->graphicData->children(Namespaces::CHART)->chart;
1427+
$thisChart = (string) self::getAttributes($chartRef, $xmlNamespaceBase);
1428+
$width = Drawing::EMUToPixels((int) self::getArrayItem(self::getAttributes($absoluteAnchor->ext), 'cx')[0]);
1429+
$height = Drawing::EMUToPixels((int) self::getArrayItem(self::getAttributes($absoluteAnchor->ext), 'cy')[0]);
1430+
1431+
$chartDetails[$docSheet->getTitle() . '!' . $thisChart] = [
1432+
'fromCoordinate' => 'A1',
1433+
'fromOffsetX' => 0,
1434+
'fromOffsetY' => 0,
1435+
'width' => $width,
1436+
'height' => $height,
1437+
'worksheetTitle' => $docSheet->getTitle(),
1438+
];
1439+
}
1440+
}
1441+
}
14041442
if (empty($relsDrawing) && $xmlDrawing->count() == 0) {
14051443
// Save Drawing without rels and children as unparsed
14061444
$unparsedDrawings[$drawingRelId] = $xmlDrawing->asXML();
@@ -1600,16 +1638,21 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
16001638
$chartEntryRef = ltrim((string) $contentType['PartName'], '/');
16011639
$chartElements = $this->loadZip($chartEntryRef);
16021640
$objChart = Chart::readChart($chartElements, basename($chartEntryRef, '.xml'));
1603-
16041641
if (isset($charts[$chartEntryRef])) {
16051642
$chartPositionRef = $charts[$chartEntryRef]['sheet'] . '!' . $charts[$chartEntryRef]['id'];
16061643
if (isset($chartDetails[$chartPositionRef])) {
16071644
$excel->getSheetByName($charts[$chartEntryRef]['sheet'])->addChart($objChart);
16081645
$objChart->setWorksheet($excel->getSheetByName($charts[$chartEntryRef]['sheet']));
1609-
$objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']);
1646+
// For oneCellAnchor or absoluteAnchor positioned charts,
1647+
// toCoordinate is not in the data. Does it need to be calculated?
16101648
if (array_key_exists('toCoordinate', $chartDetails[$chartPositionRef])) {
1611-
// For oneCellAnchor positioned charts, toCoordinate is not in the data. Does it need to be calculated?
1649+
// twoCellAnchor
1650+
$objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']);
16121651
$objChart->setBottomRightPosition($chartDetails[$chartPositionRef]['toCoordinate'], $chartDetails[$chartPositionRef]['toOffsetX'], $chartDetails[$chartPositionRef]['toOffsetY']);
1652+
} else {
1653+
// oneCellAnchor or absoluteAnchor (e.g. Chart sheet)
1654+
$objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']);
1655+
$objChart->setBottomRightPosition('', $chartDetails[$chartPositionRef]['width'], $chartDetails[$chartPositionRef]['height']);
16131656
}
16141657
}
16151658
}

src/PhpSpreadsheet/Reader/Xlsx/Namespaces.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class Namespaces
5050

5151
const WORKSHEET = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet';
5252

53+
const CHARTSHEET = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet';
54+
5355
const SCHEMA_MICROSOFT = 'http://schemas.microsoft.com/office/2006/relationships';
5456

5557
const EXTENSIBILITY = 'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility';

src/PhpSpreadsheet/Writer/Xlsx/Drawing.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,36 @@ public function writeChart(XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Chart
8989
$tl = $chart->getTopLeftPosition();
9090
$tlColRow = Coordinate::indexesFromString($tl['cell']);
9191
$br = $chart->getBottomRightPosition();
92-
$brColRow = Coordinate::indexesFromString($br['cell']);
9392

94-
$objWriter->startElement('xdr:twoCellAnchor');
93+
$isTwoCellAnchor = $br['cell'] !== '';
94+
if ($isTwoCellAnchor) {
95+
$brColRow = Coordinate::indexesFromString($br['cell']);
9596

96-
$objWriter->startElement('xdr:from');
97-
$objWriter->writeElement('xdr:col', (string) ($tlColRow[0] - 1));
98-
$objWriter->writeElement('xdr:colOff', self::stringEmu($tl['xOffset']));
99-
$objWriter->writeElement('xdr:row', (string) ($tlColRow[1] - 1));
100-
$objWriter->writeElement('xdr:rowOff', self::stringEmu($tl['yOffset']));
101-
$objWriter->endElement();
102-
$objWriter->startElement('xdr:to');
103-
$objWriter->writeElement('xdr:col', (string) ($brColRow[0] - 1));
104-
$objWriter->writeElement('xdr:colOff', self::stringEmu($br['xOffset']));
105-
$objWriter->writeElement('xdr:row', (string) ($brColRow[1] - 1));
106-
$objWriter->writeElement('xdr:rowOff', self::stringEmu($br['yOffset']));
107-
$objWriter->endElement();
97+
$objWriter->startElement('xdr:twoCellAnchor');
98+
99+
$objWriter->startElement('xdr:from');
100+
$objWriter->writeElement('xdr:col', (string) ($tlColRow[0] - 1));
101+
$objWriter->writeElement('xdr:colOff', self::stringEmu($tl['xOffset']));
102+
$objWriter->writeElement('xdr:row', (string) ($tlColRow[1] - 1));
103+
$objWriter->writeElement('xdr:rowOff', self::stringEmu($tl['yOffset']));
104+
$objWriter->endElement();
105+
$objWriter->startElement('xdr:to');
106+
$objWriter->writeElement('xdr:col', (string) ($brColRow[0] - 1));
107+
$objWriter->writeElement('xdr:colOff', self::stringEmu($br['xOffset']));
108+
$objWriter->writeElement('xdr:row', (string) ($brColRow[1] - 1));
109+
$objWriter->writeElement('xdr:rowOff', self::stringEmu($br['yOffset']));
110+
$objWriter->endElement();
111+
} else {
112+
$objWriter->startElement('xdr:absoluteAnchor');
113+
$objWriter->startElement('xdr:pos');
114+
$objWriter->writeAttribute('x', '0');
115+
$objWriter->writeAttribute('y', '0');
116+
$objWriter->endElement();
117+
$objWriter->startElement('xdr:ext');
118+
$objWriter->writeAttribute('cx', self::stringEmu($br['xOffset']));
119+
$objWriter->writeAttribute('cy', self::stringEmu($br['yOffset']));
120+
$objWriter->endElement();
121+
}
108122

109123
$objWriter->startElement('xdr:graphicFrame');
110124
$objWriter->writeAttribute('macro', '');

0 commit comments

Comments
 (0)