Add support for reading Worksheet Visibility for Ods

This commit is contained in:
MarkBaker 2022-05-23 15:30:45 +02:00
parent 7c1c896959
commit 3fae29d613
6 changed files with 107 additions and 25 deletions

View File

@ -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. 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 ### Changed
- Memory and speed improvements, particularly for the Cell Collection, and the Writers. - Memory and speed improvements, particularly for the Cell Collection, and the Writers.

View File

@ -1945,36 +1945,11 @@ parameters:
count: 6 count: 6
path: src/PhpSpreadsheet/Reader/Ods/PageSettings.php 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\\.$#" message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Ods\\\\PageSettings\\:\\:\\$pageLayoutStyles has no type specified\\.$#"
count: 1 count: 1
path: src/PhpSpreadsheet/Reader/Ods/PageSettings.php 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\\.$#" message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Ods\\\\Properties\\:\\:load\\(\\) has parameter \\$namespacesMeta with no type specified\\.$#"
count: 1 count: 1

View File

@ -588,6 +588,7 @@ class Ods extends BaseReader
break; break;
} }
} }
$pageSettings->setVisibilityForWorksheet($spreadsheet->getActiveSheet(), $worksheetStyleName);
$pageSettings->setPrintSettingsForWorksheet($spreadsheet->getActiveSheet(), $worksheetStyleName); $pageSettings->setPrintSettingsForWorksheet($spreadsheet->getActiveSheet(), $worksheetStyleName);
++$worksheetID; ++$worksheetID;
} }

View File

@ -8,16 +8,41 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class PageSettings class PageSettings
{ {
/**
* @var string
*/
private $officeNs; private $officeNs;
/**
* @var string
*/
private $stylesNs; private $stylesNs;
/**
* @var string
*/
private $stylesFo; private $stylesFo;
/**
* @var string
*/
private $tableNs;
/**
* @var string[]
*/
private $tableStylesCrossReference = [];
private $pageLayoutStyles = []; private $pageLayoutStyles = [];
/**
* @var string[]
*/
private $masterStylesCrossReference = []; private $masterStylesCrossReference = [];
/**
* @var string[]
*/
private $masterPrintStylesCrossReference = []; private $masterPrintStylesCrossReference = [];
public function __construct(DOMDocument $styleDom) public function __construct(DOMDocument $styleDom)
@ -32,6 +57,7 @@ class PageSettings
$this->officeNs = $styleDom->lookupNamespaceUri('office'); $this->officeNs = $styleDom->lookupNamespaceUri('office');
$this->stylesNs = $styleDom->lookupNamespaceUri('style'); $this->stylesNs = $styleDom->lookupNamespaceUri('style');
$this->stylesFo = $styleDom->lookupNamespaceUri('fo'); $this->stylesFo = $styleDom->lookupNamespaceUri('fo');
$this->tableNs = $styleDom->lookupNamespaceUri('table');
} }
private function readPageSettingStyles(DOMDocument $styleDom): void private function readPageSettingStyles(DOMDocument $styleDom): void
@ -98,12 +124,33 @@ class PageSettings
foreach ($styleXReferences as $styleXreferenceSet) { foreach ($styleXReferences as $styleXreferenceSet) {
$styleXRefName = $styleXreferenceSet->getAttributeNS($this->stylesNs, 'name'); $styleXRefName = $styleXreferenceSet->getAttributeNS($this->stylesNs, 'name');
$stylePageLayoutName = $styleXreferenceSet->getAttributeNS($this->stylesNs, 'master-page-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)) { if (!empty($stylePageLayoutName)) {
$this->masterStylesCrossReference[$styleXRefName] = $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 public function setPrintSettingsForWorksheet(Worksheet $worksheet, string $styleName): void
{ {
if (!array_key_exists($styleName, $this->masterStylesCrossReference)) { if (!array_key_exists($styleName, $this->masterStylesCrossReference)) {

View File

@ -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 not shown.