@@ -512,6 +512,12 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
512
512
case Namespaces::PURL_WORKSHEET :
513
513
$ worksheets [(string ) $ ele ['Id ' ]] = $ ele ['Target ' ];
514
514
515
+ break ;
516
+ case Namespaces::CHARTSHEET :
517
+ if ($ this ->includeCharts === true ) {
518
+ $ worksheets [(string ) $ ele ['Id ' ]] = $ ele ['Target ' ];
519
+ }
520
+
515
521
break ;
516
522
// a vbaProject ? (: some macros)
517
523
case Namespaces::VBA :
@@ -690,6 +696,13 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
690
696
continue ;
691
697
}
692
698
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
+ }
693
706
// Map old sheet id in original workbook to new sheet id.
694
707
// They will differ if loadSheetsOnly() is being used
695
708
$ mapSheetId [$ oldSheetId ] = $ oldSheetId - $ countSkippedSheets ;
@@ -701,7 +714,8 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
701
714
// and we're simply bringing the worksheet name in line with the formula, not the
702
715
// reverse
703
716
$ 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 ];
705
719
$ xmlSheet = $ this ->loadZipNoNamespace ("$ dir/ $ fileWorksheet " , $ mainNS );
706
720
$ xmlSheetNS = $ this ->loadZip ("$ dir/ $ fileWorksheet " , $ mainNS );
707
721
@@ -1195,6 +1209,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
1195
1209
$ drawings [(string ) $ ele ['Id ' ]] = self ::dirAdd ("$ dir/ $ fileWorksheet " , $ ele ['Target ' ]);
1196
1210
}
1197
1211
}
1212
+
1198
1213
if ($ xmlSheet ->drawing && !$ this ->readDataOnly ) {
1199
1214
$ unparsedDrawings = [];
1200
1215
$ fileDrawing = null ;
@@ -1203,6 +1218,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
1203
1218
$ fileDrawing = $ drawings [$ drawingRelId ];
1204
1219
$ drawingFilename = dirname ($ fileDrawing ) . '/_rels/ ' . basename ($ fileDrawing ) . '.rels ' ;
1205
1220
$ relsDrawing = $ this ->loadZipNoNamespace ($ drawingFilename , $ xmlNamespaceBase );
1221
+
1206
1222
$ images = [];
1207
1223
$ hyperlinks = [];
1208
1224
if ($ relsDrawing && $ relsDrawing ->Relationship ) {
@@ -1223,6 +1239,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
1223
1239
}
1224
1240
}
1225
1241
}
1242
+
1226
1243
$ xmlDrawing = $ this ->loadZipNoNamespace ($ fileDrawing , '' );
1227
1244
$ xmlDrawingChildren = $ xmlDrawing ->children (Namespaces::SPREADSHEET_DRAWING );
1228
1245
@@ -1401,6 +1418,27 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
1401
1418
}
1402
1419
}
1403
1420
}
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
+ }
1404
1442
if (empty ($ relsDrawing ) && $ xmlDrawing ->count () == 0 ) {
1405
1443
// Save Drawing without rels and children as unparsed
1406
1444
$ unparsedDrawings [$ drawingRelId ] = $ xmlDrawing ->asXML ();
@@ -1600,16 +1638,21 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
1600
1638
$ chartEntryRef = ltrim ((string ) $ contentType ['PartName ' ], '/ ' );
1601
1639
$ chartElements = $ this ->loadZip ($ chartEntryRef );
1602
1640
$ objChart = Chart::readChart ($ chartElements , basename ($ chartEntryRef , '.xml ' ));
1603
-
1604
1641
if (isset ($ charts [$ chartEntryRef ])) {
1605
1642
$ chartPositionRef = $ charts [$ chartEntryRef ]['sheet ' ] . '! ' . $ charts [$ chartEntryRef ]['id ' ];
1606
1643
if (isset ($ chartDetails [$ chartPositionRef ])) {
1607
1644
$ excel ->getSheetByName ($ charts [$ chartEntryRef ]['sheet ' ])->addChart ($ objChart );
1608
1645
$ 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?
1610
1648
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 ' ]);
1612
1651
$ 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 ' ]);
1613
1656
}
1614
1657
}
1615
1658
}
0 commit comments