Skip to content
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

Add support for reading Worksheet Visibility for Ods #2851

Merged
merged 2 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).

Note that a ChartSheet is still only written as a normal Worksheet containing a single chart, not as an actual ChartSheet.

- Added Worksheet visibility in Ods Reader [PR #2851](https://github.com/PHPOffice/PhpSpreadsheet/pull/2851)

### Changed

- Memory and speed improvements, particularly for the Cell Collection, and the Writers.
Expand Down
25 changes: 0 additions & 25 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1945,36 +1945,11 @@ parameters:
count: 6
path: src/PhpSpreadsheet/Reader/Ods/PageSettings.php

-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Ods\\\\PageSettings\\:\\:\\$masterPrintStylesCrossReference has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Ods/PageSettings.php

-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Ods\\\\PageSettings\\:\\:\\$masterStylesCrossReference has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Ods/PageSettings.php

-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Ods\\\\PageSettings\\:\\:\\$officeNs has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Ods/PageSettings.php

-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Ods\\\\PageSettings\\:\\:\\$pageLayoutStyles has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Ods/PageSettings.php

-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Ods\\\\PageSettings\\:\\:\\$stylesFo has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Ods/PageSettings.php

-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Ods\\\\PageSettings\\:\\:\\$stylesNs has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Reader/Ods/PageSettings.php

-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Ods\\\\Properties\\:\\:load\\(\\) has parameter \\$namespacesMeta with no type specified\\.$#"
count: 1
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Reader/Ods.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ public function loadIntoExisting($filename, Spreadsheet $spreadsheet)
break;
}
}
$pageSettings->setVisibilityForWorksheet($spreadsheet->getActiveSheet(), $worksheetStyleName);
$pageSettings->setPrintSettingsForWorksheet($spreadsheet->getActiveSheet(), $worksheetStyleName);
++$worksheetID;
}
Expand Down
47 changes: 47 additions & 0 deletions src/PhpSpreadsheet/Reader/Ods/PageSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,41 @@

class PageSettings
{
/**
* @var string
*/
private $officeNs;

/**
* @var string
*/
private $stylesNs;

/**
* @var string
*/
private $stylesFo;

/**
* @var string
*/
private $tableNs;

/**
* @var string[]
*/
private $tableStylesCrossReference = [];

private $pageLayoutStyles = [];

/**
* @var string[]
*/
private $masterStylesCrossReference = [];

/**
* @var string[]
*/
private $masterPrintStylesCrossReference = [];

public function __construct(DOMDocument $styleDom)
Expand All @@ -32,6 +57,7 @@ private function setDomNameSpaces(DOMDocument $styleDom): void
$this->officeNs = $styleDom->lookupNamespaceUri('office');
$this->stylesNs = $styleDom->lookupNamespaceUri('style');
$this->stylesFo = $styleDom->lookupNamespaceUri('fo');
$this->tableNs = $styleDom->lookupNamespaceUri('table');
}

private function readPageSettingStyles(DOMDocument $styleDom): void
Expand Down Expand Up @@ -98,12 +124,33 @@ public function readStyleCrossReferences(DOMDocument $contentDom): void
foreach ($styleXReferences as $styleXreferenceSet) {
$styleXRefName = $styleXreferenceSet->getAttributeNS($this->stylesNs, 'name');
$stylePageLayoutName = $styleXreferenceSet->getAttributeNS($this->stylesNs, 'master-page-name');
$styleFamilyName = $styleXreferenceSet->getAttributeNS($this->stylesNs, 'family');
if (!empty($styleFamilyName) && $styleFamilyName === 'table') {
$styleVisibility = 'true';
foreach ($styleXreferenceSet->getElementsByTagNameNS($this->stylesNs, 'table-properties') as $tableProperties) {
$styleVisibility = $tableProperties->getAttributeNS($this->tableNs, 'display');
}
$this->tableStylesCrossReference[$styleXRefName] = $styleVisibility;
}
if (!empty($stylePageLayoutName)) {
$this->masterStylesCrossReference[$styleXRefName] = $stylePageLayoutName;
}
}
}

public function setVisibilityForWorksheet(Worksheet $worksheet, string $styleName): void
{
if (!array_key_exists($styleName, $this->tableStylesCrossReference)) {
return;
}

$worksheet->setSheetState(
$this->tableStylesCrossReference[$styleName] === 'false'
? Worksheet::SHEETSTATE_HIDDEN
: Worksheet::SHEETSTATE_VISIBLE
);
}

public function setPrintSettingsForWorksheet(Worksheet $worksheet, string $styleName): void
{
if (!array_key_exists($styleName, $this->masterStylesCrossReference)) {
Expand Down
57 changes: 57 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Ods/HiddenWorksheetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;

use PhpOffice\PhpSpreadsheet\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;

class HiddenWorksheetTest extends TestCase
{
/**
* @var Spreadsheet
*/
private $spreadsheet;

protected function setup(): void
{
$filename = 'tests/data/Reader/Ods/HiddenSheet.ods';
$reader = new Ods();
$this->spreadsheet = $reader->load($filename);
}

public function testPageSetup(): void
{
$assertions = $this->worksheetAssertions();

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->getSheetState();
self::assertSame(
$expectedResult,
$actualResult,
"Failed asserting sheet state {$expectedResult} for Worksheet '{$worksheet->getTitle()}' {$test}"
);
}
}
}

private function worksheetAssertions(): array
{
return [
'Sheet1' => [
'sheetState' => Worksheet::SHEETSTATE_VISIBLE,
],
'Sheet2' => [
'sheetState' => Worksheet::SHEETSTATE_HIDDEN,
],
];
}
}
Binary file added tests/data/Reader/Ods/HiddenSheet.ods
Binary file not shown.