diff --git a/src/PhpSpreadsheet/Reader/Gnumeric.php b/src/PhpSpreadsheet/Reader/Gnumeric.php index 8375b37e..32105477 100644 --- a/src/PhpSpreadsheet/Reader/Gnumeric.php +++ b/src/PhpSpreadsheet/Reader/Gnumeric.php @@ -17,6 +17,7 @@ use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Borders; use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\Font; +use PhpOffice\PhpSpreadsheet\Worksheet\PageMargins; use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use SimpleXMLElement; @@ -24,6 +25,8 @@ use XMLReader; class Gnumeric extends BaseReader { + private const UOM_CONVERSION_POINTS_TO_CENTIMETERS = 0.03527777778; + /** * Shared Expressions. * @@ -402,6 +405,72 @@ class Gnumeric extends BaseReader } } + private function printInformation(SimpleXMLElement $sheet): void + { + if (!$this->readDataOnly && isset($sheet->PrintInformation)) { + $printInformation = $sheet->PrintInformation[0]; + $scale = (string) $printInformation->Scale->attributes()['percentage']; + $pageOrder = (string) $printInformation->order; + $orientation = (string) $printInformation->orientation; + $horizontalCentered = (string) $printInformation->hcenter->attributes()['value']; + $verticalCentered = (string) $printInformation->vcenter->attributes()['value']; + + $this->spreadsheet->getActiveSheet()->getPageSetup() + ->setPageOrder($pageOrder === 'r_then_d' ? PageSetup::PAGEORDER_OVER_THEN_DOWN : PageSetup::PAGEORDER_DOWN_THEN_OVER) + ->setScale((int) $scale) + ->setOrientation($orientation ?? PageSetup::ORIENTATION_DEFAULT) + ->setHorizontalCentered((bool) $horizontalCentered) + ->setVerticalCentered((bool) $verticalCentered); + } + } + + private function sheetMargins(SimpleXMLElement $sheet): void + { + if (!$this->readDataOnly && isset($sheet->PrintInformation, $sheet->PrintInformation->Margins)) { + $marginSet = [ + // Default Settings + 'top' => 0.75, + 'header' => 0.3, + 'left' => 0.7, + 'right' => 0.7, + 'bottom' => 0.75, + 'footer' => 0.3, + ]; + + foreach ($sheet->PrintInformation->Margins->children($this->gnm, true) as $key => $margin) { + $marginAttributes = $margin->attributes(); + $marginSize = ($marginAttributes['Points']) ?? 72; // Default is 72pt + // Convert value in points to inches + $marginSize = PageMargins::fromPoints((float) $marginSize); + $marginSet[$key] = $marginSize; + } + + foreach ($marginSet as $key => $marginSize) { + // Gnumeric is quirky in the way it displays the header/footer values: + // header is actually the sum of top and header; footer is actually the sum of bottom and footer + // then top is actually the header value, and bottom is actually the footer value + switch ($key) { + case 'left': + case 'right': + $this->sheetMargin($key, $marginSize); + break; + case 'top': + $this->sheetMargin($key, $marginSet['header'] ?? 0); + break; + case 'bottom': + $this->sheetMargin($key, $marginSet['footer'] ?? 0); + break; + case 'header': + $this->sheetMargin($key, ($marginSet['top'] ?? 0) - $marginSize); + break; + case 'footer': + $this->sheetMargin($key, ($marginSet['bottom'] ?? 0) - $marginSize); + break; + } + } + } + } + private function sheetMargin(string $key, float $marginSize): void { switch ($key) { @@ -432,43 +501,6 @@ class Gnumeric extends BaseReader } } - private function printInformation(SimpleXMLElement $sheet): void - { - if (!$this->readDataOnly && isset($sheet->PrintInformation)) { - $printInformation = $sheet->PrintInformation[0]; - $scale = (string) $printInformation->Scale->attributes()['percentage']; - $pageOrder = (string) $printInformation->order; - $orientation = (string) $printInformation->orientation; - $horizontalCentered = (string) $printInformation->hcenter->attributes()['value']; - $verticalCentered = (string) $printInformation->vcenter->attributes()['value']; - - $this->spreadsheet->getActiveSheet()->getPageSetup() - ->setPageOrder($pageOrder === 'r_then_d' ? PageSetup::PAGEORDER_OVER_THEN_DOWN : PageSetup::PAGEORDER_DOWN_THEN_OVER) - ->setScale((int) $scale) - ->setOrientation($orientation ?? PageSetup::ORIENTATION_DEFAULT) - ->setHorizontalCentered((bool) $horizontalCentered) - ->setVerticalCentered((bool) $verticalCentered); - } - } - - private function sheetMargins(SimpleXMLElement $sheet): void - { - if (!$this->readDataOnly && isset($sheet->PrintInformation, $sheet->PrintInformation->Margins)) { - foreach ($sheet->PrintInformation->Margins->children($this->gnm, true) as $key => $margin) { - $marginAttributes = $margin->attributes(); - $marginSize = $marginAttributes['Points'] ?? 72 / 100; // Default - switch ($marginAttributes['PrefUnit']) { - case 'inch': - case 'mm': - $marginSize = ($marginAttributes['Points']) / 100; - - break; - } - $this->sheetMargin($key, (float) $marginSize); - } - } - } - private function processComments(SimpleXMLElement $sheet): void { if ((!$this->readDataOnly) && (isset($sheet->Objects))) { diff --git a/src/PhpSpreadsheet/Reader/Ods/PageSettings.php b/src/PhpSpreadsheet/Reader/Ods/PageSettings.php index 05095752..77341aab 100644 --- a/src/PhpSpreadsheet/Reader/Ods/PageSettings.php +++ b/src/PhpSpreadsheet/Reader/Ods/PageSettings.php @@ -47,6 +47,7 @@ class PageSettings $styleScale = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'scale-to'); $stylePrintOrder = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-page-order'); $centered = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'table-centering'); + $marginLeft = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-left'); $marginRight = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-right'); $marginTop = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-top'); @@ -59,17 +60,18 @@ class PageSettings $marginFooter = $footerProperties->getAttributeNS($this->stylesFo, 'min-height'); $this->pageLayoutStyles[$styleName] = (object) [ - 'orientation' => $styleOrientation, - 'scale' => $styleScale, + 'orientation' => $styleOrientation ?: PageSetup::ORIENTATION_DEFAULT, + 'scale' => $styleScale ?: 100, 'printOrder' => $stylePrintOrder, 'horizontalCentered' => $centered === 'horizontal' || $centered === 'both', 'verticalCentered' => $centered === 'vertical' || $centered === 'both', - 'marginLeft' => round((float) $marginLeft ?? 0.7, 5), - 'marginRight' => round((float) $marginRight ?? 0.7, 5), - 'marginTop' => round((float) $marginTop ?? 0.7, 5), - 'marginBottom' => round((float) $marginBottom ?? 0.7, 5), - 'marginHeader' => round((float) $marginHeader ?? 0.0, 5), - 'marginFooter' => round((float) $marginFooter ?? 0.0, 5), + // margin size is already stored in inches, so no UOM conversion is required + 'marginLeft' => (float) $marginLeft ?? 0.7, + 'marginRight' => (float) $marginRight ?? 0.7, + 'marginTop' => (float) $marginTop ?? 0.3, + 'marginBottom' => (float) $marginBottom ?? 0.3, + 'marginHeader' => (float) $marginHeader ?? 0.45, + 'marginFooter' => (float) $marginFooter ?? 0.45, ]; } } diff --git a/src/PhpSpreadsheet/Reader/Xml.php b/src/PhpSpreadsheet/Reader/Xml.php index f9ad5475..9ea0a82d 100644 --- a/src/PhpSpreadsheet/Reader/Xml.php +++ b/src/PhpSpreadsheet/Reader/Xml.php @@ -15,6 +15,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Font; +use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; use SimpleXMLElement; /** @@ -626,6 +627,27 @@ class Xml extends BaseReader ++$rowID; } + + $xml_x = $worksheet->children($namespaces['x']); + if (isset($xml_x->WorksheetOptions)) { + $printSettings = $this->pageSetup($xml_x, $namespaces, $this->getPrintDefaults()); + $printSettings = $this->printSetup($xml_x, $namespaces, $printSettings); + + $spreadsheet->getActiveSheet()->getPageSetup() + ->setPaperSize($printSettings->paperSize) + ->setOrientation($printSettings->orientation) + ->setScale($printSettings->scale) + ->setVerticalCentered($printSettings->verticalCentered) + ->setHorizontalCentered($printSettings->horizontalCentered) + ->setPageOrder($printSettings->printOrder); + $spreadsheet->getActiveSheet()->getPageMargins() + ->setTop($printSettings->topMargin) + ->setHeader($printSettings->headerMargin) + ->setLeft($printSettings->leftMargin) + ->setRight($printSettings->rightMargin) + ->setBottom($printSettings->bottomMargin) + ->setFooter($printSettings->footerMargin); + } } ++$worksheetID; } @@ -855,4 +877,84 @@ class Xml extends BaseReader } } } + + private function getPrintDefaults() + { + return (object) [ + 'paperSize' => 9, + 'orientation' => PageSetup::ORIENTATION_DEFAULT, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => false, + 'printOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + 'topMargin' => 0.75, + 'headerMargin' => 0.3, + 'leftMargin' => 0.7, + 'rightMargin' => 0.7, + 'bottomMargin' => 0.75, + 'footerMargin' => 0.3, + ]; + } + + private function pageSetup(SimpleXMLElement $xmlX, array $namespaces, \stdClass $printDefaults): \stdClass + { + if (isset($xmlX->WorksheetOptions->PageSetup)) { + foreach ($xmlX->WorksheetOptions->PageSetup as $pageSetupData) { + foreach ($pageSetupData as $pageSetupKey => $pageSetupValue) { + $pageSetupAttributes = $pageSetupValue->attributes($namespaces['x']); + switch ($pageSetupKey) { + case 'Layout': + $printDefaults->orientation = (string) strtolower($pageSetupAttributes->Orientation) ?: PageSetup::ORIENTATION_PORTRAIT; + $printDefaults->horizontalCentered = (bool)$pageSetupAttributes->CenterHorizontal ?: false; + $printDefaults->verticalCentered = (bool)$pageSetupAttributes->CenterVertical ?: false; + + break; + case 'Header': + $printDefaults->headerMargin = (float)$pageSetupAttributes->Margin ?: 1.0; + + break; + case 'Footer': + $printDefaults->footerMargin = (float)$pageSetupAttributes->Margin ?: 1.0; + + break; + case 'PageMargins': + $printDefaults->leftMargin = (float) $pageSetupAttributes->Left ?: 1.0; + $printDefaults->rightMargin = (float) $pageSetupAttributes->Right ?: 1.0; + $printDefaults->topMargin = (float) $pageSetupAttributes->Top ?: 1.0; + $printDefaults->bottomMargin = (float) $pageSetupAttributes->Bottom ?: 1.0; + + break; + } + } + } + } + + return $printDefaults; + } + + private function printSetup(SimpleXMLElement $xmlX, array $namespaces, \stdClass $printDefaults): \stdClass + { + if (isset($xmlX->WorksheetOptions->Print)) { + foreach ($xmlX->WorksheetOptions->Print as $printData) { + foreach ($printData as $printKey => $printValue) { + switch ($printKey) { + case 'LeftToRight': + $printDefaults->printOrder = PageSetup::PAGEORDER_OVER_THEN_DOWN; + + break; + case 'PaperSizeIndex': + $printDefaults->paperSize = (int) $printValue ?: 9; + + break; + case 'Scale': + $printDefaults->scale = (int) $printValue ?: 100; + + break; + } + } + } + } + + return $printDefaults; + } } diff --git a/src/PhpSpreadsheet/Worksheet/PageMargins.php b/src/PhpSpreadsheet/Worksheet/PageMargins.php index 9ebfb648..d59c6afd 100644 --- a/src/PhpSpreadsheet/Worksheet/PageMargins.php +++ b/src/PhpSpreadsheet/Worksheet/PageMargins.php @@ -211,4 +211,34 @@ class PageMargins } } } + + public static function fromCentimeters($value): float + { + return $value / 2.54; + } + + public static function toCentimeters($value): float + { + return $value * 2.54; + } + + public static function fromMillimeters($value): float + { + return $value / 25.4; + } + + public static function toMillimeters($value): float + { + return $value * 25.4; + } + + public static function fromPoints($value): float + { + return $value / 72; + } + + public static function toPoints($value): float + { + return $value * 72; + } } diff --git a/src/PhpSpreadsheet/Worksheet/PageSetup.php b/src/PhpSpreadsheet/Worksheet/PageSetup.php index 290b8349..d8d5098f 100644 --- a/src/PhpSpreadsheet/Worksheet/PageSetup.php +++ b/src/PhpSpreadsheet/Worksheet/PageSetup.php @@ -249,7 +249,7 @@ class PageSetup */ private $firstPageNumber; - private $pageOrder; + private $pageOrder = self::PAGEORDER_DOWN_THEN_OVER; /** * Create a new PageSetup. @@ -823,7 +823,7 @@ class PageSetup return $this->setFirstPageNumber(null); } - public function getPageOrder(): ?string + public function getPageOrder(): string { return $this->pageOrder; } @@ -831,7 +831,7 @@ class PageSetup public function setPageOrder(?string $pageOrder): self { if ($pageOrder === null || $pageOrder === self::PAGEORDER_DOWN_THEN_OVER || $pageOrder === self::PAGEORDER_OVER_THEN_DOWN) { - $this->pageOrder = $pageOrder; + $this->pageOrder = $pageOrder ?? self::PAGEORDER_DOWN_THEN_OVER; } return $this; diff --git a/tests/PhpSpreadsheetTests/Reader/Gnumeric/PageSetupTest.php b/tests/PhpSpreadsheetTests/Reader/Gnumeric/PageSetupTest.php new file mode 100644 index 00000000..057dbf69 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Gnumeric/PageSetupTest.php @@ -0,0 +1,147 @@ +spreadsheet = $reader->load($filename); + } + + public function testPageSetup() + { + $assertions = $this->pageSetupAssertions(); + + foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + if (!array_key_exists($worksheet->getTitle(), $assertions)) { + continue; + } + + $sheetAssertions = $assertions[$worksheet->getTitle()]; + foreach ($sheetAssertions as $test => $expectedResult) { + $testMethodName = 'get' . ucfirst($test); + $actualResult = $worksheet->getPageSetup()->$testMethodName(); + $this->assertSame( + $expectedResult, + $actualResult, + "Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test}" + ); + } + } + } + + public function testPageMargins() + { + $assertions = $this->pageMarginAssertions(); + + foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + if (!array_key_exists($worksheet->getTitle(), $assertions)) { + continue; + } + + $sheetAssertions = $assertions[$worksheet->getTitle()]; + foreach ($sheetAssertions as $test => $expectedResult) { + $testMethodName = 'get' . ucfirst($test); + $actualResult = $worksheet->getPageMargins()->$testMethodName(); + $this->assertEqualsWithDelta( + $expectedResult, + $actualResult, + self::MARGIN_PRECISION, + "Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test} margin" + ); + } + } + } + + public function pageSetupAssertions() + { + return [ + 'Sheet1' => [ + 'orientation' => PageSetup::ORIENTATION_PORTRAIT, + 'scale' => 75, + 'horizontalCentered' => true, + 'verticalCentered' => false, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + 'Sheet2' => [ + 'orientation' => PageSetup::ORIENTATION_LANDSCAPE, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => true, + 'pageOrder' => PageSetup::PAGEORDER_OVER_THEN_DOWN, + ], + 'Sheet3' => [ + 'orientation' => PageSetup::ORIENTATION_PORTRAIT, + 'scale' => 90, + 'horizontalCentered' => true, + 'verticalCentered' => true, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + 'Sheet4' => [ + // Default Settings + 'orientation' => PageSetup::ORIENTATION_PORTRAIT, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => false, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + ]; + } + + public function pageMarginAssertions() + { + return [ + 'Sheet1' => [ + // Here the values are in inches + 'top' => 0.315, + 'header' => 0.630, + 'left' => 0.512, + 'right' => 0.512, + 'bottom' => 0.315, + 'footer' => 0.433, + ], + 'Sheet2' => [ + // Here the values are in inches + 'top' => 0.315, + 'header' => 0.433, + 'left' => 0.709, + 'right' => 0.709, + 'bottom' => 0.315, + 'footer' => 0.433, + ], + 'Sheet3' => [ + // Here the values are in inches + 'top' => 0.512, + 'header' => 0.433, + 'left' => 0.709, + 'right' => 0.709, + 'bottom' => 0.512, + 'footer' => 0.433, + ], + 'Sheet4' => [ + // Default Settings (in inches) + 'top' => 0.3, + 'header' => 0.45, + 'left' => 0.7, + 'right' => 0.7, + 'bottom' => 0.3, + 'footer' => 0.45, + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Reader/Ods/PageSetupTest.php b/tests/PhpSpreadsheetTests/Reader/Ods/PageSetupTest.php new file mode 100644 index 00000000..74eaae8f --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Ods/PageSetupTest.php @@ -0,0 +1,149 @@ +spreadsheet = $reader->load($filename); + } + + public function testPageSetup() + { + $assertions = $this->pageSetupAssertions(); + + foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + if (!array_key_exists($worksheet->getTitle(), $assertions)) { + continue; + } + + $sheetAssertions = $assertions[$worksheet->getTitle()]; + foreach ($sheetAssertions as $test => $expectedResult) { + $testMethodName = 'get' . ucfirst($test); + $actualResult = $worksheet->getPageSetup()->$testMethodName(); + $this->assertSame( + $expectedResult, + $actualResult, + "Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test}" + ); + } + } + } + + public function testPageMargins() + { + $assertions = $this->pageMarginAssertions(); + + foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + if (!array_key_exists($worksheet->getTitle(), $assertions)) { + continue; + } + + $sheetAssertions = $assertions[$worksheet->getTitle()]; + foreach ($sheetAssertions as $test => $expectedResult) { + $testMethodName = 'get' . ucfirst($test); + $actualResult = $worksheet->getPageMargins()->$testMethodName(); + $this->assertEqualsWithDelta( + $expectedResult, + $actualResult, + self::MARGIN_PRECISION, + "Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test} margin" + ); + } + } + } + + public function pageSetupAssertions() + { + return [ + 'Sheet1' => [ + 'orientation' => PageSetup::ORIENTATION_PORTRAIT, + 'scale' => 75, + 'horizontalCentered' => true, + 'verticalCentered' => false, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + 'Sheet2' => [ + 'orientation' => PageSetup::ORIENTATION_LANDSCAPE, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => true, + 'pageOrder' => PageSetup::PAGEORDER_OVER_THEN_DOWN, + ], + 'Sheet3' => [ + 'orientation' => PageSetup::ORIENTATION_PORTRAIT, + 'scale' => 90, + 'horizontalCentered' => true, + 'verticalCentered' => true, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + 'Sheet4' => [ + // Default Settings + 'orientation' => PageSetup::ORIENTATION_DEFAULT, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => false, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + ]; + } + + public function pageMarginAssertions() + { + return [ + 'Sheet1' => [ + // Here the values are in cm + 'top' => 0.8 / self::MARGIN_UNIT_CONVERSION, + 'header' => 1.6 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 0.8 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 1.1 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet2' => [ + // Here the values are in cm + 'top' => 0.8 / self::MARGIN_UNIT_CONVERSION, + 'header' => 1.1 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 0.8 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 1.1 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet3' => [ + // Here the values are in cm + 'top' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'header' => 1.1 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 1.1 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet4' => [ + // Default Settings (already in inches) + 'top' => 0.3, + 'header' => 0.45, + 'left' => 0.7, + 'right' => 0.7, + 'bottom' => 0.3, + 'footer' => 0.45, + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Reader/Xls/PageSetupTest.php b/tests/PhpSpreadsheetTests/Reader/Xls/PageSetupTest.php new file mode 100644 index 00000000..2cfbbb3f --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Xls/PageSetupTest.php @@ -0,0 +1,149 @@ +spreadsheet = $reader->load($filename); + } + + public function testPageSetup() + { + $assertions = $this->pageSetupAssertions(); + + foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + if (!array_key_exists($worksheet->getTitle(), $assertions)) { + continue; + } + + $sheetAssertions = $assertions[$worksheet->getTitle()]; + foreach ($sheetAssertions as $test => $expectedResult) { + $testMethodName = 'get' . ucfirst($test); + $actualResult = $worksheet->getPageSetup()->$testMethodName(); + $this->assertSame( + $expectedResult, + $actualResult, + "Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test}" + ); + } + } + } + + public function testPageMargins() + { + $assertions = $this->pageMarginAssertions(); + + foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + if (!array_key_exists($worksheet->getTitle(), $assertions)) { + continue; + } + + $sheetAssertions = $assertions[$worksheet->getTitle()]; + foreach ($sheetAssertions as $test => $expectedResult) { + $testMethodName = 'get' . ucfirst($test); + $actualResult = $worksheet->getPageMargins()->$testMethodName(); + $this->assertEqualsWithDelta( + $expectedResult, + $actualResult, + self::MARGIN_PRECISION, + "Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test} margin" + ); + } + } + } + + public function pageSetupAssertions() + { + return [ + 'Sheet1' => [ + 'orientation' => PageSetup::ORIENTATION_PORTRAIT, + 'scale' => 75, + 'horizontalCentered' => true, + 'verticalCentered' => false, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + 'Sheet2' => [ + 'orientation' => PageSetup::ORIENTATION_LANDSCAPE, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => true, + 'pageOrder' => PageSetup::PAGEORDER_OVER_THEN_DOWN, + ], + 'Sheet3' => [ + 'orientation' => PageSetup::ORIENTATION_PORTRAIT, + 'scale' => 90, + 'horizontalCentered' => true, + 'verticalCentered' => true, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + 'Sheet4' => [ + // Default Settings + 'orientation' => PageSetup::ORIENTATION_DEFAULT, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => false, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + ]; + } + + public function pageMarginAssertions() + { + return [ + 'Sheet1' => [ + // Here the values are in cm, so we convert to inches for comparison with internal uom + 'top' => 2.4 / self::MARGIN_UNIT_CONVERSION, + 'header' => 0.8 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 1.9 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 0.8 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet2' => [ + // Here the values are in cm, so we convert to inches for comparison with internal uom + 'top' => 1.9 / self::MARGIN_UNIT_CONVERSION, + 'header' => 0.8 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 1.9 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 0.8 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet3' => [ + // Here the values are in cm, so we convert to inches for comparison with internal uom + 'top' => 2.4 / self::MARGIN_UNIT_CONVERSION, + 'header' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 2.4 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 1.3 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet4' => [ + // Default Settings (already in inches for comparison) + 'top' => 0.75, + 'header' => 0.3, + 'left' => 0.7, + 'right' => 0.7, + 'bottom' => 0.75, + 'footer' => 0.3, + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/PageSetupTest.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/PageSetupTest.php new file mode 100644 index 00000000..a5b287fe --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/PageSetupTest.php @@ -0,0 +1,149 @@ +spreadsheet = $reader->load($filename); + } + + public function testPageSetup() + { + $assertions = $this->pageSetupAssertions(); + + foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + if (!array_key_exists($worksheet->getTitle(), $assertions)) { + continue; + } + + $sheetAssertions = $assertions[$worksheet->getTitle()]; + foreach ($sheetAssertions as $test => $expectedResult) { + $testMethodName = 'get' . ucfirst($test); + $actualResult = $worksheet->getPageSetup()->$testMethodName(); + $this->assertSame( + $expectedResult, + $actualResult, + "Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test}" + ); + } + } + } + + public function testPageMargins() + { + $assertions = $this->pageMarginAssertions(); + + foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + if (!array_key_exists($worksheet->getTitle(), $assertions)) { + continue; + } + + $sheetAssertions = $assertions[$worksheet->getTitle()]; + foreach ($sheetAssertions as $test => $expectedResult) { + $testMethodName = 'get' . ucfirst($test); + $actualResult = $worksheet->getPageMargins()->$testMethodName(); + $this->assertEqualsWithDelta( + $expectedResult, + $actualResult, + self::MARGIN_PRECISION, + "Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test} margin" + ); + } + } + } + + public function pageSetupAssertions() + { + return [ + 'Sheet1' => [ + 'orientation' => PageSetup::ORIENTATION_PORTRAIT, + 'scale' => 75, + 'horizontalCentered' => true, + 'verticalCentered' => false, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + 'Sheet2' => [ + 'orientation' => PageSetup::ORIENTATION_LANDSCAPE, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => true, + 'pageOrder' => PageSetup::PAGEORDER_OVER_THEN_DOWN, + ], + 'Sheet3' => [ + 'orientation' => PageSetup::ORIENTATION_PORTRAIT, + 'scale' => 90, + 'horizontalCentered' => true, + 'verticalCentered' => true, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + 'Sheet4' => [ + // Default Settings + 'orientation' => PageSetup::ORIENTATION_DEFAULT, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => false, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + ]; + } + + public function pageMarginAssertions() + { + return [ + 'Sheet1' => [ + // Here the values are in cm, so we convert to inches for comparison with internal uom + 'top' => 2.4 / self::MARGIN_UNIT_CONVERSION, + 'header' => 0.8 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 1.9 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 0.8 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet2' => [ + // Here the values are in cm, so we convert to inches for comparison with internal uom + 'top' => 1.9 / self::MARGIN_UNIT_CONVERSION, + 'header' => 0.8 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 1.9 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 0.8 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet3' => [ + // Here the values are in cm, so we convert to inches for comparison with internal uom + 'top' => 2.4 / self::MARGIN_UNIT_CONVERSION, + 'header' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 2.4 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 1.3 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet4' => [ + // Default Settings (already in inches for comparison) + 'top' => 0.75, + 'header' => 0.3, + 'left' => 0.7, + 'right' => 0.7, + 'bottom' => 0.75, + 'footer' => 0.3, + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Reader/Xml/PageSetupTest.php b/tests/PhpSpreadsheetTests/Reader/Xml/PageSetupTest.php new file mode 100644 index 00000000..8b934be6 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Xml/PageSetupTest.php @@ -0,0 +1,148 @@ +spreadsheet = $reader->load($filename); + } + + public function testPageSetup() + { + $assertions = $this->pageSetupAssertions(); + + foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + if (!array_key_exists($worksheet->getTitle(), $assertions)) { + continue; + } + + $sheetAssertions = $assertions[$worksheet->getTitle()]; + foreach ($sheetAssertions as $test => $expectedResult) { + $testMethodName = 'get' . ucfirst($test); + $actualResult = $worksheet->getPageSetup()->$testMethodName(); + $this->assertSame( + $expectedResult, + $actualResult, + "Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test}" + ); + } + } + } + + public function testPageMargins() + { + $assertions = $this->pageMarginAssertions(); + + foreach ($this->spreadsheet->getAllSheets() as $worksheet) { + if (!array_key_exists($worksheet->getTitle(), $assertions)) { + continue; + } + + $sheetAssertions = $assertions[$worksheet->getTitle()]; + foreach ($sheetAssertions as $test => $expectedResult) { + $testMethodName = 'get' . ucfirst($test); + $actualResult = $worksheet->getPageMargins()->$testMethodName(); + $this->assertEqualsWithDelta( + $expectedResult, + $actualResult, + self::MARGIN_PRECISION, + "Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test} margin" + ); + } + } + } + + public function pageSetupAssertions() + { + return [ + 'Sheet1' => [ + 'orientation' => PageSetup::ORIENTATION_PORTRAIT, + 'scale' => 75, + 'horizontalCentered' => true, + 'verticalCentered' => false, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + 'Sheet2' => [ + 'orientation' => PageSetup::ORIENTATION_LANDSCAPE, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => true, + 'pageOrder' => PageSetup::PAGEORDER_OVER_THEN_DOWN, + ], + 'Sheet3' => [ + 'orientation' => PageSetup::ORIENTATION_PORTRAIT, + 'scale' => 90, + 'horizontalCentered' => true, + 'verticalCentered' => true, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + 'Sheet4' => [ + // Default Settings + 'orientation' => PageSetup::ORIENTATION_DEFAULT, + 'scale' => 100, + 'horizontalCentered' => false, + 'verticalCentered' => false, + 'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER, + ], + ]; + } + + public function pageMarginAssertions() + { + return [ + 'Sheet1' => [ + // Here the values are in cm, so we convert to inches for comparison with internal uom + 'top' => 2.4 / self::MARGIN_UNIT_CONVERSION, + 'header' => 0.8 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 1.9 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 0.8 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet2' => [ + // Here the values are in cm, so we convert to inches for comparison with internal uom + 'top' => 1.9 / self::MARGIN_UNIT_CONVERSION, + 'header' => 0.8 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 1.9 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 0.8 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet3' => [ + // Here the values are in cm, so we convert to inches for comparison with internal uom + 'top' => 2.4 / self::MARGIN_UNIT_CONVERSION, + 'header' => 1.3 / self::MARGIN_UNIT_CONVERSION, + 'left' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'right' => 1.8 / self::MARGIN_UNIT_CONVERSION, + 'bottom' => 2.4 / self::MARGIN_UNIT_CONVERSION, + 'footer' => 1.3 / self::MARGIN_UNIT_CONVERSION, + ], + 'Sheet4' => [ + // Default Settings (already in inches for comparison) + 'top' => 0.75, + 'header' => 0.3, + 'left' => 0.7, + 'right' => 0.7, + 'bottom' => 0.75, + 'footer' => 0.3, + ], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Worksheet/PageMarginsTest.php b/tests/PhpSpreadsheetTests/Worksheet/PageMarginsTest.php new file mode 100644 index 00000000..b9713707 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Worksheet/PageMarginsTest.php @@ -0,0 +1,89 @@ +