Fix/sheets xlsx chart (#1761)
* Add support for Google Sheets Exported XLSX Charts Google Sheets XLSX charts use oneCellAnchor positioning and the data series do not have the *Cache elements with cached values. * update CHANGELOG * Add support for Google Sheets Exported XLSX Charts Google Sheets XLSX charts use oneCellAnchor positioning and the data series do not have the *Cache elements with cached values. Because the reader had been assuming *Cache elements existed as children of strRef and numRef, errors about the node being deleted were thrown when reading Xlsx exported from Google Sheets. Co-authored-by: Darren Maczka <dkm@utk.edu>
This commit is contained in:
parent
304904d829
commit
44248cd04e
|
|
@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
- Resolve Google Sheets Xlsx charts issue. Google Sheets uses oneCellAnchor positioning and does not include *Cache values in the exported Xlsx.
|
||||||
- Fix for Xls Reader when SST has a bad length [#1592](https://github.com/PHPOffice/PhpSpreadsheet/issues/1592)
|
- Fix for Xls Reader when SST has a bad length [#1592](https://github.com/PHPOffice/PhpSpreadsheet/issues/1592)
|
||||||
- Resolve Xlsx loader issue whe hyperlinks don't have a destination
|
- Resolve Xlsx loader issue whe hyperlinks don't have a destination
|
||||||
- Resolve issues when printer settings resources IDs clash with drawing IDs
|
- Resolve issues when printer settings resources IDs clash with drawing IDs
|
||||||
|
|
|
||||||
|
|
@ -1155,13 +1155,27 @@ class Xlsx extends BaseReader
|
||||||
$this->readHyperLinkDrawing($objDrawing, $oneCellAnchor, $hyperlinks);
|
$this->readHyperLinkDrawing($objDrawing, $oneCellAnchor, $hyperlinks);
|
||||||
|
|
||||||
$objDrawing->setWorksheet($docSheet);
|
$objDrawing->setWorksheet($docSheet);
|
||||||
} else {
|
} elseif ($this->includeCharts && $oneCellAnchor->graphicFrame) {
|
||||||
// ? Can charts be positioned with a oneCellAnchor ?
|
// Exported XLSX from Google Sheets positions charts with a oneCellAnchor
|
||||||
$coordinates = Coordinate::stringFromColumnIndex(((string) $oneCellAnchor->from->col) + 1) . ($oneCellAnchor->from->row + 1);
|
$coordinates = Coordinate::stringFromColumnIndex(((string) $oneCellAnchor->from->col) + 1) . ($oneCellAnchor->from->row + 1);
|
||||||
$offsetX = Drawing::EMUToPixels($oneCellAnchor->from->colOff);
|
$offsetX = Drawing::EMUToPixels($oneCellAnchor->from->colOff);
|
||||||
$offsetY = Drawing::EMUToPixels($oneCellAnchor->from->rowOff);
|
$offsetY = Drawing::EMUToPixels($oneCellAnchor->from->rowOff);
|
||||||
$width = Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cx'));
|
$width = Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cx'));
|
||||||
$height = Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cy'));
|
$height = Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cy'));
|
||||||
|
|
||||||
|
$graphic = $oneCellAnchor->graphicFrame->children('http://schemas.openxmlformats.org/drawingml/2006/main')->graphic;
|
||||||
|
/** @var SimpleXMLElement $chartRef */
|
||||||
|
$chartRef = $graphic->graphicData->children('http://schemas.openxmlformats.org/drawingml/2006/chart')->chart;
|
||||||
|
$thisChart = (string) $chartRef->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships');
|
||||||
|
|
||||||
|
$chartDetails[$docSheet->getTitle() . '!' . $thisChart] = [
|
||||||
|
'fromCoordinate' => $coordinates,
|
||||||
|
'fromOffsetX' => $offsetX,
|
||||||
|
'fromOffsetY' => $offsetY,
|
||||||
|
'width' => $width,
|
||||||
|
'height' => $height,
|
||||||
|
'worksheetTitle' => $docSheet->getTitle(),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1508,10 +1522,13 @@ class Xlsx extends BaseReader
|
||||||
$excel->getSheetByName($charts[$chartEntryRef]['sheet'])->addChart($objChart);
|
$excel->getSheetByName($charts[$chartEntryRef]['sheet'])->addChart($objChart);
|
||||||
$objChart->setWorksheet($excel->getSheetByName($charts[$chartEntryRef]['sheet']));
|
$objChart->setWorksheet($excel->getSheetByName($charts[$chartEntryRef]['sheet']));
|
||||||
$objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']);
|
$objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']);
|
||||||
|
if (array_key_exists('toCoordinate', $chartDetails[$chartPositionRef])) {
|
||||||
|
// For oneCellAnchor positioned charts, toCoordinate is not in the data. Does it need to be calculated?
|
||||||
$objChart->setBottomRightPosition($chartDetails[$chartPositionRef]['toCoordinate'], $chartDetails[$chartPositionRef]['toOffsetX'], $chartDetails[$chartPositionRef]['toOffsetY']);
|
$objChart->setBottomRightPosition($chartDetails[$chartPositionRef]['toCoordinate'], $chartDetails[$chartPositionRef]['toOffsetX'], $chartDetails[$chartPositionRef]['toOffsetY']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -328,26 +328,51 @@ class Chart
|
||||||
{
|
{
|
||||||
if (isset($seriesDetail->strRef)) {
|
if (isset($seriesDetail->strRef)) {
|
||||||
$seriesSource = (string) $seriesDetail->strRef->f;
|
$seriesSource = (string) $seriesDetail->strRef->f;
|
||||||
$seriesData = self::chartDataSeriesValues($seriesDetail->strRef->strCache->children($namespacesChartMeta['c']), 's');
|
$seriesValues = new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $seriesSource, null, null, null, $marker);
|
||||||
|
|
||||||
return new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker);
|
if (isset($seriesDetail->strRef->strCache)) {
|
||||||
|
$seriesData = self::chartDataSeriesValues($seriesDetail->strRef->strCache->children($namespacesChartMeta['c']), 's');
|
||||||
|
$seriesValues
|
||||||
|
->setFormatCode($seriesData['formatCode'])
|
||||||
|
->setDataValues($seriesData['dataValues']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $seriesValues;
|
||||||
} elseif (isset($seriesDetail->numRef)) {
|
} elseif (isset($seriesDetail->numRef)) {
|
||||||
$seriesSource = (string) $seriesDetail->numRef->f;
|
$seriesSource = (string) $seriesDetail->numRef->f;
|
||||||
|
$seriesValues = new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, $seriesSource, null, null, null, $marker);
|
||||||
|
if (isset($seriesDetail->strRef->strCache)) {
|
||||||
$seriesData = self::chartDataSeriesValues($seriesDetail->numRef->numCache->children($namespacesChartMeta['c']));
|
$seriesData = self::chartDataSeriesValues($seriesDetail->numRef->numCache->children($namespacesChartMeta['c']));
|
||||||
|
$seriesValues
|
||||||
|
->setFormatCode($seriesData['formatCode'])
|
||||||
|
->setDataValues($seriesData['dataValues']);
|
||||||
|
}
|
||||||
|
|
||||||
return new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker);
|
return $seriesValues;
|
||||||
} elseif (isset($seriesDetail->multiLvlStrRef)) {
|
} elseif (isset($seriesDetail->multiLvlStrRef)) {
|
||||||
$seriesSource = (string) $seriesDetail->multiLvlStrRef->f;
|
$seriesSource = (string) $seriesDetail->multiLvlStrRef->f;
|
||||||
$seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlStrRef->multiLvlStrCache->children($namespacesChartMeta['c']), 's');
|
$seriesValues = new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $seriesSource, null, null, null, $marker);
|
||||||
$seriesData['pointCount'] = count($seriesData['dataValues']);
|
|
||||||
|
|
||||||
return new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker);
|
if (isset($seriesDetail->multiLvlStrRef->multiLvlStrCache)) {
|
||||||
|
$seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlStrRef->multiLvlStrCache->children($namespacesChartMeta['c']), 's');
|
||||||
|
$seriesValues
|
||||||
|
->setFormatCode($seriesData['formatCode'])
|
||||||
|
->setDataValues($seriesData['dataValues']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $seriesValues;
|
||||||
} elseif (isset($seriesDetail->multiLvlNumRef)) {
|
} elseif (isset($seriesDetail->multiLvlNumRef)) {
|
||||||
$seriesSource = (string) $seriesDetail->multiLvlNumRef->f;
|
$seriesSource = (string) $seriesDetail->multiLvlNumRef->f;
|
||||||
$seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlNumRef->multiLvlNumCache->children($namespacesChartMeta['c']), 's');
|
$seriesValues = new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $seriesSource, null, null, null, $marker);
|
||||||
$seriesData['pointCount'] = count($seriesData['dataValues']);
|
|
||||||
|
|
||||||
return new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $seriesSource, $seriesData['formatCode'], $seriesData['pointCount'], $seriesData['dataValues'], $marker);
|
if (isset($seriesDetail->multiLvlNumRef->multiLvlNumCache)) {
|
||||||
|
$seriesData = self::chartDataSeriesValuesMultiLevel($seriesDetail->multiLvlNumRef->multiLvlNumCache->children($namespacesChartMeta['c']), 's');
|
||||||
|
$seriesValues
|
||||||
|
->setFormatCode($seriesData['formatCode'])
|
||||||
|
->setDataValues($seriesData['dataValues']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $seriesValues;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpSpreadsheetTests\Reader;
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
|
||||||
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class SheetsXlsxChartTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testLoadSheetsXlsxChart(): void
|
||||||
|
{
|
||||||
|
$filename = 'tests/data/Reader/XLSX/sheetsChartsTest.xlsx';
|
||||||
|
$reader = IOFactory::createReader('Xlsx')->setIncludeCharts(true);
|
||||||
|
$spreadsheet = $reader->load($filename);
|
||||||
|
$worksheet = $spreadsheet->getActiveSheet();
|
||||||
|
|
||||||
|
$charts = $worksheet->getChartCollection();
|
||||||
|
self::assertEquals(2, $worksheet->getChartCount());
|
||||||
|
self::assertCount(2, $charts);
|
||||||
|
|
||||||
|
$chart1 = $charts[0];
|
||||||
|
$pa1 = $chart1->getPlotArea();
|
||||||
|
self::assertEquals(2, $pa1->getPlotSeriesCount());
|
||||||
|
|
||||||
|
$pg1 = $pa1->getPlotGroup()[0];
|
||||||
|
|
||||||
|
self::assertEquals(DataSeries::TYPE_LINECHART, $pg1->getPlotType());
|
||||||
|
self::assertCount(2, $pg1->getPlotLabels());
|
||||||
|
self::assertCount(2, $pg1->getPlotValues());
|
||||||
|
self::assertCount(2, $pg1->getPlotCategories());
|
||||||
|
|
||||||
|
$chart2 = $charts[1];
|
||||||
|
$pa1 = $chart2->getPlotArea();
|
||||||
|
self::assertEquals(2, $pa1->getPlotSeriesCount());
|
||||||
|
|
||||||
|
$pg1 = $pa1->getPlotGroupByIndex(0);
|
||||||
|
//Before a refresh, data values are empty
|
||||||
|
foreach ($pg1->getPlotValues() as $dv) {
|
||||||
|
self::assertEmpty($dv->getPointCount());
|
||||||
|
}
|
||||||
|
$pg1->refresh($worksheet);
|
||||||
|
foreach ($pg1->getPlotValues() as $dv) {
|
||||||
|
self::assertEquals(9, $dv->getPointCount());
|
||||||
|
}
|
||||||
|
self::assertEquals(DataSeries::TYPE_SCATTERCHART, $pg1->getPlotType());
|
||||||
|
self::assertCount(2, $pg1->getPlotLabels());
|
||||||
|
self::assertCount(2, $pg1->getPlotValues());
|
||||||
|
self::assertCount(2, $pg1->getPlotCategories());
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
Loading…
Reference in New Issue