| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Reader\Xlsx; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 6 |
use SimpleXMLElement; |
| 7 |
|
| 8 |
class SheetViewOptions extends BaseParserClass |
| 9 |
{ |
| 10 |
private Worksheet $worksheet; |
| 11 |
|
| 12 |
private ?SimpleXMLElement $worksheetXml; |
| 13 |
|
| 14 |
public function __construct(Worksheet $workSheet, ?SimpleXMLElement $worksheetXml = null) |
| 15 |
{ |
| 16 |
$this->worksheet = $workSheet; |
| 17 |
$this->worksheetXml = $worksheetXml; |
| 18 |
} |
| 19 |
|
| 20 |
public function load(bool $readDataOnly, Styles $styleReader): void |
| 21 |
{ |
| 22 |
if ($this->worksheetXml === null) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
if (isset($this->worksheetXml->sheetPr)) { |
| 27 |
$sheetPr = $this->worksheetXml->sheetPr; |
| 28 |
$this->tabColor($sheetPr, $styleReader); |
| 29 |
$this->codeName($sheetPr); |
| 30 |
$this->outlines($sheetPr); |
| 31 |
$this->pageSetup($sheetPr); |
| 32 |
} |
| 33 |
|
| 34 |
if (isset($this->worksheetXml->sheetFormatPr)) { |
| 35 |
$this->sheetFormat($this->worksheetXml->sheetFormatPr); |
| 36 |
} |
| 37 |
|
| 38 |
if (!$readDataOnly && isset($this->worksheetXml->printOptions)) { |
| 39 |
$this->printOptions($this->worksheetXml->printOptions); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
private function tabColor(SimpleXMLElement $sheetPr, Styles $styleReader): void |
| 44 |
{ |
| 45 |
if (isset($sheetPr->tabColor)) { |
| 46 |
$this->worksheet->getTabColor()->setARGB($styleReader->readColor($sheetPr->tabColor)); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
private function codeName(SimpleXMLElement $sheetPrx): void |
| 51 |
{ |
| 52 |
$sheetPr = $sheetPrx->attributes() ?? []; |
| 53 |
if (isset($sheetPr['codeName'])) { |
| 54 |
$this->worksheet->setCodeName((string) $sheetPr['codeName'], false); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
private function outlines(SimpleXMLElement $sheetPr): void |
| 59 |
{ |
| 60 |
if (isset($sheetPr->outlinePr)) { |
| 61 |
$attr = $sheetPr->outlinePr->attributes() ?? []; |
| 62 |
if ( |
| 63 |
isset($attr['summaryRight']) |
| 64 |
&& !self::boolean((string) $attr['summaryRight']) |
| 65 |
) { |
| 66 |
$this->worksheet->setShowSummaryRight(false); |
| 67 |
} else { |
| 68 |
$this->worksheet->setShowSummaryRight(true); |
| 69 |
} |
| 70 |
|
| 71 |
if ( |
| 72 |
isset($attr['summaryBelow']) |
| 73 |
&& !self::boolean((string) $attr['summaryBelow']) |
| 74 |
) { |
| 75 |
$this->worksheet->setShowSummaryBelow(false); |
| 76 |
} else { |
| 77 |
$this->worksheet->setShowSummaryBelow(true); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
private function pageSetup(SimpleXMLElement $sheetPr): void |
| 83 |
{ |
| 84 |
if (isset($sheetPr->pageSetUpPr)) { |
| 85 |
$attr = $sheetPr->pageSetUpPr->attributes() ?? []; |
| 86 |
if ( |
| 87 |
isset($attr['fitToPage']) |
| 88 |
&& !self::boolean((string) $attr['fitToPage']) |
| 89 |
) { |
| 90 |
$this->worksheet->getPageSetup()->setFitToPage(false); |
| 91 |
} else { |
| 92 |
$this->worksheet->getPageSetup()->setFitToPage(true); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
private function sheetFormat(SimpleXMLElement $sheetFormatPrx): void |
| 98 |
{ |
| 99 |
$sheetFormatPr = $sheetFormatPrx->attributes() ?? []; |
| 100 |
if ( |
| 101 |
isset($sheetFormatPr['customHeight']) |
| 102 |
&& self::boolean((string) $sheetFormatPr['customHeight']) |
| 103 |
&& isset($sheetFormatPr['defaultRowHeight']) |
| 104 |
) { |
| 105 |
$this->worksheet->getDefaultRowDimension() |
| 106 |
->setRowHeight((float) $sheetFormatPr['defaultRowHeight']); |
| 107 |
} |
| 108 |
|
| 109 |
if (isset($sheetFormatPr['defaultColWidth'])) { |
| 110 |
$this->worksheet->getDefaultColumnDimension() |
| 111 |
->setWidth((float) $sheetFormatPr['defaultColWidth']); |
| 112 |
} |
| 113 |
|
| 114 |
if ( |
| 115 |
isset($sheetFormatPr['zeroHeight']) |
| 116 |
&& ((string) $sheetFormatPr['zeroHeight'] === '1') |
| 117 |
) { |
| 118 |
$this->worksheet->getDefaultRowDimension()->setZeroHeight(true); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
private function printOptions(SimpleXMLElement $printOptionsx): void |
| 123 |
{ |
| 124 |
$printOptions = $printOptionsx->attributes() ?? []; |
| 125 |
// Spec is weird. gridLines (default false) |
| 126 |
// and gridLinesSet (default true) must both be true. |
| 127 |
if (isset($printOptions['gridLines']) && self::boolean((string) $printOptions['gridLines'])) { |
| 128 |
if (!isset($printOptions['gridLinesSet']) || self::boolean((string) $printOptions['gridLinesSet'])) { |
| 129 |
$this->worksheet->setPrintGridlines(true); |
| 130 |
} |
| 131 |
} |
| 132 |
if (isset($printOptions['horizontalCentered']) && self::boolean((string) $printOptions['horizontalCentered'])) { |
| 133 |
$this->worksheet->getPageSetup()->setHorizontalCentered(true); |
| 134 |
} |
| 135 |
if (isset($printOptions['verticalCentered']) && self::boolean((string) $printOptions['verticalCentered'])) { |
| 136 |
$this->worksheet->getPageSetup()->setVerticalCentered(true); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|