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
This commit is contained in:
parent
092ddbd542
commit
34dd0a929e
|
|
@ -124,7 +124,7 @@ class Chart
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
private $bottomRightCellRef = 'A1';
|
||||
private $bottomRightCellRef = '';
|
||||
|
||||
/**
|
||||
* Bottom-Right X-Offset.
|
||||
|
|
@ -524,7 +524,7 @@ class Chart
|
|||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setBottomRightPosition($cell, $xOffset = null, $yOffset = null)
|
||||
public function setBottomRightPosition($cell = '', $xOffset = null, $yOffset = null)
|
||||
{
|
||||
$this->bottomRightCellRef = $cell;
|
||||
if ($xOffset !== null) {
|
||||
|
|
|
|||
|
|
@ -512,6 +512,12 @@ class Xlsx extends BaseReader
|
|||
case Namespaces::PURL_WORKSHEET:
|
||||
$worksheets[(string) $ele['Id']] = $ele['Target'];
|
||||
|
||||
break;
|
||||
case Namespaces::CHARTSHEET:
|
||||
if ($this->includeCharts === true) {
|
||||
$worksheets[(string) $ele['Id']] = $ele['Target'];
|
||||
}
|
||||
|
||||
break;
|
||||
// a vbaProject ? (: some macros)
|
||||
case Namespaces::VBA:
|
||||
|
|
@ -690,6 +696,13 @@ class Xlsx extends BaseReader
|
|||
continue;
|
||||
}
|
||||
|
||||
$sheetReferenceId = (string) self::getArrayItem(self::getAttributes($eleSheet, $xmlNamespaceBase), 'id');
|
||||
if (isset($worksheets[$sheetReferenceId]) === false) {
|
||||
++$countSkippedSheets;
|
||||
$mapSheetId[$oldSheetId] = null;
|
||||
|
||||
continue;
|
||||
}
|
||||
// Map old sheet id in original workbook to new sheet id.
|
||||
// They will differ if loadSheetsOnly() is being used
|
||||
$mapSheetId[$oldSheetId] = $oldSheetId - $countSkippedSheets;
|
||||
|
|
@ -701,7 +714,8 @@ class Xlsx extends BaseReader
|
|||
// and we're simply bringing the worksheet name in line with the formula, not the
|
||||
// reverse
|
||||
$docSheet->setTitle((string) $eleSheetAttr['name'], false, false);
|
||||
$fileWorksheet = (string) $worksheets[(string) self::getArrayItem(self::getAttributes($eleSheet, $xmlNamespaceBase), 'id')];
|
||||
|
||||
$fileWorksheet = (string) $worksheets[$sheetReferenceId];
|
||||
$xmlSheet = $this->loadZipNoNamespace("$dir/$fileWorksheet", $mainNS);
|
||||
$xmlSheetNS = $this->loadZip("$dir/$fileWorksheet", $mainNS);
|
||||
|
||||
|
|
@ -1195,6 +1209,7 @@ class Xlsx extends BaseReader
|
|||
$drawings[(string) $ele['Id']] = self::dirAdd("$dir/$fileWorksheet", $ele['Target']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($xmlSheet->drawing && !$this->readDataOnly) {
|
||||
$unparsedDrawings = [];
|
||||
$fileDrawing = null;
|
||||
|
|
@ -1203,6 +1218,7 @@ class Xlsx extends BaseReader
|
|||
$fileDrawing = $drawings[$drawingRelId];
|
||||
$drawingFilename = dirname($fileDrawing) . '/_rels/' . basename($fileDrawing) . '.rels';
|
||||
$relsDrawing = $this->loadZipNoNamespace($drawingFilename, $xmlNamespaceBase);
|
||||
|
||||
$images = [];
|
||||
$hyperlinks = [];
|
||||
if ($relsDrawing && $relsDrawing->Relationship) {
|
||||
|
|
@ -1223,6 +1239,7 @@ class Xlsx extends BaseReader
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
$xmlDrawing = $this->loadZipNoNamespace($fileDrawing, '');
|
||||
$xmlDrawingChildren = $xmlDrawing->children(Namespaces::SPREADSHEET_DRAWING);
|
||||
|
||||
|
|
@ -1401,6 +1418,27 @@ class Xlsx extends BaseReader
|
|||
}
|
||||
}
|
||||
}
|
||||
if ($xmlDrawingChildren->absoluteAnchor) {
|
||||
foreach ($xmlDrawingChildren->absoluteAnchor as $absoluteAnchor) {
|
||||
if (($this->includeCharts) && ($absoluteAnchor->graphicFrame)) {
|
||||
$graphic = $absoluteAnchor->graphicFrame->children(Namespaces::DRAWINGML)->graphic;
|
||||
/** @var SimpleXMLElement $chartRef */
|
||||
$chartRef = $graphic->graphicData->children(Namespaces::CHART)->chart;
|
||||
$thisChart = (string) self::getAttributes($chartRef, $xmlNamespaceBase);
|
||||
$width = Drawing::EMUToPixels((int) self::getArrayItem(self::getAttributes($absoluteAnchor->ext), 'cx')[0]);
|
||||
$height = Drawing::EMUToPixels((int) self::getArrayItem(self::getAttributes($absoluteAnchor->ext), 'cy')[0]);
|
||||
|
||||
$chartDetails[$docSheet->getTitle() . '!' . $thisChart] = [
|
||||
'fromCoordinate' => 'A1',
|
||||
'fromOffsetX' => 0,
|
||||
'fromOffsetY' => 0,
|
||||
'width' => $width,
|
||||
'height' => $height,
|
||||
'worksheetTitle' => $docSheet->getTitle(),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (empty($relsDrawing) && $xmlDrawing->count() == 0) {
|
||||
// Save Drawing without rels and children as unparsed
|
||||
$unparsedDrawings[$drawingRelId] = $xmlDrawing->asXML();
|
||||
|
|
@ -1600,16 +1638,21 @@ class Xlsx extends BaseReader
|
|||
$chartEntryRef = ltrim((string) $contentType['PartName'], '/');
|
||||
$chartElements = $this->loadZip($chartEntryRef);
|
||||
$objChart = Chart::readChart($chartElements, basename($chartEntryRef, '.xml'));
|
||||
|
||||
if (isset($charts[$chartEntryRef])) {
|
||||
$chartPositionRef = $charts[$chartEntryRef]['sheet'] . '!' . $charts[$chartEntryRef]['id'];
|
||||
if (isset($chartDetails[$chartPositionRef])) {
|
||||
$excel->getSheetByName($charts[$chartEntryRef]['sheet'])->addChart($objChart);
|
||||
$objChart->setWorksheet($excel->getSheetByName($charts[$chartEntryRef]['sheet']));
|
||||
$objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']);
|
||||
// For oneCellAnchor or absoluteAnchor positioned charts,
|
||||
// toCoordinate is not in the data. Does it need to be calculated?
|
||||
if (array_key_exists('toCoordinate', $chartDetails[$chartPositionRef])) {
|
||||
// For oneCellAnchor positioned charts, toCoordinate is not in the data. Does it need to be calculated?
|
||||
// twoCellAnchor
|
||||
$objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']);
|
||||
$objChart->setBottomRightPosition($chartDetails[$chartPositionRef]['toCoordinate'], $chartDetails[$chartPositionRef]['toOffsetX'], $chartDetails[$chartPositionRef]['toOffsetY']);
|
||||
} else {
|
||||
// oneCellAnchor or absoluteAnchor (e.g. Chart sheet)
|
||||
$objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']);
|
||||
$objChart->setBottomRightPosition('', $chartDetails[$chartPositionRef]['width'], $chartDetails[$chartPositionRef]['height']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,6 +50,8 @@ class Namespaces
|
|||
|
||||
const WORKSHEET = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet';
|
||||
|
||||
const CHARTSHEET = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet';
|
||||
|
||||
const SCHEMA_MICROSOFT = 'http://schemas.microsoft.com/office/2006/relationships';
|
||||
|
||||
const EXTENSIBILITY = 'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility';
|
||||
|
|
|
|||
|
|
@ -89,22 +89,36 @@ class Drawing extends WriterPart
|
|||
$tl = $chart->getTopLeftPosition();
|
||||
$tlColRow = Coordinate::indexesFromString($tl['cell']);
|
||||
$br = $chart->getBottomRightPosition();
|
||||
$brColRow = Coordinate::indexesFromString($br['cell']);
|
||||
|
||||
$objWriter->startElement('xdr:twoCellAnchor');
|
||||
$isTwoCellAnchor = $br['cell'] !== '';
|
||||
if ($isTwoCellAnchor) {
|
||||
$brColRow = Coordinate::indexesFromString($br['cell']);
|
||||
|
||||
$objWriter->startElement('xdr:from');
|
||||
$objWriter->writeElement('xdr:col', (string) ($tlColRow[0] - 1));
|
||||
$objWriter->writeElement('xdr:colOff', self::stringEmu($tl['xOffset']));
|
||||
$objWriter->writeElement('xdr:row', (string) ($tlColRow[1] - 1));
|
||||
$objWriter->writeElement('xdr:rowOff', self::stringEmu($tl['yOffset']));
|
||||
$objWriter->endElement();
|
||||
$objWriter->startElement('xdr:to');
|
||||
$objWriter->writeElement('xdr:col', (string) ($brColRow[0] - 1));
|
||||
$objWriter->writeElement('xdr:colOff', self::stringEmu($br['xOffset']));
|
||||
$objWriter->writeElement('xdr:row', (string) ($brColRow[1] - 1));
|
||||
$objWriter->writeElement('xdr:rowOff', self::stringEmu($br['yOffset']));
|
||||
$objWriter->endElement();
|
||||
$objWriter->startElement('xdr:twoCellAnchor');
|
||||
|
||||
$objWriter->startElement('xdr:from');
|
||||
$objWriter->writeElement('xdr:col', (string) ($tlColRow[0] - 1));
|
||||
$objWriter->writeElement('xdr:colOff', self::stringEmu($tl['xOffset']));
|
||||
$objWriter->writeElement('xdr:row', (string) ($tlColRow[1] - 1));
|
||||
$objWriter->writeElement('xdr:rowOff', self::stringEmu($tl['yOffset']));
|
||||
$objWriter->endElement();
|
||||
$objWriter->startElement('xdr:to');
|
||||
$objWriter->writeElement('xdr:col', (string) ($brColRow[0] - 1));
|
||||
$objWriter->writeElement('xdr:colOff', self::stringEmu($br['xOffset']));
|
||||
$objWriter->writeElement('xdr:row', (string) ($brColRow[1] - 1));
|
||||
$objWriter->writeElement('xdr:rowOff', self::stringEmu($br['yOffset']));
|
||||
$objWriter->endElement();
|
||||
} else {
|
||||
$objWriter->startElement('xdr:absoluteAnchor');
|
||||
$objWriter->startElement('xdr:pos');
|
||||
$objWriter->writeAttribute('x', '0');
|
||||
$objWriter->writeAttribute('y', '0');
|
||||
$objWriter->endElement();
|
||||
$objWriter->startElement('xdr:ext');
|
||||
$objWriter->writeAttribute('cx', self::stringEmu($br['xOffset']));
|
||||
$objWriter->writeAttribute('cy', self::stringEmu($br['yOffset']));
|
||||
$objWriter->endElement();
|
||||
}
|
||||
|
||||
$objWriter->startElement('xdr:graphicFrame');
|
||||
$objWriter->writeAttribute('macro', '');
|
||||
|
|
|
|||
Loading…
Reference in New Issue