Conditions | 381 |
Paths | > 20000 |
Total Lines | 1538 |
Code Lines | 967 |
Lines | 140 |
Ratio | 9.1 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
362 | public function load($pFilename) |
||
363 | { |
||
364 | // Check if file exists |
||
365 | if (!file_exists($pFilename)) { |
||
366 | throw new Exception("Could not open " . $pFilename . " for reading! File does not exist."); |
||
367 | } |
||
368 | |||
369 | // Initialisations |
||
370 | $excel = new \PhpSpreadsheet\Spreadsheet; |
||
371 | $excel->removeSheetByIndex(0); |
||
372 | if (!$this->readDataOnly) { |
||
373 | $excel->removeCellStyleXfByIndex(0); // remove the default style |
||
374 | $excel->removeCellXfByIndex(0); // remove the default style |
||
375 | } |
||
376 | |||
377 | $zipClass = \PhpSpreadsheet\Settings::getZipClass(); |
||
378 | |||
379 | $zip = new $zipClass; |
||
380 | $zip->open($pFilename); |
||
381 | |||
382 | // Read the theme first, because we need the colour scheme when reading the styles |
||
383 | $wbRels = simplexml_load_string( |
||
384 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
385 | $this->securityScan($this->getFromZipArchive($zip, "xl/_rels/workbook.xml.rels")), |
||
386 | 'SimpleXMLElement', |
||
387 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
388 | ); |
||
389 | foreach ($wbRels->Relationship as $rel) { |
||
390 | switch ($rel["Type"]) { |
||
391 | case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme": |
||
392 | $themeOrderArray = array('lt1', 'dk1', 'lt2', 'dk2'); |
||
393 | $themeOrderAdditional = count($themeOrderArray); |
||
394 | |||
395 | $xmlTheme = simplexml_load_string( |
||
396 | $this->securityScan($this->getFromZipArchive($zip, "xl/{$rel['Target']}")), |
||
397 | 'SimpleXMLElement', |
||
398 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
399 | ); |
||
400 | if (is_object($xmlTheme)) { |
||
401 | $xmlThemeName = $xmlTheme->attributes(); |
||
402 | $xmlTheme = $xmlTheme->children("http://schemas.openxmlformats.org/drawingml/2006/main"); |
||
403 | $themeName = (string)$xmlThemeName['name']; |
||
404 | |||
405 | $colourScheme = $xmlTheme->themeElements->clrScheme->attributes(); |
||
406 | $colourSchemeName = (string)$colourScheme['name']; |
||
407 | $colourScheme = $xmlTheme->themeElements->clrScheme->children("http://schemas.openxmlformats.org/drawingml/2006/main"); |
||
408 | |||
409 | $themeColours = array(); |
||
410 | foreach ($colourScheme as $k => $xmlColour) { |
||
411 | $themePos = array_search($k, $themeOrderArray); |
||
412 | if ($themePos === false) { |
||
413 | $themePos = $themeOrderAdditional++; |
||
414 | } |
||
415 | if (isset($xmlColour->sysClr)) { |
||
416 | $xmlColourData = $xmlColour->sysClr->attributes(); |
||
417 | $themeColours[$themePos] = $xmlColourData['lastClr']; |
||
418 | } elseif (isset($xmlColour->srgbClr)) { |
||
419 | $xmlColourData = $xmlColour->srgbClr->attributes(); |
||
420 | $themeColours[$themePos] = $xmlColourData['val']; |
||
421 | } |
||
422 | } |
||
423 | self::$theme = new Excel2007\Theme($themeName, $colourSchemeName, $themeColours); |
||
424 | } |
||
425 | break; |
||
426 | } |
||
427 | } |
||
428 | |||
429 | $rels = simplexml_load_string( |
||
430 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
431 | $this->securityScan($this->getFromZipArchive($zip, "_rels/.rels")), |
||
432 | 'SimpleXMLElement', |
||
433 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
434 | ); |
||
435 | foreach ($rels->Relationship as $rel) { |
||
436 | switch ($rel["Type"]) { |
||
437 | case "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties": |
||
438 | $xmlCore = simplexml_load_string( |
||
439 | $this->securityScan($this->getFromZipArchive($zip, "{$rel['Target']}")), |
||
440 | 'SimpleXMLElement', |
||
441 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
442 | ); |
||
443 | if (is_object($xmlCore)) { |
||
444 | $xmlCore->registerXPathNamespace("dc", "http://purl.org/dc/elements/1.1/"); |
||
445 | $xmlCore->registerXPathNamespace("dcterms", "http://purl.org/dc/terms/"); |
||
446 | $xmlCore->registerXPathNamespace("cp", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties"); |
||
447 | $docProps = $excel->getProperties(); |
||
448 | $docProps->setCreator((string) self::getArrayItem($xmlCore->xpath("dc:creator"))); |
||
449 | $docProps->setLastModifiedBy((string) self::getArrayItem($xmlCore->xpath("cp:lastModifiedBy"))); |
||
450 | $docProps->setCreated(strtotime(self::getArrayItem($xmlCore->xpath("dcterms:created")))); //! respect xsi:type |
||
451 | $docProps->setModified(strtotime(self::getArrayItem($xmlCore->xpath("dcterms:modified")))); //! respect xsi:type |
||
452 | $docProps->setTitle((string) self::getArrayItem($xmlCore->xpath("dc:title"))); |
||
453 | $docProps->setDescription((string) self::getArrayItem($xmlCore->xpath("dc:description"))); |
||
454 | $docProps->setSubject((string) self::getArrayItem($xmlCore->xpath("dc:subject"))); |
||
455 | $docProps->setKeywords((string) self::getArrayItem($xmlCore->xpath("cp:keywords"))); |
||
456 | $docProps->setCategory((string) self::getArrayItem($xmlCore->xpath("cp:category"))); |
||
457 | } |
||
458 | break; |
||
459 | case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties": |
||
460 | $xmlCore = simplexml_load_string( |
||
461 | $this->securityScan($this->getFromZipArchive($zip, "{$rel['Target']}")), |
||
462 | 'SimpleXMLElement', |
||
463 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
464 | ); |
||
465 | if (is_object($xmlCore)) { |
||
466 | $docProps = $excel->getProperties(); |
||
467 | if (isset($xmlCore->Company)) { |
||
468 | $docProps->setCompany((string) $xmlCore->Company); |
||
469 | } |
||
470 | if (isset($xmlCore->Manager)) { |
||
471 | $docProps->setManager((string) $xmlCore->Manager); |
||
472 | } |
||
473 | } |
||
474 | break; |
||
475 | case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties": |
||
476 | $xmlCore = simplexml_load_string( |
||
477 | $this->securityScan($this->getFromZipArchive($zip, "{$rel['Target']}")), |
||
478 | 'SimpleXMLElement', |
||
479 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
480 | ); |
||
481 | if (is_object($xmlCore)) { |
||
482 | $docProps = $excel->getProperties(); |
||
483 | foreach ($xmlCore as $xmlProperty) { |
||
484 | $cellDataOfficeAttributes = $xmlProperty->attributes(); |
||
485 | if (isset($cellDataOfficeAttributes['name'])) { |
||
486 | $propertyName = (string) $cellDataOfficeAttributes['name']; |
||
487 | $cellDataOfficeChildren = $xmlProperty->children('http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes'); |
||
488 | $attributeType = $cellDataOfficeChildren->getName(); |
||
489 | $attributeValue = (string) $cellDataOfficeChildren->{$attributeType}; |
||
490 | $attributeValue = \PhpSpreadsheet\Document\Properties::convertProperty($attributeValue, $attributeType); |
||
491 | $attributeType = \PhpSpreadsheet\Document\Properties::convertPropertyType($attributeType); |
||
492 | $docProps->setCustomProperty($propertyName, $attributeValue, $attributeType); |
||
493 | } |
||
494 | } |
||
495 | } |
||
496 | break; |
||
497 | //Ribbon |
||
498 | case "http://schemas.microsoft.com/office/2006/relationships/ui/extensibility": |
||
499 | $customUI = $rel['Target']; |
||
500 | if (!is_null($customUI)) { |
||
501 | $this->readRibbon($excel, $customUI, $zip); |
||
502 | } |
||
503 | break; |
||
504 | case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument": |
||
505 | $dir = dirname($rel["Target"]); |
||
506 | $relsWorkbook = simplexml_load_string( |
||
507 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
508 | $this->securityScan($this->getFromZipArchive($zip, "$dir/_rels/" . basename($rel["Target"]) . ".rels")), |
||
509 | 'SimpleXMLElement', |
||
510 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
511 | ); |
||
512 | $relsWorkbook->registerXPathNamespace("rel", "http://schemas.openxmlformats.org/package/2006/relationships"); |
||
513 | |||
514 | $sharedStrings = array(); |
||
515 | $xpath = self::getArrayItem($relsWorkbook->xpath("rel:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings']")); |
||
516 | $xmlStrings = simplexml_load_string( |
||
517 | //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main" |
||
518 | $this->securityScan($this->getFromZipArchive($zip, "$dir/$xpath[Target]")), |
||
519 | 'SimpleXMLElement', |
||
520 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
521 | ); |
||
522 | if (isset($xmlStrings) && isset($xmlStrings->si)) { |
||
523 | foreach ($xmlStrings->si as $val) { |
||
524 | if (isset($val->t)) { |
||
525 | $sharedStrings[] = \PhpSpreadsheet\Shared\StringHelper::controlCharacterOOXML2PHP((string) $val->t); |
||
526 | } elseif (isset($val->r)) { |
||
527 | $sharedStrings[] = $this->parseRichText($val); |
||
528 | } |
||
529 | } |
||
530 | } |
||
531 | |||
532 | $worksheets = array(); |
||
533 | $macros = $customUI = null; |
||
534 | foreach ($relsWorkbook->Relationship as $ele) { |
||
535 | switch ($ele['Type']) { |
||
536 | case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet": |
||
537 | $worksheets[(string) $ele["Id"]] = $ele["Target"]; |
||
538 | break; |
||
539 | // a vbaProject ? (: some macros) |
||
540 | case "http://schemas.microsoft.com/office/2006/relationships/vbaProject": |
||
541 | $macros = $ele["Target"]; |
||
542 | break; |
||
543 | } |
||
544 | } |
||
545 | |||
546 | if (!is_null($macros)) { |
||
547 | $macrosCode = $this->getFromZipArchive($zip, 'xl/vbaProject.bin');//vbaProject.bin always in 'xl' dir and always named vbaProject.bin |
||
548 | if ($macrosCode !== false) { |
||
549 | $excel->setMacrosCode($macrosCode); |
||
550 | $excel->setHasMacros(true); |
||
551 | //short-circuit : not reading vbaProject.bin.rel to get Signature =>allways vbaProjectSignature.bin in 'xl' dir |
||
552 | $Certificate = $this->getFromZipArchive($zip, 'xl/vbaProjectSignature.bin'); |
||
553 | if ($Certificate !== false) { |
||
554 | $excel->setMacrosCertificate($Certificate); |
||
555 | } |
||
556 | } |
||
557 | } |
||
558 | $styles = array(); |
||
559 | $cellStyles = array(); |
||
560 | $xpath = self::getArrayItem($relsWorkbook->xpath("rel:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles']")); |
||
561 | $xmlStyles = simplexml_load_string( |
||
562 | //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main" |
||
563 | $this->securityScan($this->getFromZipArchive($zip, "$dir/$xpath[Target]")), |
||
564 | 'SimpleXMLElement', |
||
565 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
566 | ); |
||
567 | $numFmts = null; |
||
568 | if ($xmlStyles && $xmlStyles->numFmts[0]) { |
||
569 | $numFmts = $xmlStyles->numFmts[0]; |
||
570 | } |
||
571 | if (isset($numFmts) && ($numFmts !== null)) { |
||
572 | $numFmts->registerXPathNamespace("sml", "http://schemas.openxmlformats.org/spreadsheetml/2006/main"); |
||
573 | } |
||
574 | if (!$this->readDataOnly && $xmlStyles) { |
||
575 | foreach ($xmlStyles->cellXfs->xf as $xf) { |
||
576 | $numFmt = \PhpSpreadsheet\Style\NumberFormat::FORMAT_GENERAL; |
||
577 | |||
578 | if ($xf["numFmtId"]) { |
||
579 | if (isset($numFmts)) { |
||
580 | $tmpNumFmt = self::getArrayItem($numFmts->xpath("sml:numFmt[@numFmtId=$xf[numFmtId]]")); |
||
581 | |||
582 | if (isset($tmpNumFmt["formatCode"])) { |
||
583 | $numFmt = (string) $tmpNumFmt["formatCode"]; |
||
584 | } |
||
585 | } |
||
586 | |||
587 | // We shouldn't override any of the built-in MS Excel values (values below id 164) |
||
588 | // But there's a lot of naughty homebrew xlsx writers that do use "reserved" id values that aren't actually used |
||
589 | // So we make allowance for them rather than lose formatting masks |
||
590 | if ((int)$xf["numFmtId"] < 164 && |
||
591 | \PhpSpreadsheet\Style\NumberFormat::builtInFormatCode((int)$xf["numFmtId"]) !== '') { |
||
592 | $numFmt = \PhpSpreadsheet\Style\NumberFormat::builtInFormatCode((int)$xf["numFmtId"]); |
||
593 | } |
||
594 | } |
||
595 | $quotePrefix = false; |
||
596 | if (isset($xf["quotePrefix"])) { |
||
597 | $quotePrefix = (boolean) $xf["quotePrefix"]; |
||
598 | } |
||
599 | |||
600 | $style = (object) array( |
||
601 | "numFmt" => $numFmt, |
||
602 | "font" => $xmlStyles->fonts->font[intval($xf["fontId"])], |
||
603 | "fill" => $xmlStyles->fills->fill[intval($xf["fillId"])], |
||
604 | "border" => $xmlStyles->borders->border[intval($xf["borderId"])], |
||
605 | "alignment" => $xf->alignment, |
||
606 | "protection" => $xf->protection, |
||
607 | "quotePrefix" => $quotePrefix, |
||
608 | ); |
||
609 | $styles[] = $style; |
||
610 | |||
611 | // add style to cellXf collection |
||
612 | $objStyle = new \PhpSpreadsheet\Style; |
||
613 | self::readStyle($objStyle, $style); |
||
614 | $excel->addCellXf($objStyle); |
||
615 | } |
||
616 | |||
617 | foreach ($xmlStyles->cellStyleXfs->xf as $xf) { |
||
618 | $numFmt = \PhpSpreadsheet\Style\NumberFormat::FORMAT_GENERAL; |
||
619 | if ($numFmts && $xf["numFmtId"]) { |
||
620 | $tmpNumFmt = self::getArrayItem($numFmts->xpath("sml:numFmt[@numFmtId=$xf[numFmtId]]")); |
||
621 | if (isset($tmpNumFmt["formatCode"])) { |
||
622 | $numFmt = (string) $tmpNumFmt["formatCode"]; |
||
623 | } elseif ((int)$xf["numFmtId"] < 165) { |
||
624 | $numFmt = \PhpSpreadsheet\Style\NumberFormat::builtInFormatCode((int)$xf["numFmtId"]); |
||
625 | } |
||
626 | } |
||
627 | |||
628 | $cellStyle = (object) array( |
||
629 | "numFmt" => $numFmt, |
||
630 | "font" => $xmlStyles->fonts->font[intval($xf["fontId"])], |
||
631 | "fill" => $xmlStyles->fills->fill[intval($xf["fillId"])], |
||
632 | "border" => $xmlStyles->borders->border[intval($xf["borderId"])], |
||
633 | "alignment" => $xf->alignment, |
||
634 | "protection" => $xf->protection, |
||
635 | "quotePrefix" => $quotePrefix, |
||
636 | ); |
||
637 | $cellStyles[] = $cellStyle; |
||
638 | |||
639 | // add style to cellStyleXf collection |
||
640 | $objStyle = new \PhpSpreadsheet\Style; |
||
641 | self::readStyle($objStyle, $cellStyle); |
||
642 | $excel->addCellStyleXf($objStyle); |
||
643 | } |
||
644 | } |
||
645 | |||
646 | $dxfs = array(); |
||
647 | if (!$this->readDataOnly && $xmlStyles) { |
||
648 | // Conditional Styles |
||
649 | if ($xmlStyles->dxfs) { |
||
650 | foreach ($xmlStyles->dxfs->dxf as $dxf) { |
||
651 | $style = new \PhpSpreadsheet\Style(false, true); |
||
652 | self::readStyle($style, $dxf); |
||
653 | $dxfs[] = $style; |
||
654 | } |
||
655 | } |
||
656 | // Cell Styles |
||
657 | if ($xmlStyles->cellStyles) { |
||
658 | foreach ($xmlStyles->cellStyles->cellStyle as $cellStyle) { |
||
659 | if (intval($cellStyle['builtinId']) == 0) { |
||
660 | if (isset($cellStyles[intval($cellStyle['xfId'])])) { |
||
661 | // Set default style |
||
662 | $style = new \PhpSpreadsheet\Style; |
||
663 | self::readStyle($style, $cellStyles[intval($cellStyle['xfId'])]); |
||
664 | |||
665 | // normal style, currently not using it for anything |
||
666 | } |
||
667 | } |
||
668 | } |
||
669 | } |
||
670 | } |
||
671 | |||
672 | $xmlWorkbook = simplexml_load_string( |
||
673 | //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main" |
||
674 | $this->securityScan($this->getFromZipArchive($zip, "{$rel['Target']}")), |
||
675 | 'SimpleXMLElement', |
||
676 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
677 | ); |
||
678 | |||
679 | // Set base date |
||
680 | if ($xmlWorkbook->workbookPr) { |
||
681 | \PhpSpreadsheet\Shared\Date::setExcelCalendar(\PhpSpreadsheet\Shared\Date::CALENDAR_WINDOWS_1900); |
||
682 | if (isset($xmlWorkbook->workbookPr['date1904'])) { |
||
683 | if (self::boolean((string) $xmlWorkbook->workbookPr['date1904'])) { |
||
684 | \PhpSpreadsheet\Shared\Date::setExcelCalendar(\PhpSpreadsheet\Shared\Date::CALENDAR_MAC_1904); |
||
685 | } |
||
686 | } |
||
687 | } |
||
688 | |||
689 | $sheetId = 0; // keep track of new sheet id in final workbook |
||
690 | $oldSheetId = -1; // keep track of old sheet id in final workbook |
||
691 | $countSkippedSheets = 0; // keep track of number of skipped sheets |
||
692 | $mapSheetId = array(); // mapping of sheet ids from old to new |
||
693 | |||
694 | $charts = $chartDetails = array(); |
||
695 | |||
696 | if ($xmlWorkbook->sheets) { |
||
697 | foreach ($xmlWorkbook->sheets->sheet as $eleSheet) { |
||
698 | ++$oldSheetId; |
||
699 | |||
700 | // Check if sheet should be skipped |
||
701 | if (isset($this->loadSheetsOnly) && !in_array((string) $eleSheet["name"], $this->loadSheetsOnly)) { |
||
702 | ++$countSkippedSheets; |
||
703 | $mapSheetId[$oldSheetId] = null; |
||
704 | continue; |
||
705 | } |
||
706 | |||
707 | // Map old sheet id in original workbook to new sheet id. |
||
708 | // They will differ if loadSheetsOnly() is being used |
||
709 | $mapSheetId[$oldSheetId] = $oldSheetId - $countSkippedSheets; |
||
710 | |||
711 | // Load sheet |
||
712 | $docSheet = $excel->createSheet(); |
||
713 | // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet |
||
714 | // references in formula cells... during the load, all formulae should be correct, |
||
715 | // and we're simply bringing the worksheet name in line with the formula, not the |
||
716 | // reverse |
||
717 | $docSheet->setTitle((string) $eleSheet["name"], false); |
||
718 | $fileWorksheet = $worksheets[(string) self::getArrayItem($eleSheet->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), "id")]; |
||
719 | $xmlSheet = simplexml_load_string( |
||
720 | //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main" |
||
721 | $this->securityScan($this->getFromZipArchive($zip, "$dir/$fileWorksheet")), |
||
722 | 'SimpleXMLElement', |
||
723 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
724 | ); |
||
725 | |||
726 | $sharedFormulas = array(); |
||
727 | |||
728 | if (isset($eleSheet["state"]) && (string) $eleSheet["state"] != '') { |
||
729 | $docSheet->setSheetState((string) $eleSheet["state"]); |
||
730 | } |
||
731 | |||
732 | if (isset($xmlSheet->sheetViews) && isset($xmlSheet->sheetViews->sheetView)) { |
||
733 | View Code Duplication | if (isset($xmlSheet->sheetViews->sheetView['zoomScale'])) { |
|
734 | $docSheet->getSheetView()->setZoomScale(intval($xmlSheet->sheetViews->sheetView['zoomScale'])); |
||
735 | } |
||
736 | View Code Duplication | if (isset($xmlSheet->sheetViews->sheetView['zoomScaleNormal'])) { |
|
737 | $docSheet->getSheetView()->setZoomScaleNormal(intval($xmlSheet->sheetViews->sheetView['zoomScaleNormal'])); |
||
738 | } |
||
739 | if (isset($xmlSheet->sheetViews->sheetView['view'])) { |
||
740 | $docSheet->getSheetView()->setView((string) $xmlSheet->sheetViews->sheetView['view']); |
||
741 | } |
||
742 | View Code Duplication | if (isset($xmlSheet->sheetViews->sheetView['showGridLines'])) { |
|
743 | $docSheet->setShowGridLines(self::boolean((string)$xmlSheet->sheetViews->sheetView['showGridLines'])); |
||
744 | } |
||
745 | View Code Duplication | if (isset($xmlSheet->sheetViews->sheetView['showRowColHeaders'])) { |
|
746 | $docSheet->setShowRowColHeaders(self::boolean((string)$xmlSheet->sheetViews->sheetView['showRowColHeaders'])); |
||
747 | } |
||
748 | View Code Duplication | if (isset($xmlSheet->sheetViews->sheetView['rightToLeft'])) { |
|
749 | $docSheet->setRightToLeft(self::boolean((string)$xmlSheet->sheetViews->sheetView['rightToLeft'])); |
||
750 | } |
||
751 | if (isset($xmlSheet->sheetViews->sheetView->pane)) { |
||
752 | if (isset($xmlSheet->sheetViews->sheetView->pane['topLeftCell'])) { |
||
753 | $docSheet->freezePane((string)$xmlSheet->sheetViews->sheetView->pane['topLeftCell']); |
||
754 | } else { |
||
755 | $xSplit = 0; |
||
756 | $ySplit = 0; |
||
757 | |||
758 | View Code Duplication | if (isset($xmlSheet->sheetViews->sheetView->pane['xSplit'])) { |
|
759 | $xSplit = 1 + intval($xmlSheet->sheetViews->sheetView->pane['xSplit']); |
||
760 | } |
||
761 | |||
762 | View Code Duplication | if (isset($xmlSheet->sheetViews->sheetView->pane['ySplit'])) { |
|
763 | $ySplit = 1 + intval($xmlSheet->sheetViews->sheetView->pane['ySplit']); |
||
764 | } |
||
765 | |||
766 | $docSheet->freezePaneByColumnAndRow($xSplit, $ySplit); |
||
767 | } |
||
768 | } |
||
769 | |||
770 | if (isset($xmlSheet->sheetViews->sheetView->selection)) { |
||
771 | if (isset($xmlSheet->sheetViews->sheetView->selection['sqref'])) { |
||
772 | $sqref = (string)$xmlSheet->sheetViews->sheetView->selection['sqref']; |
||
773 | $sqref = explode(' ', $sqref); |
||
774 | $sqref = $sqref[0]; |
||
775 | $docSheet->setSelectedCells($sqref); |
||
776 | } |
||
777 | } |
||
778 | } |
||
779 | |||
780 | if (isset($xmlSheet->sheetPr) && isset($xmlSheet->sheetPr->tabColor)) { |
||
781 | if (isset($xmlSheet->sheetPr->tabColor['rgb'])) { |
||
782 | $docSheet->getTabColor()->setARGB((string)$xmlSheet->sheetPr->tabColor['rgb']); |
||
783 | } |
||
784 | } |
||
785 | if (isset($xmlSheet->sheetPr) && isset($xmlSheet->sheetPr['codeName'])) { |
||
786 | $docSheet->setCodeName((string) $xmlSheet->sheetPr['codeName']); |
||
787 | } |
||
788 | if (isset($xmlSheet->sheetPr) && isset($xmlSheet->sheetPr->outlinePr)) { |
||
789 | View Code Duplication | if (isset($xmlSheet->sheetPr->outlinePr['summaryRight']) && |
|
790 | !self::boolean((string) $xmlSheet->sheetPr->outlinePr['summaryRight'])) { |
||
791 | $docSheet->setShowSummaryRight(false); |
||
792 | } else { |
||
793 | $docSheet->setShowSummaryRight(true); |
||
794 | } |
||
795 | |||
796 | View Code Duplication | if (isset($xmlSheet->sheetPr->outlinePr['summaryBelow']) && |
|
797 | !self::boolean((string) $xmlSheet->sheetPr->outlinePr['summaryBelow'])) { |
||
798 | $docSheet->setShowSummaryBelow(false); |
||
799 | } else { |
||
800 | $docSheet->setShowSummaryBelow(true); |
||
801 | } |
||
802 | } |
||
803 | |||
804 | if (isset($xmlSheet->sheetPr) && isset($xmlSheet->sheetPr->pageSetUpPr)) { |
||
805 | if (isset($xmlSheet->sheetPr->pageSetUpPr['fitToPage']) && |
||
806 | !self::boolean((string) $xmlSheet->sheetPr->pageSetUpPr['fitToPage'])) { |
||
807 | $docSheet->getPageSetup()->setFitToPage(false); |
||
808 | } else { |
||
809 | $docSheet->getPageSetup()->setFitToPage(true); |
||
810 | } |
||
811 | } |
||
812 | |||
813 | if (isset($xmlSheet->sheetFormatPr)) { |
||
814 | if (isset($xmlSheet->sheetFormatPr['customHeight']) && |
||
815 | self::boolean((string) $xmlSheet->sheetFormatPr['customHeight']) && |
||
816 | isset($xmlSheet->sheetFormatPr['defaultRowHeight'])) { |
||
817 | $docSheet->getDefaultRowDimension()->setRowHeight((float)$xmlSheet->sheetFormatPr['defaultRowHeight']); |
||
818 | } |
||
819 | if (isset($xmlSheet->sheetFormatPr['defaultColWidth'])) { |
||
820 | $docSheet->getDefaultColumnDimension()->setWidth((float)$xmlSheet->sheetFormatPr['defaultColWidth']); |
||
821 | } |
||
822 | if (isset($xmlSheet->sheetFormatPr['zeroHeight']) && |
||
823 | ((string)$xmlSheet->sheetFormatPr['zeroHeight'] == '1')) { |
||
824 | $docSheet->getDefaultRowDimension()->setZeroHeight(true); |
||
825 | } |
||
826 | } |
||
827 | |||
828 | if (isset($xmlSheet->cols) && !$this->readDataOnly) { |
||
829 | foreach ($xmlSheet->cols->col as $col) { |
||
830 | for ($i = intval($col["min"]) - 1; $i < intval($col["max"]); ++$i) { |
||
831 | if ($col["style"] && !$this->readDataOnly) { |
||
832 | $docSheet->getColumnDimension(\PhpSpreadsheet\Cell::stringFromColumnIndex($i))->setXfIndex(intval($col["style"])); |
||
833 | } |
||
834 | if (self::boolean($col["bestFit"])) { |
||
835 | //$docSheet->getColumnDimension(\PhpSpreadsheet\Cell::stringFromColumnIndex($i))->setAutoSize(true); |
||
836 | } |
||
837 | if (self::boolean($col["hidden"])) { |
||
838 | // echo \PhpSpreadsheet\Cell::stringFromColumnIndex($i), ': HIDDEN COLUMN',PHP_EOL; |
||
839 | $docSheet->getColumnDimension(\PhpSpreadsheet\Cell::stringFromColumnIndex($i))->setVisible(false); |
||
840 | } |
||
841 | if (self::boolean($col["collapsed"])) { |
||
842 | $docSheet->getColumnDimension(\PhpSpreadsheet\Cell::stringFromColumnIndex($i))->setCollapsed(true); |
||
843 | } |
||
844 | if ($col["outlineLevel"] > 0) { |
||
845 | $docSheet->getColumnDimension(\PhpSpreadsheet\Cell::stringFromColumnIndex($i))->setOutlineLevel(intval($col["outlineLevel"])); |
||
846 | } |
||
847 | $docSheet->getColumnDimension(\PhpSpreadsheet\Cell::stringFromColumnIndex($i))->setWidth(floatval($col["width"])); |
||
848 | |||
849 | if (intval($col["max"]) == 16384) { |
||
850 | break; |
||
851 | } |
||
852 | } |
||
853 | } |
||
854 | } |
||
855 | |||
856 | if (isset($xmlSheet->printOptions) && !$this->readDataOnly) { |
||
857 | if (self::boolean((string) $xmlSheet->printOptions['gridLinesSet'])) { |
||
858 | $docSheet->setShowGridlines(true); |
||
859 | } |
||
860 | if (self::boolean((string) $xmlSheet->printOptions['gridLines'])) { |
||
861 | $docSheet->setPrintGridlines(true); |
||
862 | } |
||
863 | if (self::boolean((string) $xmlSheet->printOptions['horizontalCentered'])) { |
||
864 | $docSheet->getPageSetup()->setHorizontalCentered(true); |
||
865 | } |
||
866 | if (self::boolean((string) $xmlSheet->printOptions['verticalCentered'])) { |
||
867 | $docSheet->getPageSetup()->setVerticalCentered(true); |
||
868 | } |
||
869 | } |
||
870 | |||
871 | if ($xmlSheet && $xmlSheet->sheetData && $xmlSheet->sheetData->row) { |
||
872 | foreach ($xmlSheet->sheetData->row as $row) { |
||
873 | if ($row["ht"] && !$this->readDataOnly) { |
||
874 | $docSheet->getRowDimension(intval($row["r"]))->setRowHeight(floatval($row["ht"])); |
||
875 | } |
||
876 | if (self::boolean($row["hidden"]) && !$this->readDataOnly) { |
||
877 | $docSheet->getRowDimension(intval($row["r"]))->setVisible(false); |
||
878 | } |
||
879 | if (self::boolean($row["collapsed"])) { |
||
880 | $docSheet->getRowDimension(intval($row["r"]))->setCollapsed(true); |
||
881 | } |
||
882 | if ($row["outlineLevel"] > 0) { |
||
883 | $docSheet->getRowDimension(intval($row["r"]))->setOutlineLevel(intval($row["outlineLevel"])); |
||
884 | } |
||
885 | View Code Duplication | if ($row["s"] && !$this->readDataOnly) { |
|
886 | $docSheet->getRowDimension(intval($row["r"]))->setXfIndex(intval($row["s"])); |
||
887 | } |
||
888 | |||
889 | foreach ($row->c as $c) { |
||
890 | $r = (string) $c["r"]; |
||
891 | $cellDataType = (string) $c["t"]; |
||
892 | $value = null; |
||
893 | $calculatedValue = null; |
||
894 | |||
895 | // Read cell? |
||
896 | if ($this->getReadFilter() !== null) { |
||
897 | $coordinates = \PhpSpreadsheet\Cell::coordinateFromString($r); |
||
898 | |||
899 | if (!$this->getReadFilter()->readCell($coordinates[0], $coordinates[1], $docSheet->getTitle())) { |
||
900 | continue; |
||
901 | } |
||
902 | } |
||
903 | |||
904 | // echo 'Reading cell ', $coordinates[0], $coordinates[1], PHP_EOL; |
||
905 | // print_r($c); |
||
906 | // echo PHP_EOL; |
||
907 | // echo 'Cell Data Type is ', $cellDataType, ': '; |
||
908 | // |
||
909 | // Read cell! |
||
910 | switch ($cellDataType) { |
||
911 | case "s": |
||
912 | // echo 'String', PHP_EOL; |
||
913 | if ((string)$c->v != '') { |
||
914 | $value = $sharedStrings[intval($c->v)]; |
||
915 | |||
916 | if ($value instanceof \PhpSpreadsheet\RichText) { |
||
917 | $value = clone $value; |
||
918 | } |
||
919 | } else { |
||
920 | $value = ''; |
||
921 | } |
||
922 | break; |
||
923 | case "b": |
||
924 | // echo 'Boolean', PHP_EOL; |
||
925 | if (!isset($c->f)) { |
||
926 | $value = self::castToBoolean($c); |
||
927 | } else { |
||
928 | // Formula |
||
929 | $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToBoolean'); |
||
930 | if (isset($c->f['t'])) { |
||
931 | $att = array(); |
||
932 | $att = $c->f; |
||
933 | $docSheet->getCell($r)->setFormulaAttributes($att); |
||
934 | } |
||
935 | // echo '$calculatedValue = ', $calculatedValue, PHP_EOL; |
||
936 | } |
||
937 | break; |
||
938 | View Code Duplication | case "inlineStr": |
|
939 | // echo 'Inline String', PHP_EOL; |
||
940 | if (isset($c->f)) { |
||
941 | $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError'); |
||
942 | } else { |
||
943 | $value = $this->parseRichText($c->is); |
||
944 | } |
||
945 | break; |
||
946 | View Code Duplication | case "e": |
|
947 | // echo 'Error', PHP_EOL; |
||
948 | if (!isset($c->f)) { |
||
949 | $value = self::castToError($c); |
||
950 | } else { |
||
951 | // Formula |
||
952 | $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError'); |
||
953 | // echo '$calculatedValue = ', $calculatedValue, PHP_EOL; |
||
954 | } |
||
955 | break; |
||
956 | default: |
||
957 | // echo 'Default', PHP_EOL; |
||
958 | if (!isset($c->f)) { |
||
959 | // echo 'Not a Formula', PHP_EOL; |
||
960 | $value = self::castToString($c); |
||
961 | } else { |
||
962 | // echo 'Treat as Formula', PHP_EOL; |
||
963 | // Formula |
||
964 | $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToString'); |
||
965 | // echo '$calculatedValue = ', $calculatedValue, PHP_EOL; |
||
966 | } |
||
967 | break; |
||
968 | } |
||
969 | // echo 'Value is ', $value, PHP_EOL; |
||
970 | |||
971 | // Check for numeric values |
||
972 | if (is_numeric($value) && $cellDataType != 's') { |
||
973 | if ($value == (int)$value) { |
||
974 | $value = (int)$value; |
||
975 | } elseif ($value == (float)$value) { |
||
976 | $value = (float)$value; |
||
977 | } elseif ($value == (double)$value) { |
||
978 | $value = (double)$value; |
||
979 | } |
||
980 | } |
||
981 | |||
982 | // Rich text? |
||
983 | if ($value instanceof \PhpSpreadsheet\RichText && $this->readDataOnly) { |
||
984 | $value = $value->getPlainText(); |
||
985 | } |
||
986 | |||
987 | $cell = $docSheet->getCell($r); |
||
988 | // Assign value |
||
989 | if ($cellDataType != '') { |
||
990 | $cell->setValueExplicit($value, $cellDataType); |
||
991 | } else { |
||
992 | $cell->setValue($value); |
||
993 | } |
||
994 | if ($calculatedValue !== null) { |
||
995 | $cell->setCalculatedValue($calculatedValue); |
||
996 | } |
||
997 | |||
998 | // Style information? |
||
999 | View Code Duplication | if ($c["s"] && !$this->readDataOnly) { |
|
1000 | // no style index means 0, it seems |
||
1001 | $cell->setXfIndex(isset($styles[intval($c["s"])]) ? |
||
1002 | intval($c["s"]) : 0); |
||
1003 | } |
||
1004 | } |
||
1005 | } |
||
1006 | } |
||
1007 | |||
1008 | $conditionals = array(); |
||
1009 | if (!$this->readDataOnly && $xmlSheet && $xmlSheet->conditionalFormatting) { |
||
1010 | foreach ($xmlSheet->conditionalFormatting as $conditional) { |
||
1011 | foreach ($conditional->cfRule as $cfRule) { |
||
1012 | if (((string)$cfRule["type"] == \PhpSpreadsheet\Style\Conditional::CONDITION_NONE || (string)$cfRule["type"] == \PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS || (string)$cfRule["type"] == \PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT || (string)$cfRule["type"] == \PhpSpreadsheet\Style\Conditional::CONDITION_EXPRESSION) && isset($dxfs[intval($cfRule["dxfId"])])) { |
||
1013 | $conditionals[(string) $conditional["sqref"]][intval($cfRule["priority"])] = $cfRule; |
||
1014 | } |
||
1015 | } |
||
1016 | } |
||
1017 | |||
1018 | foreach ($conditionals as $ref => $cfRules) { |
||
1019 | ksort($cfRules); |
||
1020 | $conditionalStyles = array(); |
||
1021 | foreach ($cfRules as $cfRule) { |
||
1022 | $objConditional = new \PhpSpreadsheet\Style\Conditional(); |
||
1023 | $objConditional->setConditionType((string)$cfRule["type"]); |
||
1024 | $objConditional->setOperatorType((string)$cfRule["operator"]); |
||
1025 | |||
1026 | if ((string)$cfRule["text"] != '') { |
||
1027 | $objConditional->setText((string)$cfRule["text"]); |
||
1028 | } |
||
1029 | |||
1030 | if (count($cfRule->formula) > 1) { |
||
1031 | foreach ($cfRule->formula as $formula) { |
||
1032 | $objConditional->addCondition((string)$formula); |
||
1033 | } |
||
1034 | } else { |
||
1035 | $objConditional->addCondition((string)$cfRule->formula); |
||
1036 | } |
||
1037 | $objConditional->setStyle(clone $dxfs[intval($cfRule["dxfId"])]); |
||
1038 | $conditionalStyles[] = $objConditional; |
||
1039 | } |
||
1040 | |||
1041 | // Extract all cell references in $ref |
||
1042 | $cellBlocks = explode(' ', str_replace('$', '', strtoupper($ref))); |
||
1043 | foreach ($cellBlocks as $cellBlock) { |
||
1044 | $docSheet->getStyle($cellBlock)->setConditionalStyles($conditionalStyles); |
||
1045 | } |
||
1046 | } |
||
1047 | } |
||
1048 | |||
1049 | $aKeys = ["sheet", "objects", "scenarios", "formatCells", "formatColumns", "formatRows", "insertColumns", "insertRows", "insertHyperlinks", "deleteColumns", "deleteRows", "selectLockedCells", "sort", "autoFilter", "pivotTables", "selectUnlockedCells"]; |
||
1050 | if (!$this->readDataOnly && $xmlSheet && $xmlSheet->sheetProtection) { |
||
1051 | foreach ($aKeys as $key) { |
||
1052 | $method = "set" . ucfirst($key); |
||
1053 | $docSheet->getProtection()->$method(self::boolean((string) $xmlSheet->sheetProtection[$key])); |
||
1054 | } |
||
1055 | } |
||
1056 | |||
1057 | if (!$this->readDataOnly && $xmlSheet && $xmlSheet->sheetProtection) { |
||
1058 | $docSheet->getProtection()->setPassword((string) $xmlSheet->sheetProtection["password"], true); |
||
1059 | if ($xmlSheet->protectedRanges->protectedRange) { |
||
1060 | foreach ($xmlSheet->protectedRanges->protectedRange as $protectedRange) { |
||
1061 | $docSheet->protectCells((string) $protectedRange["sqref"], (string) $protectedRange["password"], true); |
||
1062 | } |
||
1063 | } |
||
1064 | } |
||
1065 | |||
1066 | if ($xmlSheet && $xmlSheet->autoFilter && !$this->readDataOnly) { |
||
1067 | $autoFilterRange = (string) $xmlSheet->autoFilter["ref"]; |
||
1068 | if (strpos($autoFilterRange, ':') !== false) { |
||
1069 | $autoFilter = $docSheet->getAutoFilter(); |
||
1070 | $autoFilter->setRange($autoFilterRange); |
||
1071 | |||
1072 | foreach ($xmlSheet->autoFilter->filterColumn as $filterColumn) { |
||
1073 | $column = $autoFilter->getColumnByOffset((integer) $filterColumn["colId"]); |
||
1074 | // Check for standard filters |
||
1075 | if ($filterColumn->filters) { |
||
1076 | $column->setFilterType(\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER); |
||
1077 | $filters = $filterColumn->filters; |
||
1078 | if ((isset($filters["blank"])) && ($filters["blank"] == 1)) { |
||
1079 | // Operator is undefined, but always treated as EQUAL |
||
1080 | $column->createRule()->setRule(null, '')->setRuleType(\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_FILTER); |
||
1081 | } |
||
1082 | // Standard filters are always an OR join, so no join rule needs to be set |
||
1083 | // Entries can be either filter elements |
||
1084 | View Code Duplication | foreach ($filters->filter as $filterRule) { |
|
1085 | // Operator is undefined, but always treated as EQUAL |
||
1086 | $column->createRule()->setRule(null, (string) $filterRule["val"])->setRuleType(\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_FILTER); |
||
1087 | } |
||
1088 | // Or Date Group elements |
||
1089 | foreach ($filters->dateGroupItem as $dateGroupItem) { |
||
1090 | $column->createRule()->setRule( |
||
1091 | // Operator is undefined, but always treated as EQUAL |
||
1092 | null, |
||
1093 | array( |
||
1094 | 'year' => (string) $dateGroupItem["year"], |
||
1095 | 'month' => (string) $dateGroupItem["month"], |
||
1096 | 'day' => (string) $dateGroupItem["day"], |
||
1097 | 'hour' => (string) $dateGroupItem["hour"], |
||
1098 | 'minute' => (string) $dateGroupItem["minute"], |
||
1099 | 'second' => (string) $dateGroupItem["second"], |
||
1100 | ), |
||
1101 | (string) $dateGroupItem["dateTimeGrouping"] |
||
1102 | ) |
||
1103 | ->setRuleType(\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_DATEGROUP); |
||
1104 | } |
||
1105 | } |
||
1106 | // Check for custom filters |
||
1107 | if ($filterColumn->customFilters) { |
||
1108 | $column->setFilterType(\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); |
||
1109 | $customFilters = $filterColumn->customFilters; |
||
1110 | // Custom filters can an AND or an OR join; |
||
1111 | // and there should only ever be one or two entries |
||
1112 | if ((isset($customFilters["and"])) && ($customFilters["and"] == 1)) { |
||
1113 | $column->setJoin(\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_COLUMN_JOIN_AND); |
||
1114 | } |
||
1115 | View Code Duplication | foreach ($customFilters->customFilter as $filterRule) { |
|
1116 | $column->createRule()->setRule( |
||
1117 | (string) $filterRule["operator"], |
||
1118 | (string) $filterRule["val"] |
||
1119 | ) |
||
1120 | ->setRuleType(\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); |
||
1121 | } |
||
1122 | } |
||
1123 | // Check for dynamic filters |
||
1124 | if ($filterColumn->dynamicFilter) { |
||
1125 | $column->setFilterType(\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); |
||
1126 | // We should only ever have one dynamic filter |
||
1127 | foreach ($filterColumn->dynamicFilter as $filterRule) { |
||
1128 | $column->createRule()->setRule( |
||
1129 | // Operator is undefined, but always treated as EQUAL |
||
1130 | null, |
||
1131 | (string) $filterRule["val"], |
||
1132 | (string) $filterRule["type"] |
||
1133 | ) |
||
1134 | ->setRuleType(\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); |
||
1135 | if (isset($filterRule["val"])) { |
||
1136 | $column->setAttribute('val', (string) $filterRule["val"]); |
||
1137 | } |
||
1138 | if (isset($filterRule["maxVal"])) { |
||
1139 | $column->setAttribute('maxVal', (string) $filterRule["maxVal"]); |
||
1140 | } |
||
1141 | } |
||
1142 | } |
||
1143 | // Check for dynamic filters |
||
1144 | if ($filterColumn->top10) { |
||
1145 | $column->setFilterType(\PhpSpreadsheet\Worksheet\AutoFilter\Column::AUTOFILTER_FILTERTYPE_TOPTENFILTER); |
||
1146 | // We should only ever have one top10 filter |
||
1147 | foreach ($filterColumn->top10 as $filterRule) { |
||
1148 | $column->createRule()->setRule( |
||
1149 | (((isset($filterRule["percent"])) && ($filterRule["percent"] == 1)) |
||
1150 | ? \PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT |
||
1151 | : \PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE |
||
1152 | ), |
||
1153 | (string) $filterRule["val"], |
||
1154 | (((isset($filterRule["top"])) && ($filterRule["top"] == 1)) |
||
1155 | ? \PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP |
||
1156 | : \PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM |
||
1157 | ) |
||
1158 | ) |
||
1159 | ->setRuleType(\PhpSpreadsheet\Worksheet\AutoFilter\Column\Rule::AUTOFILTER_RULETYPE_TOPTENFILTER); |
||
1160 | } |
||
1161 | } |
||
1162 | } |
||
1163 | } |
||
1164 | } |
||
1165 | |||
1166 | if ($xmlSheet && $xmlSheet->mergeCells && $xmlSheet->mergeCells->mergeCell && !$this->readDataOnly) { |
||
1167 | foreach ($xmlSheet->mergeCells->mergeCell as $mergeCell) { |
||
1168 | $mergeRef = (string) $mergeCell["ref"]; |
||
1169 | if (strpos($mergeRef, ':') !== false) { |
||
1170 | $docSheet->mergeCells((string) $mergeCell["ref"]); |
||
1171 | } |
||
1172 | } |
||
1173 | } |
||
1174 | |||
1175 | if ($xmlSheet && $xmlSheet->pageMargins && !$this->readDataOnly) { |
||
1176 | $docPageMargins = $docSheet->getPageMargins(); |
||
1177 | $docPageMargins->setLeft(floatval($xmlSheet->pageMargins["left"])); |
||
1178 | $docPageMargins->setRight(floatval($xmlSheet->pageMargins["right"])); |
||
1179 | $docPageMargins->setTop(floatval($xmlSheet->pageMargins["top"])); |
||
1180 | $docPageMargins->setBottom(floatval($xmlSheet->pageMargins["bottom"])); |
||
1181 | $docPageMargins->setHeader(floatval($xmlSheet->pageMargins["header"])); |
||
1182 | $docPageMargins->setFooter(floatval($xmlSheet->pageMargins["footer"])); |
||
1183 | } |
||
1184 | |||
1185 | if ($xmlSheet && $xmlSheet->pageSetup && !$this->readDataOnly) { |
||
1186 | $docPageSetup = $docSheet->getPageSetup(); |
||
1187 | |||
1188 | if (isset($xmlSheet->pageSetup["orientation"])) { |
||
1189 | $docPageSetup->setOrientation((string) $xmlSheet->pageSetup["orientation"]); |
||
1190 | } |
||
1191 | if (isset($xmlSheet->pageSetup["paperSize"])) { |
||
1192 | $docPageSetup->setPaperSize(intval($xmlSheet->pageSetup["paperSize"])); |
||
1193 | } |
||
1194 | if (isset($xmlSheet->pageSetup["scale"])) { |
||
1195 | $docPageSetup->setScale(intval($xmlSheet->pageSetup["scale"]), false); |
||
1196 | } |
||
1197 | View Code Duplication | if (isset($xmlSheet->pageSetup["fitToHeight"]) && intval($xmlSheet->pageSetup["fitToHeight"]) >= 0) { |
|
1198 | $docPageSetup->setFitToHeight(intval($xmlSheet->pageSetup["fitToHeight"]), false); |
||
1199 | } |
||
1200 | View Code Duplication | if (isset($xmlSheet->pageSetup["fitToWidth"]) && intval($xmlSheet->pageSetup["fitToWidth"]) >= 0) { |
|
1201 | $docPageSetup->setFitToWidth(intval($xmlSheet->pageSetup["fitToWidth"]), false); |
||
1202 | } |
||
1203 | if (isset($xmlSheet->pageSetup["firstPageNumber"]) && isset($xmlSheet->pageSetup["useFirstPageNumber"]) && |
||
1204 | self::boolean((string) $xmlSheet->pageSetup["useFirstPageNumber"])) { |
||
1205 | $docPageSetup->setFirstPageNumber(intval($xmlSheet->pageSetup["firstPageNumber"])); |
||
1206 | } |
||
1207 | } |
||
1208 | |||
1209 | if ($xmlSheet && $xmlSheet->headerFooter && !$this->readDataOnly) { |
||
1210 | $docHeaderFooter = $docSheet->getHeaderFooter(); |
||
1211 | |||
1212 | View Code Duplication | if (isset($xmlSheet->headerFooter["differentOddEven"]) && |
|
1213 | self::boolean((string)$xmlSheet->headerFooter["differentOddEven"])) { |
||
1214 | $docHeaderFooter->setDifferentOddEven(true); |
||
1215 | } else { |
||
1216 | $docHeaderFooter->setDifferentOddEven(false); |
||
1217 | } |
||
1218 | View Code Duplication | if (isset($xmlSheet->headerFooter["differentFirst"]) && |
|
1219 | self::boolean((string)$xmlSheet->headerFooter["differentFirst"])) { |
||
1220 | $docHeaderFooter->setDifferentFirst(true); |
||
1221 | } else { |
||
1222 | $docHeaderFooter->setDifferentFirst(false); |
||
1223 | } |
||
1224 | View Code Duplication | if (isset($xmlSheet->headerFooter["scaleWithDoc"]) && |
|
1225 | !self::boolean((string)$xmlSheet->headerFooter["scaleWithDoc"])) { |
||
1226 | $docHeaderFooter->setScaleWithDocument(false); |
||
1227 | } else { |
||
1228 | $docHeaderFooter->setScaleWithDocument(true); |
||
1229 | } |
||
1230 | View Code Duplication | if (isset($xmlSheet->headerFooter["alignWithMargins"]) && |
|
1231 | !self::boolean((string)$xmlSheet->headerFooter["alignWithMargins"])) { |
||
1232 | $docHeaderFooter->setAlignWithMargins(false); |
||
1233 | } else { |
||
1234 | $docHeaderFooter->setAlignWithMargins(true); |
||
1235 | } |
||
1236 | |||
1237 | $docHeaderFooter->setOddHeader((string) $xmlSheet->headerFooter->oddHeader); |
||
1238 | $docHeaderFooter->setOddFooter((string) $xmlSheet->headerFooter->oddFooter); |
||
1239 | $docHeaderFooter->setEvenHeader((string) $xmlSheet->headerFooter->evenHeader); |
||
1240 | $docHeaderFooter->setEvenFooter((string) $xmlSheet->headerFooter->evenFooter); |
||
1241 | $docHeaderFooter->setFirstHeader((string) $xmlSheet->headerFooter->firstHeader); |
||
1242 | $docHeaderFooter->setFirstFooter((string) $xmlSheet->headerFooter->firstFooter); |
||
1243 | } |
||
1244 | |||
1245 | if ($xmlSheet && $xmlSheet->rowBreaks && $xmlSheet->rowBreaks->brk && !$this->readDataOnly) { |
||
1246 | foreach ($xmlSheet->rowBreaks->brk as $brk) { |
||
1247 | if ($brk["man"]) { |
||
1248 | $docSheet->setBreak("A$brk[id]", \PhpSpreadsheet\Worksheet::BREAK_ROW); |
||
1249 | } |
||
1250 | } |
||
1251 | } |
||
1252 | if ($xmlSheet && $xmlSheet->colBreaks && $xmlSheet->colBreaks->brk && !$this->readDataOnly) { |
||
1253 | foreach ($xmlSheet->colBreaks->brk as $brk) { |
||
1254 | if ($brk["man"]) { |
||
1255 | $docSheet->setBreak(\PhpSpreadsheet\Cell::stringFromColumnIndex((string) $brk["id"]) . "1", \PhpSpreadsheet\Worksheet::BREAK_COLUMN); |
||
1256 | } |
||
1257 | } |
||
1258 | } |
||
1259 | |||
1260 | if ($xmlSheet && $xmlSheet->dataValidations && !$this->readDataOnly) { |
||
1261 | foreach ($xmlSheet->dataValidations->dataValidation as $dataValidation) { |
||
1262 | // Uppercase coordinate |
||
1263 | $range = strtoupper($dataValidation["sqref"]); |
||
1264 | $rangeSet = explode(' ', $range); |
||
1265 | foreach ($rangeSet as $range) { |
||
1266 | $stRange = $docSheet->shrinkRangeToFit($range); |
||
1267 | |||
1268 | // Extract all cell references in $range |
||
1269 | foreach (\PhpSpreadsheet\Cell::extractAllCellReferencesInRange($stRange) as $reference) { |
||
1270 | // Create validation |
||
1271 | $docValidation = $docSheet->getCell($reference)->getDataValidation(); |
||
1272 | $docValidation->setType((string) $dataValidation["type"]); |
||
1273 | $docValidation->setErrorStyle((string) $dataValidation["errorStyle"]); |
||
1274 | $docValidation->setOperator((string) $dataValidation["operator"]); |
||
1275 | $docValidation->setAllowBlank($dataValidation["allowBlank"] != 0); |
||
1276 | $docValidation->setShowDropDown($dataValidation["showDropDown"] == 0); |
||
1277 | $docValidation->setShowInputMessage($dataValidation["showInputMessage"] != 0); |
||
1278 | $docValidation->setShowErrorMessage($dataValidation["showErrorMessage"] != 0); |
||
1279 | $docValidation->setErrorTitle((string) $dataValidation["errorTitle"]); |
||
1280 | $docValidation->setError((string) $dataValidation["error"]); |
||
1281 | $docValidation->setPromptTitle((string) $dataValidation["promptTitle"]); |
||
1282 | $docValidation->setPrompt((string) $dataValidation["prompt"]); |
||
1283 | $docValidation->setFormula1((string) $dataValidation->formula1); |
||
1284 | $docValidation->setFormula2((string) $dataValidation->formula2); |
||
1285 | } |
||
1286 | } |
||
1287 | } |
||
1288 | } |
||
1289 | |||
1290 | // Add hyperlinks |
||
1291 | $hyperlinks = array(); |
||
1292 | if (!$this->readDataOnly) { |
||
1293 | // Locate hyperlink relations |
||
1294 | if ($zip->locateName(dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels")) { |
||
1295 | $relsWorksheet = simplexml_load_string( |
||
1296 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1297 | $this->securityScan( |
||
1298 | $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels") |
||
1299 | ), |
||
1300 | 'SimpleXMLElement', |
||
1301 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1302 | ); |
||
1303 | foreach ($relsWorksheet->Relationship as $ele) { |
||
1304 | if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink") { |
||
1305 | $hyperlinks[(string)$ele["Id"]] = (string)$ele["Target"]; |
||
1306 | } |
||
1307 | } |
||
1308 | } |
||
1309 | |||
1310 | // Loop through hyperlinks |
||
1311 | if ($xmlSheet && $xmlSheet->hyperlinks) { |
||
1312 | foreach ($xmlSheet->hyperlinks->hyperlink as $hyperlink) { |
||
1313 | // Link url |
||
1314 | $linkRel = $hyperlink->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'); |
||
1315 | |||
1316 | foreach (\PhpSpreadsheet\Cell::extractAllCellReferencesInRange($hyperlink['ref']) as $cellReference) { |
||
1317 | $cell = $docSheet->getCell($cellReference); |
||
1318 | if (isset($linkRel['id'])) { |
||
1319 | $hyperlinkUrl = $hyperlinks[ (string)$linkRel['id'] ]; |
||
1320 | if (isset($hyperlink['location'])) { |
||
1321 | $hyperlinkUrl .= '#' . (string) $hyperlink['location']; |
||
1322 | } |
||
1323 | $cell->getHyperlink()->setUrl($hyperlinkUrl); |
||
1324 | } elseif (isset($hyperlink['location'])) { |
||
1325 | $cell->getHyperlink()->setUrl('sheet://' . (string)$hyperlink['location']); |
||
1326 | } |
||
1327 | |||
1328 | // Tooltip |
||
1329 | if (isset($hyperlink['tooltip'])) { |
||
1330 | $cell->getHyperlink()->setTooltip((string)$hyperlink['tooltip']); |
||
1331 | } |
||
1332 | } |
||
1333 | } |
||
1334 | } |
||
1335 | } |
||
1336 | |||
1337 | // Add comments |
||
1338 | $comments = array(); |
||
1339 | $vmlComments = array(); |
||
1340 | if (!$this->readDataOnly) { |
||
1341 | // Locate comment relations |
||
1342 | if ($zip->locateName(dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels")) { |
||
1343 | $relsWorksheet = simplexml_load_string( |
||
1344 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1345 | $this->securityScan( |
||
1346 | $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels") |
||
1347 | ), |
||
1348 | 'SimpleXMLElement', |
||
1349 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1350 | ); |
||
1351 | foreach ($relsWorksheet->Relationship as $ele) { |
||
1352 | if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments") { |
||
1353 | $comments[(string)$ele["Id"]] = (string)$ele["Target"]; |
||
1354 | } |
||
1355 | if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing") { |
||
1356 | $vmlComments[(string)$ele["Id"]] = (string)$ele["Target"]; |
||
1357 | } |
||
1358 | } |
||
1359 | } |
||
1360 | |||
1361 | // Loop through comments |
||
1362 | foreach ($comments as $relName => $relPath) { |
||
1363 | // Load comments file |
||
1364 | $relPath = \PhpSpreadsheet\Shared\File::realpath(dirname("$dir/$fileWorksheet") . "/" . $relPath); |
||
1365 | $commentsFile = simplexml_load_string( |
||
1366 | $this->securityScan($this->getFromZipArchive($zip, $relPath)), |
||
1367 | 'SimpleXMLElement', |
||
1368 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1369 | ); |
||
1370 | |||
1371 | // Utility variables |
||
1372 | $authors = array(); |
||
1373 | |||
1374 | // Loop through authors |
||
1375 | foreach ($commentsFile->authors->author as $author) { |
||
1376 | $authors[] = (string)$author; |
||
1377 | } |
||
1378 | |||
1379 | // Loop through contents |
||
1380 | foreach ($commentsFile->commentList->comment as $comment) { |
||
1381 | if (!empty($comment['authorId'])) { |
||
1382 | $docSheet->getComment((string)$comment['ref'])->setAuthor($authors[(string)$comment['authorId']]); |
||
1383 | } |
||
1384 | $docSheet->getComment((string)$comment['ref'])->setText($this->parseRichText($comment->text)); |
||
1385 | } |
||
1386 | } |
||
1387 | |||
1388 | // Loop through VML comments |
||
1389 | foreach ($vmlComments as $relName => $relPath) { |
||
1390 | // Load VML comments file |
||
1391 | $relPath = \PhpSpreadsheet\Shared\File::realpath(dirname("$dir/$fileWorksheet") . "/" . $relPath); |
||
1392 | $vmlCommentsFile = simplexml_load_string( |
||
1393 | $this->securityScan($this->getFromZipArchive($zip, $relPath)), |
||
1394 | 'SimpleXMLElement', |
||
1395 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1396 | ); |
||
1397 | $vmlCommentsFile->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml'); |
||
1398 | |||
1399 | $shapes = $vmlCommentsFile->xpath('//v:shape'); |
||
1400 | foreach ($shapes as $shape) { |
||
1401 | $shape->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml'); |
||
1402 | |||
1403 | if (isset($shape['style'])) { |
||
1404 | $style = (string)$shape['style']; |
||
1405 | $fillColor = strtoupper(substr((string)$shape['fillcolor'], 1)); |
||
1406 | $column = null; |
||
1407 | $row = null; |
||
1408 | |||
1409 | $clientData = $shape->xpath('.//x:ClientData'); |
||
1410 | if (is_array($clientData) && !empty($clientData)) { |
||
1411 | $clientData = $clientData[0]; |
||
1412 | |||
1413 | if (isset($clientData['ObjectType']) && (string)$clientData['ObjectType'] == 'Note') { |
||
1414 | $temp = $clientData->xpath('.//x:Row'); |
||
1415 | if (is_array($temp)) { |
||
1416 | $row = $temp[0]; |
||
1417 | } |
||
1418 | |||
1419 | $temp = $clientData->xpath('.//x:Column'); |
||
1420 | if (is_array($temp)) { |
||
1421 | $column = $temp[0]; |
||
1422 | } |
||
1423 | } |
||
1424 | } |
||
1425 | |||
1426 | if (($column !== null) && ($row !== null)) { |
||
1427 | // Set comment properties |
||
1428 | $comment = $docSheet->getCommentByColumnAndRow((string) $column, $row + 1); |
||
1429 | $comment->getFillColor()->setRGB($fillColor); |
||
1430 | |||
1431 | // Parse style |
||
1432 | $styleArray = explode(';', str_replace(' ', '', $style)); |
||
1433 | foreach ($styleArray as $stylePair) { |
||
1434 | $stylePair = explode(':', $stylePair); |
||
1435 | |||
1436 | if ($stylePair[0] == 'margin-left') { |
||
1437 | $comment->setMarginLeft($stylePair[1]); |
||
1438 | } |
||
1439 | if ($stylePair[0] == 'margin-top') { |
||
1440 | $comment->setMarginTop($stylePair[1]); |
||
1441 | } |
||
1442 | if ($stylePair[0] == 'width') { |
||
1443 | $comment->setWidth($stylePair[1]); |
||
1444 | } |
||
1445 | if ($stylePair[0] == 'height') { |
||
1446 | $comment->setHeight($stylePair[1]); |
||
1447 | } |
||
1448 | if ($stylePair[0] == 'visibility') { |
||
1449 | $comment->setVisible($stylePair[1] == 'visible'); |
||
1450 | } |
||
1451 | } |
||
1452 | } |
||
1453 | } |
||
1454 | } |
||
1455 | } |
||
1456 | |||
1457 | // Header/footer images |
||
1458 | if ($xmlSheet && $xmlSheet->legacyDrawingHF && !$this->readDataOnly) { |
||
1459 | if ($zip->locateName(dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels")) { |
||
1460 | $relsWorksheet = simplexml_load_string( |
||
1461 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1462 | $this->securityScan( |
||
1463 | $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels") |
||
1464 | ), |
||
1465 | 'SimpleXMLElement', |
||
1466 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1467 | ); |
||
1468 | $vmlRelationship = ''; |
||
1469 | |||
1470 | foreach ($relsWorksheet->Relationship as $ele) { |
||
1471 | if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing") { |
||
1472 | $vmlRelationship = self::dirAdd("$dir/$fileWorksheet", $ele["Target"]); |
||
1473 | } |
||
1474 | } |
||
1475 | |||
1476 | if ($vmlRelationship != '') { |
||
1477 | // Fetch linked images |
||
1478 | $relsVML = simplexml_load_string( |
||
1479 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1480 | $this->securityScan( |
||
1481 | $this->getFromZipArchive($zip, dirname($vmlRelationship) . '/_rels/' . basename($vmlRelationship) . '.rels') |
||
1482 | ), |
||
1483 | 'SimpleXMLElement', |
||
1484 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1485 | ); |
||
1486 | $drawings = array(); |
||
1487 | View Code Duplication | foreach ($relsVML->Relationship as $ele) { |
|
1488 | if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image") { |
||
1489 | $drawings[(string) $ele["Id"]] = self::dirAdd($vmlRelationship, $ele["Target"]); |
||
1490 | } |
||
1491 | } |
||
1492 | |||
1493 | // Fetch VML document |
||
1494 | $vmlDrawing = simplexml_load_string( |
||
1495 | $this->securityScan($this->getFromZipArchive($zip, $vmlRelationship)), |
||
1496 | 'SimpleXMLElement', |
||
1497 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1498 | ); |
||
1499 | $vmlDrawing->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml'); |
||
1500 | |||
1501 | $hfImages = array(); |
||
1502 | |||
1503 | $shapes = $vmlDrawing->xpath('//v:shape'); |
||
1504 | foreach ($shapes as $idx => $shape) { |
||
1505 | $shape->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml'); |
||
1506 | $imageData = $shape->xpath('//v:imagedata'); |
||
1507 | $imageData = $imageData[$idx]; |
||
1508 | |||
1509 | $imageData = $imageData->attributes('urn:schemas-microsoft-com:office:office'); |
||
1510 | $style = self::toCSSArray((string)$shape['style']); |
||
1511 | |||
1512 | $hfImages[(string) $shape['id']] = new \PhpSpreadsheet\Worksheet\HeaderFooterDrawing(); |
||
1513 | if (isset($imageData['title'])) { |
||
1514 | $hfImages[(string) $shape['id']]->setName((string)$imageData['title']); |
||
1515 | } |
||
1516 | |||
1517 | $hfImages[(string) $shape['id']]->setPath("zip://".\PhpSpreadsheet\Shared_File::realpath($pFilename)."#" . $drawings[(string)$imageData['relid']], false); |
||
1518 | $hfImages[(string) $shape['id']]->setResizeProportional(false); |
||
1519 | $hfImages[(string) $shape['id']]->setWidth($style['width']); |
||
1520 | $hfImages[(string) $shape['id']]->setHeight($style['height']); |
||
1521 | if (isset($style['margin-left'])) { |
||
1522 | $hfImages[(string) $shape['id']]->setOffsetX($style['margin-left']); |
||
1523 | } |
||
1524 | $hfImages[(string) $shape['id']]->setOffsetY($style['margin-top']); |
||
1525 | $hfImages[(string) $shape['id']]->setResizeProportional(true); |
||
1526 | } |
||
1527 | |||
1528 | $docSheet->getHeaderFooter()->setImages($hfImages); |
||
1529 | } |
||
1530 | } |
||
1531 | } |
||
1532 | } |
||
1533 | |||
1534 | // TODO: Autoshapes from twoCellAnchors! |
||
1535 | if ($zip->locateName(dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels")) { |
||
1536 | $relsWorksheet = simplexml_load_string( |
||
1537 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1538 | $this->securityScan( |
||
1539 | $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels") |
||
1540 | ), |
||
1541 | 'SimpleXMLElement', |
||
1542 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1543 | ); |
||
1544 | $drawings = array(); |
||
1545 | View Code Duplication | foreach ($relsWorksheet->Relationship as $ele) { |
|
1546 | if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing") { |
||
1547 | $drawings[(string) $ele["Id"]] = self::dirAdd("$dir/$fileWorksheet", $ele["Target"]); |
||
1548 | } |
||
1549 | } |
||
1550 | if ($xmlSheet->drawing && !$this->readDataOnly) { |
||
1551 | foreach ($xmlSheet->drawing as $drawing) { |
||
1552 | $fileDrawing = $drawings[(string) self::getArrayItem($drawing->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), "id")]; |
||
1553 | $relsDrawing = simplexml_load_string( |
||
1554 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1555 | $this->securityScan( |
||
1556 | $this->getFromZipArchive($zip, dirname($fileDrawing) . "/_rels/" . basename($fileDrawing) . ".rels") |
||
1557 | ), |
||
1558 | 'SimpleXMLElement', |
||
1559 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1560 | ); |
||
1561 | $images = array(); |
||
1562 | |||
1563 | if ($relsDrawing && $relsDrawing->Relationship) { |
||
1564 | foreach ($relsDrawing->Relationship as $ele) { |
||
1565 | if ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image") { |
||
1566 | $images[(string) $ele["Id"]] = self::dirAdd($fileDrawing, $ele["Target"]); |
||
1567 | } elseif ($ele["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart") { |
||
1568 | if ($this->includeCharts) { |
||
1569 | $charts[self::dirAdd($fileDrawing, $ele["Target"])] = array( |
||
1570 | 'id' => (string) $ele["Id"], |
||
1571 | 'sheet' => $docSheet->getTitle() |
||
1572 | ); |
||
1573 | } |
||
1574 | } |
||
1575 | } |
||
1576 | } |
||
1577 | $xmlDrawing = simplexml_load_string( |
||
1578 | $this->securityScan($this->getFromZipArchive($zip, $fileDrawing)), |
||
1579 | 'SimpleXMLElement', |
||
1580 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1581 | )->children("http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"); |
||
1582 | |||
1583 | if ($xmlDrawing->oneCellAnchor) { |
||
1584 | foreach ($xmlDrawing->oneCellAnchor as $oneCellAnchor) { |
||
1585 | if ($oneCellAnchor->pic->blipFill) { |
||
1586 | $blip = $oneCellAnchor->pic->blipFill->children("http://schemas.openxmlformats.org/drawingml/2006/main")->blip; |
||
1587 | $xfrm = $oneCellAnchor->pic->spPr->children("http://schemas.openxmlformats.org/drawingml/2006/main")->xfrm; |
||
1588 | $outerShdw = $oneCellAnchor->pic->spPr->children("http://schemas.openxmlformats.org/drawingml/2006/main")->effectLst->outerShdw; |
||
1589 | $objDrawing = new \PhpSpreadsheet\Worksheet\Drawing; |
||
1590 | $objDrawing->setName((string) self::getArrayItem($oneCellAnchor->pic->nvPicPr->cNvPr->attributes(), "name")); |
||
1591 | $objDrawing->setDescription((string) self::getArrayItem($oneCellAnchor->pic->nvPicPr->cNvPr->attributes(), "descr")); |
||
1592 | $objDrawing->setPath( |
||
1593 | "zip://".\PhpSpreadsheet\Shared\File::realpath($pFilename)."#" . |
||
1594 | $images[(string) self::getArrayItem( |
||
1595 | $blip->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), |
||
1596 | "embed" |
||
1597 | )], |
||
1598 | false |
||
1599 | ); |
||
1600 | $objDrawing->setCoordinates(\PhpSpreadsheet\Cell::stringFromColumnIndex((string) $oneCellAnchor->from->col) . ($oneCellAnchor->from->row + 1)); |
||
1601 | $objDrawing->setOffsetX(\PhpSpreadsheet\Shared\Drawing::EMUToPixels($oneCellAnchor->from->colOff)); |
||
1602 | $objDrawing->setOffsetY(\PhpSpreadsheet\Shared\Drawing::EMUToPixels($oneCellAnchor->from->rowOff)); |
||
1603 | $objDrawing->setResizeProportional(false); |
||
1604 | $objDrawing->setWidth(\PhpSpreadsheet\Shared\Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), "cx"))); |
||
1605 | $objDrawing->setHeight(\PhpSpreadsheet\Shared\Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), "cy"))); |
||
1606 | if ($xfrm) { |
||
1607 | $objDrawing->setRotation(\PhpSpreadsheet\Shared\Drawing::angleToDegrees(self::getArrayItem($xfrm->attributes(), "rot"))); |
||
1608 | } |
||
1609 | View Code Duplication | if ($outerShdw) { |
|
1610 | $shadow = $objDrawing->getShadow(); |
||
1611 | $shadow->setVisible(true); |
||
1612 | $shadow->setBlurRadius(\PhpSpreadsheet\Shared\Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), "blurRad"))); |
||
1613 | $shadow->setDistance(\PhpSpreadsheet\Shared\Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), "dist"))); |
||
1614 | $shadow->setDirection(\PhpSpreadsheet\Shared\Drawing::angleToDegrees(self::getArrayItem($outerShdw->attributes(), "dir"))); |
||
1615 | $shadow->setAlignment((string) self::getArrayItem($outerShdw->attributes(), "algn")); |
||
1616 | $shadow->getColor()->setRGB(self::getArrayItem($outerShdw->srgbClr->attributes(), "val")); |
||
1617 | $shadow->setAlpha(self::getArrayItem($outerShdw->srgbClr->alpha->attributes(), "val") / 1000); |
||
1618 | } |
||
1619 | $objDrawing->setWorksheet($docSheet); |
||
1620 | } else { |
||
1621 | // ? Can charts be positioned with a oneCellAnchor ? |
||
1622 | $coordinates = \PhpSpreadsheet\Cell::stringFromColumnIndex((string) $oneCellAnchor->from->col) . ($oneCellAnchor->from->row + 1); |
||
1623 | $offsetX = \PhpSpreadsheet\Shared\Drawing::EMUToPixels($oneCellAnchor->from->colOff); |
||
1624 | $offsetY = \PhpSpreadsheet\Shared\Drawing::EMUToPixels($oneCellAnchor->from->rowOff); |
||
1625 | $width = \PhpSpreadsheet\Shared\Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), "cx")); |
||
1626 | $height = \PhpSpreadsheet\Shared\Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), "cy")); |
||
1627 | } |
||
1628 | } |
||
1629 | } |
||
1630 | if ($xmlDrawing->twoCellAnchor) { |
||
1631 | foreach ($xmlDrawing->twoCellAnchor as $twoCellAnchor) { |
||
1632 | if ($twoCellAnchor->pic->blipFill) { |
||
1633 | $blip = $twoCellAnchor->pic->blipFill->children("http://schemas.openxmlformats.org/drawingml/2006/main")->blip; |
||
1634 | $xfrm = $twoCellAnchor->pic->spPr->children("http://schemas.openxmlformats.org/drawingml/2006/main")->xfrm; |
||
1635 | $outerShdw = $twoCellAnchor->pic->spPr->children("http://schemas.openxmlformats.org/drawingml/2006/main")->effectLst->outerShdw; |
||
1636 | $objDrawing = new \PhpSpreadsheet\Worksheet\Drawing; |
||
1637 | $objDrawing->setName((string) self::getArrayItem($twoCellAnchor->pic->nvPicPr->cNvPr->attributes(), "name")); |
||
1638 | $objDrawing->setDescription((string) self::getArrayItem($twoCellAnchor->pic->nvPicPr->cNvPr->attributes(), "descr")); |
||
1639 | $objDrawing->setPath( |
||
1640 | "zip://".\PhpSpreadsheet\Shared\File::realpath($pFilename)."#" . |
||
1641 | $images[(string) self::getArrayItem( |
||
1642 | $blip->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), |
||
1643 | "embed" |
||
1644 | )], |
||
1645 | false |
||
1646 | ); |
||
1647 | $objDrawing->setCoordinates(\PhpSpreadsheet\Cell::stringFromColumnIndex((string) $twoCellAnchor->from->col) . ($twoCellAnchor->from->row + 1)); |
||
1648 | $objDrawing->setOffsetX(\PhpSpreadsheet\Shared\Drawing::EMUToPixels($twoCellAnchor->from->colOff)); |
||
1649 | $objDrawing->setOffsetY(\PhpSpreadsheet\Shared\Drawing::EMUToPixels($twoCellAnchor->from->rowOff)); |
||
1650 | $objDrawing->setResizeProportional(false); |
||
1651 | |||
1652 | if ($xfrm) { |
||
1653 | $objDrawing->setWidth(\PhpSpreadsheet\Shared\Drawing::EMUToPixels(self::getArrayItem($xfrm->ext->attributes(), "cx"))); |
||
1654 | $objDrawing->setHeight(\PhpSpreadsheet\Shared\Drawing::EMUToPixels(self::getArrayItem($xfrm->ext->attributes(), "cy"))); |
||
1655 | $objDrawing->setRotation(\PhpSpreadsheet\Shared\Drawing::angleToDegrees(self::getArrayItem($xfrm->attributes(), "rot"))); |
||
1656 | } |
||
1657 | View Code Duplication | if ($outerShdw) { |
|
1658 | $shadow = $objDrawing->getShadow(); |
||
1659 | $shadow->setVisible(true); |
||
1660 | $shadow->setBlurRadius(\PhpSpreadsheet\Shared\Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), "blurRad"))); |
||
1661 | $shadow->setDistance(\PhpSpreadsheet\Shared\Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), "dist"))); |
||
1662 | $shadow->setDirection(\PhpSpreadsheet\Shared\Drawing::angleToDegrees(self::getArrayItem($outerShdw->attributes(), "dir"))); |
||
1663 | $shadow->setAlignment((string) self::getArrayItem($outerShdw->attributes(), "algn")); |
||
1664 | $shadow->getColor()->setRGB(self::getArrayItem($outerShdw->srgbClr->attributes(), "val")); |
||
1665 | $shadow->setAlpha(self::getArrayItem($outerShdw->srgbClr->alpha->attributes(), "val") / 1000); |
||
1666 | } |
||
1667 | $objDrawing->setWorksheet($docSheet); |
||
1668 | } elseif (($this->includeCharts) && ($twoCellAnchor->graphicFrame)) { |
||
1669 | $fromCoordinate = \PhpSpreadsheet\Cell::stringFromColumnIndex((string) $twoCellAnchor->from->col) . ($twoCellAnchor->from->row + 1); |
||
1670 | $fromOffsetX = \PhpSpreadsheet\Shared\Drawing::EMUToPixels($twoCellAnchor->from->colOff); |
||
1671 | $fromOffsetY = \PhpSpreadsheet\Shared\Drawing::EMUToPixels($twoCellAnchor->from->rowOff); |
||
1672 | $toCoordinate = \PhpSpreadsheet\Cell::stringFromColumnIndex((string) $twoCellAnchor->to->col) . ($twoCellAnchor->to->row + 1); |
||
1673 | $toOffsetX = \PhpSpreadsheet\Shared\Drawing::EMUToPixels($twoCellAnchor->to->colOff); |
||
1674 | $toOffsetY = \PhpSpreadsheet\Shared\Drawing::EMUToPixels($twoCellAnchor->to->rowOff); |
||
1675 | $graphic = $twoCellAnchor->graphicFrame->children("http://schemas.openxmlformats.org/drawingml/2006/main")->graphic; |
||
1676 | $chartRef = $graphic->graphicData->children("http://schemas.openxmlformats.org/drawingml/2006/chart")->chart; |
||
1677 | $thisChart = (string) $chartRef->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"); |
||
1678 | |||
1679 | $chartDetails[$docSheet->getTitle().'!'.$thisChart] = array( |
||
1680 | 'fromCoordinate' => $fromCoordinate, |
||
1681 | 'fromOffsetX' => $fromOffsetX, |
||
1682 | 'fromOffsetY' => $fromOffsetY, |
||
1683 | 'toCoordinate' => $toCoordinate, |
||
1684 | 'toOffsetX' => $toOffsetX, |
||
1685 | 'toOffsetY' => $toOffsetY, |
||
1686 | 'worksheetTitle' => $docSheet->getTitle() |
||
1687 | ); |
||
1688 | } |
||
1689 | } |
||
1690 | } |
||
1691 | } |
||
1692 | } |
||
1693 | } |
||
1694 | |||
1695 | // Loop through definedNames |
||
1696 | if ($xmlWorkbook->definedNames) { |
||
1697 | foreach ($xmlWorkbook->definedNames->definedName as $definedName) { |
||
1698 | // Extract range |
||
1699 | $extractedRange = (string)$definedName; |
||
1700 | $extractedRange = preg_replace('/\'(\w+)\'\!/', '', $extractedRange); |
||
1701 | View Code Duplication | if (($spos = strpos($extractedRange, '!')) !== false) { |
|
1702 | $extractedRange = substr($extractedRange, 0, $spos).str_replace('$', '', substr($extractedRange, $spos)); |
||
1703 | } else { |
||
1704 | $extractedRange = str_replace('$', '', $extractedRange); |
||
1705 | } |
||
1706 | |||
1707 | // Valid range? |
||
1708 | if (stripos((string)$definedName, '#REF!') !== false || $extractedRange == '') { |
||
1709 | continue; |
||
1710 | } |
||
1711 | |||
1712 | // Some definedNames are only applicable if we are on the same sheet... |
||
1713 | if ((string)$definedName['localSheetId'] != '' && (string)$definedName['localSheetId'] == $sheetId) { |
||
1714 | // Switch on type |
||
1715 | switch ((string)$definedName['name']) { |
||
1716 | case '_xlnm._FilterDatabase': |
||
1717 | if ((string)$definedName['hidden'] !== '1') { |
||
1718 | $extractedRange = explode(',', $extractedRange); |
||
1719 | foreach ($extractedRange as $range) { |
||
1720 | $autoFilterRange = $range; |
||
1721 | if (strpos($autoFilterRange, ':') !== false) { |
||
1722 | $docSheet->getAutoFilter()->setRange($autoFilterRange); |
||
1723 | } |
||
1724 | } |
||
1725 | } |
||
1726 | break; |
||
1727 | case '_xlnm.Print_Titles': |
||
1728 | // Split $extractedRange |
||
1729 | $extractedRange = explode(',', $extractedRange); |
||
1730 | |||
1731 | // Set print titles |
||
1732 | foreach ($extractedRange as $range) { |
||
1733 | $matches = array(); |
||
1734 | $range = str_replace('$', '', $range); |
||
1735 | |||
1736 | // check for repeating columns, e g. 'A:A' or 'A:D' |
||
1737 | if (preg_match('/!?([A-Z]+)\:([A-Z]+)$/', $range, $matches)) { |
||
1738 | $docSheet->getPageSetup()->setColumnsToRepeatAtLeft(array($matches[1], $matches[2])); |
||
1739 | } elseif (preg_match('/!?(\d+)\:(\d+)$/', $range, $matches)) { |
||
1740 | // check for repeating rows, e.g. '1:1' or '1:5' |
||
1741 | $docSheet->getPageSetup()->setRowsToRepeatAtTop(array($matches[1], $matches[2])); |
||
1742 | } |
||
1743 | } |
||
1744 | break; |
||
1745 | case '_xlnm.Print_Area': |
||
1746 | $rangeSets = explode(',', $extractedRange); // FIXME: what if sheetname contains comma? |
||
1747 | $newRangeSets = array(); |
||
1748 | foreach ($rangeSets as $rangeSet) { |
||
1749 | $range = explode('!', $rangeSet); // FIXME: what if sheetname contains exclamation mark? |
||
1750 | $rangeSet = isset($range[1]) ? $range[1] : $range[0]; |
||
1751 | if (strpos($rangeSet, ':') === false) { |
||
1752 | $rangeSet = $rangeSet . ':' . $rangeSet; |
||
1753 | } |
||
1754 | $newRangeSets[] = str_replace('$', '', $rangeSet); |
||
1755 | } |
||
1756 | $docSheet->getPageSetup()->setPrintArea(implode(',', $newRangeSets)); |
||
1757 | break; |
||
1758 | |||
1759 | default: |
||
1760 | break; |
||
1761 | } |
||
1762 | } |
||
1763 | } |
||
1764 | } |
||
1765 | |||
1766 | // Next sheet id |
||
1767 | ++$sheetId; |
||
1768 | } |
||
1769 | |||
1770 | // Loop through definedNames |
||
1771 | if ($xmlWorkbook->definedNames) { |
||
1772 | foreach ($xmlWorkbook->definedNames->definedName as $definedName) { |
||
1773 | // Extract range |
||
1774 | $extractedRange = (string)$definedName; |
||
1775 | $extractedRange = preg_replace('/\'(\w+)\'\!/', '', $extractedRange); |
||
1776 | View Code Duplication | if (($spos = strpos($extractedRange, '!')) !== false) { |
|
1777 | $extractedRange = substr($extractedRange, 0, $spos).str_replace('$', '', substr($extractedRange, $spos)); |
||
1778 | } else { |
||
1779 | $extractedRange = str_replace('$', '', $extractedRange); |
||
1780 | } |
||
1781 | |||
1782 | // Valid range? |
||
1783 | if (stripos((string)$definedName, '#REF!') !== false || $extractedRange == '') { |
||
1784 | continue; |
||
1785 | } |
||
1786 | |||
1787 | // Some definedNames are only applicable if we are on the same sheet... |
||
1788 | if ((string)$definedName['localSheetId'] != '') { |
||
1789 | // Local defined name |
||
1790 | // Switch on type |
||
1791 | switch ((string)$definedName['name']) { |
||
1792 | case '_xlnm._FilterDatabase': |
||
1793 | case '_xlnm.Print_Titles': |
||
1794 | case '_xlnm.Print_Area': |
||
1795 | break; |
||
1796 | default: |
||
1797 | if ($mapSheetId[(integer) $definedName['localSheetId']] !== null) { |
||
1798 | $range = explode('!', (string)$definedName); |
||
1799 | if (count($range) == 2) { |
||
1800 | $range[0] = str_replace("''", "'", $range[0]); |
||
1801 | $range[0] = str_replace("'", "", $range[0]); |
||
1802 | if ($worksheet = $docSheet->getParent()->getSheetByName($range[0])) { |
||
1803 | $extractedRange = str_replace('$', '', $range[1]); |
||
1804 | $scope = $docSheet->getParent()->getSheet($mapSheetId[(integer) $definedName['localSheetId']]); |
||
1805 | $excel->addNamedRange(new \PhpSpreadsheet\NamedRange((string)$definedName['name'], $worksheet, $extractedRange, true, $scope)); |
||
1806 | } |
||
1807 | } |
||
1808 | } |
||
1809 | break; |
||
1810 | } |
||
1811 | } elseif (!isset($definedName['localSheetId'])) { |
||
1812 | // "Global" definedNames |
||
1813 | $locatedSheet = null; |
||
1814 | $extractedSheetName = ''; |
||
1815 | if (strpos((string)$definedName, '!') !== false) { |
||
1816 | // Extract sheet name |
||
1817 | $extractedSheetName = \PhpSpreadsheet\Worksheet::extractSheetTitle((string)$definedName, true); |
||
1818 | $extractedSheetName = $extractedSheetName[0]; |
||
1819 | |||
1820 | // Locate sheet |
||
1821 | $locatedSheet = $excel->getSheetByName($extractedSheetName); |
||
1822 | |||
1823 | // Modify range |
||
1824 | $range = explode('!', $extractedRange); |
||
1825 | $extractedRange = isset($range[1]) ? $range[1] : $range[0]; |
||
1826 | } |
||
1827 | |||
1828 | if ($locatedSheet !== null) { |
||
1829 | $excel->addNamedRange(new \PhpSpreadsheet\NamedRange((string)$definedName['name'], $locatedSheet, $extractedRange, false)); |
||
1830 | } |
||
1831 | } |
||
1832 | } |
||
1833 | } |
||
1834 | } |
||
1835 | |||
1836 | if ((!$this->readDataOnly) || (!empty($this->loadSheetsOnly))) { |
||
1837 | // active sheet index |
||
1838 | $activeTab = intval($xmlWorkbook->bookViews->workbookView["activeTab"]); // refers to old sheet index |
||
1839 | |||
1840 | // keep active sheet index if sheet is still loaded, else first sheet is set as the active |
||
1841 | if (isset($mapSheetId[$activeTab]) && $mapSheetId[$activeTab] !== null) { |
||
1842 | $excel->setActiveSheetIndex($mapSheetId[$activeTab]); |
||
1843 | } else { |
||
1844 | if ($excel->getSheetCount() == 0) { |
||
1845 | $excel->createSheet(); |
||
1846 | } |
||
1847 | $excel->setActiveSheetIndex(0); |
||
1848 | } |
||
1849 | } |
||
1850 | break; |
||
1851 | } |
||
1852 | } |
||
1853 | |||
1854 | if (!$this->readDataOnly) { |
||
1855 | $contentTypes = simplexml_load_string( |
||
1856 | $this->securityScan( |
||
1857 | $this->getFromZipArchive($zip, "[Content_Types].xml") |
||
1858 | ), |
||
1859 | 'SimpleXMLElement', |
||
1860 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1861 | ); |
||
1862 | foreach ($contentTypes->Override as $contentType) { |
||
1863 | switch ($contentType["ContentType"]) { |
||
1864 | case "application/vnd.openxmlformats-officedocument.drawingml.chart+xml": |
||
1865 | if ($this->includeCharts) { |
||
1866 | $chartEntryRef = ltrim($contentType['PartName'], '/'); |
||
1867 | $chartElements = simplexml_load_string( |
||
1868 | $this->securityScan( |
||
1869 | $this->getFromZipArchive($zip, $chartEntryRef) |
||
1870 | ), |
||
1871 | 'SimpleXMLElement', |
||
1872 | \PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
||
1873 | ); |
||
1874 | $objChart = \PhpSpreadsheet\Reader\Excel2007\Chart::readChart($chartElements, basename($chartEntryRef, '.xml')); |
||
1875 | |||
1876 | // echo 'Chart ', $chartEntryRef, '<br />'; |
||
1877 | // var_dump($charts[$chartEntryRef]); |
||
1878 | // |
||
1879 | if (isset($charts[$chartEntryRef])) { |
||
1880 | $chartPositionRef = $charts[$chartEntryRef]['sheet'].'!'.$charts[$chartEntryRef]['id']; |
||
1881 | // echo 'Position Ref ', $chartPositionRef, '<br />'; |
||
1882 | if (isset($chartDetails[$chartPositionRef])) { |
||
1883 | // var_dump($chartDetails[$chartPositionRef]); |
||
1884 | |||
1885 | $excel->getSheetByName($charts[$chartEntryRef]['sheet'])->addChart($objChart); |
||
1886 | $objChart->setWorksheet($excel->getSheetByName($charts[$chartEntryRef]['sheet'])); |
||
1887 | $objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']); |
||
1888 | $objChart->setBottomRightPosition($chartDetails[$chartPositionRef]['toCoordinate'], $chartDetails[$chartPositionRef]['toOffsetX'], $chartDetails[$chartPositionRef]['toOffsetY']); |
||
1889 | } |
||
1890 | } |
||
1891 | } |
||
1892 | } |
||
1893 | } |
||
1894 | } |
||
1895 | |||
1896 | $zip->close(); |
||
1897 | |||
1898 | return $excel; |
||
1899 | } |
||
1900 | |||
2217 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.