-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Support pageOrder in page setup for Xlsx/Xls Readers/Writers, and implement basic page setup support for other Readers/Writers #1559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 27 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
a0bba38
Support pageOrder in page setup for Xlsx Reader and Writer
21b34f0
pageOrder is nullable
362b18c
Read Print Settings Page Order in Xls Reader
4060cde
Fix comment
fbb04c1
Xls Writer changes to save print order
1f865c8
Keep phpcs happy
3780072
Retrieve basic print settings in the Ods Reader
13a3363
Return void, just to keep phpcs happy
4dadf4a
Refactor reading Ods Page Settings into a separate class
736d9ff
Gotta keep phpcs happy, even if it is only a blank line between prope…
163da06
Horizontal an dVertical centering for the Ods Reader
e644cc7
Additional print information for Gnumeric
c288c11
Failed to avoid the void trap
aecef13
Read Ods Margins
84ba214
Fix datatype conversion for Gnumeric values
8629337
Retrieving print/page setup for the Xml Reader
d009347
Forgot to check in the test files for the unit tests
3997dcc
Not sure why PHP 7.4 should suddenly have decided to report a notice …
e89196f
phpcs fixes, although I thought I'd successfully added the pre-commit…
cf6769e
Hopefully a final phpcs fix before I start looking at how to run a pr…
961dc69
Improve SCrutinizer happiness
586b36b
Refactor Xml PageSettings into a separate loader class
6cbb622
Minor refactoring
f3fc321
Gah! Using PHPStorm refactor changes the formatting, so valid code su…
c6de56e
Minor refactoring
d4094c0
Minor refactoring
47eb3e9
Update Change Log
c3fa31d
Missing typing
PowerKiKi 395b750
Stricter visibility
PowerKiKi 5233e9c
Merge branch 'master' into Page-Setup-Page-Order
0489e78
Merge branch 'master' into Page-Setup-Page-Order
PowerKiKi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheet\Reader\Gnumeric; | ||
|
||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Worksheet\PageMargins; | ||
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup as WorksheetPageSetup; | ||
use SimpleXMLElement; | ||
|
||
class PageSetup | ||
{ | ||
/** | ||
* @var Spreadsheet | ||
*/ | ||
private $spreadsheet; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $gnm; | ||
|
||
public function __construct(Spreadsheet $spreadsheet, $gnm) | ||
{ | ||
$this->spreadsheet = $spreadsheet; | ||
$this->gnm = $gnm; | ||
} | ||
|
||
public function printInformation(SimpleXMLElement $sheet): self | ||
{ | ||
if (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' ? WorksheetPageSetup::PAGEORDER_OVER_THEN_DOWN : WorksheetPageSetup::PAGEORDER_DOWN_THEN_OVER) | ||
->setScale((int) $scale) | ||
->setOrientation($orientation ?? WorksheetPageSetup::ORIENTATION_DEFAULT) | ||
->setHorizontalCentered((bool) $horizontalCentered) | ||
->setVerticalCentered((bool) $verticalCentered); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function sheetMargins(SimpleXMLElement $sheet): self | ||
{ | ||
if (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, | ||
]; | ||
|
||
$marginSet = $this->buildMarginSet($sheet, $marginSet); | ||
$this->adjustMargins($marginSet); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
private function buildMarginSet(SimpleXMLElement $sheet, array $marginSet): array | ||
{ | ||
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; | ||
} | ||
|
||
return $marginSet; | ||
} | ||
|
||
private function adjustMargins(array $marginSet): void | ||
{ | ||
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) { | ||
case 'top': | ||
$this->spreadsheet->getActiveSheet()->getPageMargins()->setTop($marginSize); | ||
|
||
break; | ||
case 'bottom': | ||
$this->spreadsheet->getActiveSheet()->getPageMargins()->setBottom($marginSize); | ||
|
||
break; | ||
case 'left': | ||
$this->spreadsheet->getActiveSheet()->getPageMargins()->setLeft($marginSize); | ||
|
||
break; | ||
case 'right': | ||
$this->spreadsheet->getActiveSheet()->getPageMargins()->setRight($marginSize); | ||
|
||
break; | ||
case 'header': | ||
$this->spreadsheet->getActiveSheet()->getPageMargins()->setHeader($marginSize); | ||
|
||
break; | ||
case 'footer': | ||
$this->spreadsheet->getActiveSheet()->getPageMargins()->setFooter($marginSize); | ||
|
||
break; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.