27
27
use PhpOffice \PhpSpreadsheet \Shared \Font ;
28
28
use PhpOffice \PhpSpreadsheet \Shared \StringHelper ;
29
29
use PhpOffice \PhpSpreadsheet \Spreadsheet ;
30
- use PhpOffice \PhpSpreadsheet \Style \Border ;
31
- use PhpOffice \PhpSpreadsheet \Style \Borders ;
32
30
use PhpOffice \PhpSpreadsheet \Style \Color ;
33
- use PhpOffice \PhpSpreadsheet \Style \Fill ;
34
31
use PhpOffice \PhpSpreadsheet \Style \NumberFormat ;
35
- use PhpOffice \PhpSpreadsheet \Style \Protection ;
36
32
use PhpOffice \PhpSpreadsheet \Style \Style ;
37
33
use PhpOffice \PhpSpreadsheet \Worksheet \HeaderFooterDrawing ;
38
34
use PhpOffice \PhpSpreadsheet \Worksheet \Worksheet ;
@@ -1570,31 +1566,6 @@ public function load($pFilename)
1570
1566
return $ excel ;
1571
1567
}
1572
1568
1573
- private static function readColor ($ color , $ background = false )
1574
- {
1575
- if (isset ($ color ['rgb ' ])) {
1576
- return (string ) $ color ['rgb ' ];
1577
- } elseif (isset ($ color ['indexed ' ])) {
1578
- return Color::indexedColor ($ color ['indexed ' ] - 7 , $ background )->getARGB ();
1579
- } elseif (isset ($ color ['theme ' ])) {
1580
- if (self ::$ theme !== null ) {
1581
- $ returnColour = self ::$ theme ->getColourByIndex ((int ) $ color ['theme ' ]);
1582
- if (isset ($ color ['tint ' ])) {
1583
- $ tintAdjust = (float ) $ color ['tint ' ];
1584
- $ returnColour = Color::changeBrightness ($ returnColour , $ tintAdjust );
1585
- }
1586
-
1587
- return 'FF ' . $ returnColour ;
1588
- }
1589
- }
1590
-
1591
- if ($ background ) {
1592
- return 'FFFFFFFF ' ;
1593
- }
1594
-
1595
- return 'FF000000 ' ;
1596
- }
1597
-
1598
1569
/**
1599
1570
* @param SimpleXMLElement|stdClass $style
1600
1571
*/
@@ -1604,116 +1575,28 @@ private static function readStyle(Style $docStyle, $style): void
1604
1575
1605
1576
// font
1606
1577
if (isset ($ style ->font )) {
1607
- $ docStyle ->getFont ()->setName ((string ) $ style ->font ->name ['val ' ]);
1608
- $ docStyle ->getFont ()->setSize ((string ) $ style ->font ->sz ['val ' ]);
1609
- if (isset ($ style ->font ->b )) {
1610
- $ docStyle ->getFont ()->setBold (!isset ($ style ->font ->b ['val ' ]) || self ::boolean ((string ) $ style ->font ->b ['val ' ]));
1611
- }
1612
- if (isset ($ style ->font ->i )) {
1613
- $ docStyle ->getFont ()->setItalic (!isset ($ style ->font ->i ['val ' ]) || self ::boolean ((string ) $ style ->font ->i ['val ' ]));
1614
- }
1615
- if (isset ($ style ->font ->strike )) {
1616
- $ docStyle ->getFont ()->setStrikethrough (!isset ($ style ->font ->strike ['val ' ]) || self ::boolean ((string ) $ style ->font ->strike ['val ' ]));
1617
- }
1618
- $ docStyle ->getFont ()->getColor ()->setARGB (self ::readColor ($ style ->font ->color ));
1619
-
1620
- if (isset ($ style ->font ->u ) && !isset ($ style ->font ->u ['val ' ])) {
1621
- $ docStyle ->getFont ()->setUnderline (\PhpOffice \PhpSpreadsheet \Style \Font::UNDERLINE_SINGLE );
1622
- } elseif (isset ($ style ->font ->u , $ style ->font ->u ['val ' ])) {
1623
- $ docStyle ->getFont ()->setUnderline ((string ) $ style ->font ->u ['val ' ]);
1624
- }
1625
-
1626
- if (isset ($ style ->font ->vertAlign , $ style ->font ->vertAlign ['val ' ])) {
1627
- $ vertAlign = strtolower ((string ) $ style ->font ->vertAlign ['val ' ]);
1628
- if ($ vertAlign == 'superscript ' ) {
1629
- $ docStyle ->getFont ()->setSuperscript (true );
1630
- }
1631
- if ($ vertAlign == 'subscript ' ) {
1632
- $ docStyle ->getFont ()->setSubscript (true );
1633
- }
1634
- }
1578
+ Styles::readFontStyle ($ docStyle ->getFont (), $ style ->font );
1635
1579
}
1636
1580
1637
1581
// fill
1638
1582
if (isset ($ style ->fill )) {
1639
- if ($ style ->fill ->gradientFill ) {
1640
- /** @var SimpleXMLElement $gradientFill */
1641
- $ gradientFill = $ style ->fill ->gradientFill [0 ];
1642
- if (!empty ($ gradientFill ['type ' ])) {
1643
- $ docStyle ->getFill ()->setFillType ((string ) $ gradientFill ['type ' ]);
1644
- }
1645
- $ docStyle ->getFill ()->setRotation ((float ) ($ gradientFill ['degree ' ]));
1646
- $ gradientFill ->registerXPathNamespace ('sml ' , 'http://schemas.openxmlformats.org/spreadsheetml/2006/main ' );
1647
- $ docStyle ->getFill ()->getStartColor ()->setARGB (self ::readColor (self ::getArrayItem ($ gradientFill ->xpath ('sml:stop[@position=0] ' ))->color ));
1648
- $ docStyle ->getFill ()->getEndColor ()->setARGB (self ::readColor (self ::getArrayItem ($ gradientFill ->xpath ('sml:stop[@position=1] ' ))->color ));
1649
- } elseif ($ style ->fill ->patternFill ) {
1650
- $ patternType = (string ) $ style ->fill ->patternFill ['patternType ' ] != '' ? (string ) $ style ->fill ->patternFill ['patternType ' ] : Fill::FILL_NONE ;
1651
- $ docStyle ->getFill ()->setFillType ($ patternType );
1652
- if ($ style ->fill ->patternFill ->fgColor ) {
1653
- $ docStyle ->getFill ()->getStartColor ()->setARGB (self ::readColor ($ style ->fill ->patternFill ->fgColor , true ));
1654
- }
1655
- if ($ style ->fill ->patternFill ->bgColor ) {
1656
- $ docStyle ->getFill ()->getEndColor ()->setARGB (self ::readColor ($ style ->fill ->patternFill ->bgColor , true ));
1657
- }
1658
- }
1583
+ Styles::readFillStyle ($ docStyle ->getFill (), $ style ->fill );
1659
1584
}
1660
1585
1661
1586
// border
1662
1587
if (isset ($ style ->border )) {
1663
- $ diagonalUp = self ::boolean ((string ) $ style ->border ['diagonalUp ' ]);
1664
- $ diagonalDown = self ::boolean ((string ) $ style ->border ['diagonalDown ' ]);
1665
- if (!$ diagonalUp && !$ diagonalDown ) {
1666
- $ docStyle ->getBorders ()->setDiagonalDirection (Borders::DIAGONAL_NONE );
1667
- } elseif ($ diagonalUp && !$ diagonalDown ) {
1668
- $ docStyle ->getBorders ()->setDiagonalDirection (Borders::DIAGONAL_UP );
1669
- } elseif (!$ diagonalUp && $ diagonalDown ) {
1670
- $ docStyle ->getBorders ()->setDiagonalDirection (Borders::DIAGONAL_DOWN );
1671
- } else {
1672
- $ docStyle ->getBorders ()->setDiagonalDirection (Borders::DIAGONAL_BOTH );
1673
- }
1674
- self ::readBorder ($ docStyle ->getBorders ()->getLeft (), $ style ->border ->left );
1675
- self ::readBorder ($ docStyle ->getBorders ()->getRight (), $ style ->border ->right );
1676
- self ::readBorder ($ docStyle ->getBorders ()->getTop (), $ style ->border ->top );
1677
- self ::readBorder ($ docStyle ->getBorders ()->getBottom (), $ style ->border ->bottom );
1678
- self ::readBorder ($ docStyle ->getBorders ()->getDiagonal (), $ style ->border ->diagonal );
1588
+ Styles::readBorderStyle ($ docStyle ->getBorders (), $ style ->border );
1679
1589
}
1680
1590
1681
1591
// alignment
1682
1592
if (isset ($ style ->alignment )) {
1683
- $ docStyle ->getAlignment ()->setHorizontal ((string ) $ style ->alignment ['horizontal ' ]);
1684
- $ docStyle ->getAlignment ()->setVertical ((string ) $ style ->alignment ['vertical ' ]);
1685
-
1686
- $ textRotation = 0 ;
1687
- if ((int ) $ style ->alignment ['textRotation ' ] <= 90 ) {
1688
- $ textRotation = (int ) $ style ->alignment ['textRotation ' ];
1689
- } elseif ((int ) $ style ->alignment ['textRotation ' ] > 90 ) {
1690
- $ textRotation = 90 - (int ) $ style ->alignment ['textRotation ' ];
1691
- }
1692
-
1693
- $ docStyle ->getAlignment ()->setTextRotation ((int ) $ textRotation );
1694
- $ docStyle ->getAlignment ()->setWrapText (self ::boolean ((string ) $ style ->alignment ['wrapText ' ]));
1695
- $ docStyle ->getAlignment ()->setShrinkToFit (self ::boolean ((string ) $ style ->alignment ['shrinkToFit ' ]));
1696
- $ docStyle ->getAlignment ()->setIndent ((int ) ((string ) $ style ->alignment ['indent ' ]) > 0 ? (int ) ((string ) $ style ->alignment ['indent ' ]) : 0 );
1697
- $ docStyle ->getAlignment ()->setReadOrder ((int ) ((string ) $ style ->alignment ['readingOrder ' ]) > 0 ? (int ) ((string ) $ style ->alignment ['readingOrder ' ]) : 0 );
1593
+ Styles::readAlignmentStyle ($ docStyle ->getAlignment (), $ style ->alignment );
1698
1594
}
1699
1595
1700
1596
// protection
1701
1597
if (isset ($ style ->protection )) {
1702
- if (isset ($ style ->protection ['locked ' ])) {
1703
- if (self ::boolean ((string ) $ style ->protection ['locked ' ])) {
1704
- $ docStyle ->getProtection ()->setLocked (Protection::PROTECTION_PROTECTED );
1705
- } else {
1706
- $ docStyle ->getProtection ()->setLocked (Protection::PROTECTION_UNPROTECTED );
1707
- }
1708
- }
1709
-
1710
- if (isset ($ style ->protection ['hidden ' ])) {
1711
- if (self ::boolean ((string ) $ style ->protection ['hidden ' ])) {
1712
- $ docStyle ->getProtection ()->setHidden (Protection::PROTECTION_PROTECTED );
1713
- } else {
1714
- $ docStyle ->getProtection ()->setHidden (Protection::PROTECTION_UNPROTECTED );
1715
- }
1716
- }
1598
+ Styles::readProtectionLocked ($ docStyle , $ style ->protection );
1599
+ Styles::readProtectionHidden ($ docStyle , $ style ->protection );
1717
1600
}
1718
1601
1719
1602
// top-level style settings
@@ -1722,19 +1605,6 @@ private static function readStyle(Style $docStyle, $style): void
1722
1605
}
1723
1606
}
1724
1607
1725
- /**
1726
- * @param SimpleXMLElement $eleBorder
1727
- */
1728
- private static function readBorder (Border $ docBorder , $ eleBorder ): void
1729
- {
1730
- if (isset ($ eleBorder ['style ' ])) {
1731
- $ docBorder ->setBorderStyle ((string ) $ eleBorder ['style ' ]);
1732
- }
1733
- if (isset ($ eleBorder ->color )) {
1734
- $ docBorder ->getColor ()->setARGB (self ::readColor ($ eleBorder ->color ));
1735
- }
1736
- }
1737
-
1738
1608
/**
1739
1609
* @param SimpleXMLElement | null $is
1740
1610
*
@@ -1763,7 +1633,7 @@ private function parseRichText(?SimpleXMLElement $is)
1763
1633
$ objText ->getFont ()->setSize ((float ) $ run ->rPr ->sz ['val ' ]);
1764
1634
}
1765
1635
if (isset ($ run ->rPr ->color )) {
1766
- $ objText ->getFont ()->setColor (new Color (self ::readColor ($ run ->rPr ->color )));
1636
+ $ objText ->getFont ()->setColor (new Color (Styles ::readColor ($ run ->rPr ->color )));
1767
1637
}
1768
1638
if (
1769
1639
(isset ($ run ->rPr ->b ['val ' ]) && self ::boolean ((string ) $ run ->rPr ->b ['val ' ])) ||
@@ -1949,11 +1819,17 @@ private function readProtection(Spreadsheet $excel, SimpleXMLElement $xmlWorkboo
1949
1819
}
1950
1820
1951
1821
if ($ xmlWorkbook ->workbookProtection ['revisionsPassword ' ]) {
1952
- $ excel ->getSecurity ()->setRevisionsPassword ((string ) $ xmlWorkbook ->workbookProtection ['revisionsPassword ' ], true );
1822
+ $ excel ->getSecurity ()->setRevisionsPassword (
1823
+ (string ) $ xmlWorkbook ->workbookProtection ['revisionsPassword ' ],
1824
+ true
1825
+ );
1953
1826
}
1954
1827
1955
1828
if ($ xmlWorkbook ->workbookProtection ['workbookPassword ' ]) {
1956
- $ excel ->getSecurity ()->setWorkbookPassword ((string ) $ xmlWorkbook ->workbookProtection ['workbookPassword ' ], true );
1829
+ $ excel ->getSecurity ()->setWorkbookPassword (
1830
+ (string ) $ xmlWorkbook ->workbookProtection ['workbookPassword ' ],
1831
+ true
1832
+ );
1957
1833
}
1958
1834
}
1959
1835
0 commit comments