Skip to content

Commit 2952cf5

Browse files
committed
Ods Reader Allow Omission of Some Page Settings Tags
Fix #4099. Ods Reader was expecting there to always be `header-style` and `footer-style` tags when `page-layout` tag is present, but these need not exist. It seemed like there might be other exposures along this line in `readPageSettingStyles`; rather than waiting for a problem report to show up for each, the code is updated to use `->item(0)` in place of `[0]` when appropriate, and make use of the nullsafe `?->` operator introduced with Php8.
1 parent 1b68270 commit 2952cf5

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

src/PhpSpreadsheet/Reader/Ods/PageSettings.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,22 @@ private function readPageSettingStyles(DOMDocument $styleDom): void
5555

5656
foreach ($styles as $styleSet) {
5757
$styleName = $styleSet->getAttributeNS($this->stylesNs, 'name');
58-
$pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')[0];
59-
$styleOrientation = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-orientation');
60-
$styleScale = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'scale-to');
61-
$stylePrintOrder = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'print-page-order');
62-
$centered = $pageLayoutProperties->getAttributeNS($this->stylesNs, 'table-centering');
63-
64-
$marginLeft = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-left');
65-
$marginRight = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-right');
66-
$marginTop = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-top');
67-
$marginBottom = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-bottom');
68-
$header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')[0];
69-
$headerProperties = $header->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0];
70-
$marginHeader = isset($headerProperties) ? $headerProperties->getAttributeNS($this->stylesFo, 'min-height') : null;
71-
$footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')[0];
72-
$footerProperties = $footer->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0];
73-
$marginFooter = isset($footerProperties) ? $footerProperties->getAttributeNS($this->stylesFo, 'min-height') : null;
58+
$pageLayoutProperties = $styleSet->getElementsByTagNameNS($this->stylesNs, 'page-layout-properties')->item(0);
59+
$styleOrientation = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'print-orientation');
60+
$styleScale = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'scale-to');
61+
$stylePrintOrder = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'print-page-order');
62+
$centered = $pageLayoutProperties?->getAttributeNS($this->stylesNs, 'table-centering');
63+
64+
$marginLeft = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-left');
65+
$marginRight = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-right');
66+
$marginTop = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-top');
67+
$marginBottom = $pageLayoutProperties?->getAttributeNS($this->stylesFo, 'margin-bottom');
68+
$header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')->item(0);
69+
$headerProperties = $header?->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')?->item(0);
70+
$marginHeader = $headerProperties?->getAttributeNS($this->stylesFo, 'min-height');
71+
$footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')->item(0);
72+
$footerProperties = $footer?->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')?->item(0);
73+
$marginFooter = $footerProperties?->getAttributeNS($this->stylesFo, 'min-height');
7474

7575
$this->pageLayoutStyles[$styleName] = (object) [
7676
'orientation' => $styleOrientation ?: PageSetup::ORIENTATION_DEFAULT,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Ods as OdsReader;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue4099Test extends TestCase
11+
{
12+
private string $file = 'tests/data/Reader/Ods/issue.4099.ods';
13+
14+
public function testNoHeaderFooterStyle(): void
15+
{
16+
// header-style and footer-style are missing in styles.xml
17+
$zipFile = 'zip://' . $this->file . '#styles.xml';
18+
$contents = (string) file_get_contents($zipFile);
19+
self::assertStringContainsString('page-layout ', $contents);
20+
self::assertStringNotContainsString('header-style', $contents);
21+
self::assertStringNotContainsString('footer-style', $contents);
22+
$reader = new OdsReader();
23+
$spreadsheet = $reader->load($this->file);
24+
$sheet = $spreadsheet->getActiveSheet();
25+
self::assertSame('FirstCell', $sheet->getCell('A1')->getValue());
26+
$spreadsheet->disconnectWorksheets();
27+
}
28+
}

tests/data/Reader/Ods/issue.4099.ods

8.68 KB
Binary file not shown.

0 commit comments

Comments
 (0)