Add support for reading Worksheet Visibility for Ods
This commit is contained in:
parent
7c1c896959
commit
3fae29d613
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -588,6 +588,7 @@ class Ods extends BaseReader
|
|||
break;
|
||||
}
|
||||
}
|
||||
$pageSettings->setVisibilityForWorksheet($spreadsheet->getActiveSheet(), $worksheetStyleName);
|
||||
$pageSettings->setPrintSettingsForWorksheet($spreadsheet->getActiveSheet(), $worksheetStyleName);
|
||||
++$worksheetID;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,16 +8,41 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|||
|
||||
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)
|
||||
|
|
@ -32,6 +57,7 @@ class PageSettings
|
|||
$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
|
||||
|
|
@ -98,12 +124,33 @@ class PageSettings
|
|||
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)) {
|
||||
|
|
|
|||
|
|
@ -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.
Loading…
Reference in New Issue