Retrieving print/page setup for the Xml Reader

This commit is contained in:
MarkBaker 2020-07-05 16:22:35 +02:00
parent 84ba21400c
commit 8629337101
11 changed files with 1045 additions and 48 deletions

View File

@ -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))) {

View File

@ -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,
];
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -0,0 +1,147 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Gnumeric;
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PHPUnit\Framework\TestCase;
class PageSetupTest extends TestCase
{
private const MARGIN_PRECISION = 0.001;
/**
* @var Spreadsheet $spreadsheet
*/
private $spreadsheet;
public function setup(): void
{
$filename = 'tests/data/Reader/Gnumeric/PageSetup.gnumeric';
$reader = new Gnumeric();
$this->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,
],
];
}
}

View File

@ -0,0 +1,149 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PHPUnit\Framework\TestCase;
class PageSetupTest extends TestCase
{
private const MARGIN_PRECISION = 0.00000001;
private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm
/**
* @var Spreadsheet $spreadsheet
*/
private $spreadsheet;
public function setup(): void
{
$filename = 'tests/data/Reader/Ods/PageSetup.ods';
$reader = new Ods();
$this->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,
],
];
}
}

View File

@ -0,0 +1,149 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PHPUnit\Framework\TestCase;
class PageSetupTest extends TestCase
{
private const MARGIN_PRECISION = 0.00000001;
private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm
/**
* @var Spreadsheet $spreadsheet
*/
private $spreadsheet;
public function setup(): void
{
$filename = 'tests/data/Reader/XLS/PageSetup.xls';
$reader = new Xls();
$this->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,
],
];
}
}

View File

@ -0,0 +1,149 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PHPUnit\Framework\TestCase;
class PageSetupTest extends TestCase
{
private const MARGIN_PRECISION = 0.00000001;
private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm
/**
* @var Spreadsheet $spreadsheet
*/
private $spreadsheet;
public function setup(): void
{
$filename = 'tests/data/Reader/XLSX/PageSetup.xlsx';
$reader = new Xlsx();
$this->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,
],
];
}
}

View File

@ -0,0 +1,148 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xml;
use PhpOffice\PhpSpreadsheet\Reader\Xml;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PHPUnit\Framework\TestCase;
class PageSetupTest extends TestCase
{
private const MARGIN_PRECISION = 0.000001;
private const MARGIN_UNIT_CONVERSION = 2.54; // Inches to cm
/**
* @var Spreadsheet $spreadsheet
*/
private $spreadsheet;
public function setup(): void
{
$filename = 'tests/data/Reader/Xml/PageSetup.xml';
$reader = new Xml();
$this->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,
],
];
}
}

View File

@ -0,0 +1,89 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageMargins;
use PHPUnit\Framework\TestCase;
class PageMarginsTest extends TestCase
{
/**
* @dataProvider providerPointsAndInches
*/
public function testPointsToInches(float $value, float $expectedResult): void
{
$actualResult = PageMargins::fromPoints($value);
self::assertSame($expectedResult, $actualResult);
}
/**
* @dataProvider providerPointsAndInches
*/
public function testInchesToPoints(float $expectedResult, float $value): void
{
$actualResult = PageMargins::toPoints($value);
self::assertSame($expectedResult, $actualResult);
}
/**
* @dataProvider providerCentimetersAndInches
*/
public function testCentimetersToInches(float $value, float $expectedResult): void
{
$actualResult = PageMargins::fromCentimeters($value);
self::assertSame($expectedResult, $actualResult);
}
/**
* @dataProvider providerCentimetersAndInches
*/
public function testPointsToCentimeters(float $expectedResult, float $value): void
{
$actualResult = PageMargins::toCentimeters($value);
self::assertSame($expectedResult, $actualResult);
}
/**
* @dataProvider providerMillimetersAndInches
*/
public function testMillimetersToInches(float $value, float $expectedResult): void
{
$actualResult = PageMargins::fromMillimeters($value);
self::assertSame($expectedResult, $actualResult);
}
/**
* @dataProvider providerMillimetersAndInches
*/
public function testPointsToMillimeters(float $expectedResult, float $value): void
{
$actualResult = PageMargins::toMillimeters($value);
self::assertSame($expectedResult, $actualResult);
}
public function providerPointsAndInches()
{
return [
[36, 0.5],
[72, 1.0],
[90, 1.25],
[144, 2.0],
];
}
public function providerCentimetersAndInches()
{
return [
[1.27, 0.5],
[2.54, 1.0],
];
}
public function providerMillimetersAndInches()
{
return [
[12.7, 0.5],
[25.4, 1.0],
];
}
}