Skip to content

Commit

Permalink
Merge pull request #4101 from oleibman/issue4099
Browse files Browse the repository at this point in the history
Ods Reader Allow Omission of Some Page Settings Tags
  • Loading branch information
oleibman authored Jul 27, 2024
2 parents 8225096 + debb177 commit b43947f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ and this project adheres to [Semantic Versioning](https://semver.org).
### Fixed

- Add Sheet may leave Active Sheet uninitialized. [Issue #4112](https://github.com/PHPOffice/PhpSpreadsheet/issues/4112) [PR #4114](https://github.com/PHPOffice/PhpSpreadsheet/pull/4114)
- Reference to Defined Name Specifying Worksheet. [Issue #206](https://github.com/PHPOffice/PhpSpreadsheet/issues/296) [PR #4096](https://github.com/PHPOffice/PhpSpreadsheet/pull/4096)
- Xls Reader Print/Show Gridlines. [Issue #912](https://github.com/PHPOffice/PhpSpreadsheet/issues/912) [PR #4098](https://github.com/PHPOffice/PhpSpreadsheet/pull/4098)
- ODS Reader Allow Omission of Page Settings Tags. [Issue #4099](https://github.com/PHPOffice/PhpSpreadsheet/issues/4099) [PR #4101](https://github.com/PHPOffice/PhpSpreadsheet/pull/4101)

## 2024-07-24 - 2.2.0

Expand Down
32 changes: 16 additions & 16 deletions src/PhpSpreadsheet/Reader/Ods/PageSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ private function readPageSettingStyles(DOMDocument $styleDom): void

foreach ($styles as $styleSet) {
$styleName = $styleSet->getAttributeNS($this->stylesNs, 'name');
$pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')[0];
$styleOrientation = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-orientation');
$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');
$marginBottom = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-bottom');
$header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')[0];
$headerProperties = $header->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0];
$marginHeader = isset($headerProperties) ? $headerProperties->getAttributeNS($this->stylesFo, 'min-height') : null;
$footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')[0];
$footerProperties = $footer->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0];
$marginFooter = isset($footerProperties) ? $footerProperties->getAttributeNS($this->stylesFo, 'min-height') : null;
$pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')->item(0);
$styleOrientation = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'print-orientation');
$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');
$marginBottom = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-bottom');
$header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')->item(0);
$headerProperties = $header?->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')?->item(0);
$marginHeader = $headerProperties?->getAttributeNS($this->stylesFo, 'min-height');
$footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')->item(0);
$footerProperties = $footer?->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')?->item(0);
$marginFooter = $footerProperties?->getAttributeNS($this->stylesFo, 'min-height');

$this->pageLayoutStyles[$styleName] = (object) [
'orientation' => $styleOrientation ?: PageSetup::ORIENTATION_DEFAULT,
Expand Down
28 changes: 28 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Ods/Issue4099Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;

use PhpOffice\PhpSpreadsheet\Reader\Ods as OdsReader;
use PHPUnit\Framework\TestCase;

class Issue4099Test extends TestCase
{
private string $file = 'tests/data/Reader/Ods/issue.4099.ods';

public function testNoHeaderFooterStyle(): void
{
// header-style and footer-style are missing in styles.xml
$zipFile = 'zip://' . $this->file . '#styles.xml';
$contents = (string) file_get_contents($zipFile);
self::assertStringContainsString('page-layout ', $contents);
self::assertStringNotContainsString('header-style', $contents);
self::assertStringNotContainsString('footer-style', $contents);
$reader = new OdsReader();
$spreadsheet = $reader->load($this->file);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('FirstCell', $sheet->getCell('A1')->getValue());
$spreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/Ods/issue.4099.ods
Binary file not shown.

0 comments on commit b43947f

Please sign in to comment.