Skip to content

Commit 13e1b9a

Browse files
authored
Merge pull request #2853 from PHPOffice/Gnumeric-Reader-Worksheet-Visibility
Add support for reading Worksheet Visibility for Gnumeric
2 parents 7fff764 + c26215f commit 13e1b9a

File tree

10 files changed

+195
-3
lines changed

10 files changed

+195
-3
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1414

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

17-
- Added Worksheet visibility in Ods Reader [PR #2851](https://github.com/PHPOffice/PhpSpreadsheet/pull/2851)
17+
- Added Worksheet visibility in Ods Reader [PR #2851](https://github.com/PHPOffice/PhpSpreadsheet/pull/2851) and Gnumeric Reader [PR #2853](https://github.com/PHPOffice/PhpSpreadsheet/pull/2853)
1818
- Added Worksheet visibility in Ods Writer [PR #2850](https://github.com/PHPOffice/PhpSpreadsheet/pull/2850)
1919

2020
### Changed

docs/references/features-cross-reference.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<th></th>
1717
<th>XLS</th>
1818
<th>XLSX</th>
19-
<th>Excel2003XML</th>
19+
<th>XML (Excel2003XML)</th>
2020
<th>Ods</th>
2121
<th>Gnumeric</th>
2222
<th>CSV</th>
@@ -732,12 +732,32 @@
732732
<td></td>
733733
<td></td>
734734
</tr>
735+
<tr>
736+
<td style="padding-left: 1em;">Hidden Worksheets</td>
737+
<td style="text-align: center; color: green;">✔</td>
738+
<td style="text-align: center; color: green;">✔</td>
739+
<td></td>
740+
<td style="text-align: center; color: green;">✔</td>
741+
<td style="text-align: center; color: green;">✔</td>
742+
<td style="text-align: center;">N/A</td>
743+
<td></td>
744+
<td></td>
745+
<td></td>
746+
<td></td>
747+
<td></td>
748+
<td></td>
749+
<td></td>
750+
<td></td>
751+
<td></td>
752+
<td></td>
753+
</tr>
735754
<tr>
736755
<td style="padding-left: 1em;">Coloured Tabs</td>
737756
<td></td>
738757
<td></td>
739758
<td></td>
740759
<td></td>
760+
<td></td>
741761
<td style="text-align: center;">N/A</td>
742762
<td></td>
743763
<td></td>

src/PhpSpreadsheet/Reader/Gnumeric.php

+5
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Sp
272272
// name in line with the formula, not the reverse
273273
$this->spreadsheet->getActiveSheet()->setTitle($worksheetName, false, false);
274274

275+
$visibility = $sheetOrNull->attributes()['Visibility'] ?? 'GNM_SHEET_VISIBILITY_VISIBLE';
276+
if ((string) $visibility !== 'GNM_SHEET_VISIBILITY_VISIBLE') {
277+
$this->spreadsheet->getActiveSheet()->setSheetState(Worksheet::SHEETSTATE_HIDDEN);
278+
}
279+
275280
if (!$this->readDataOnly) {
276281
(new PageSetup($this->spreadsheet))
277282
->printInformation($sheet)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Gnumeric;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric;
6+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class HiddenWorksheetTest extends TestCase
11+
{
12+
/**
13+
* @var Spreadsheet
14+
*/
15+
private $spreadsheet;
16+
17+
protected function setup(): void
18+
{
19+
$filename = 'tests/data/Reader/Gnumeric/HiddenSheet.gnumeric';
20+
$reader = new Gnumeric();
21+
$this->spreadsheet = $reader->load($filename);
22+
}
23+
24+
public function testPageSetup(): void
25+
{
26+
$assertions = $this->worksheetAssertions();
27+
28+
foreach ($this->spreadsheet->getAllSheets() as $worksheet) {
29+
if (!array_key_exists($worksheet->getTitle(), $assertions)) {
30+
continue;
31+
}
32+
33+
$sheetAssertions = $assertions[$worksheet->getTitle()];
34+
foreach ($sheetAssertions as $test => $expectedResult) {
35+
$actualResult = $worksheet->getSheetState();
36+
self::assertSame(
37+
$expectedResult,
38+
$actualResult,
39+
"Failed asserting sheet state {$expectedResult} for Worksheet '{$worksheet->getTitle()}' {$test}"
40+
);
41+
}
42+
}
43+
}
44+
45+
private function worksheetAssertions(): array
46+
{
47+
return [
48+
'Sheet1' => [
49+
'sheetState' => Worksheet::SHEETSTATE_VISIBLE,
50+
],
51+
'Sheet2' => [
52+
'sheetState' => Worksheet::SHEETSTATE_HIDDEN,
53+
],
54+
];
55+
}
56+
}

tests/PhpSpreadsheetTests/Reader/Ods/HiddenWorksheetTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function testPageSetup(): void
3232

3333
$sheetAssertions = $assertions[$worksheet->getTitle()];
3434
foreach ($sheetAssertions as $test => $expectedResult) {
35-
$testMethodName = 'get' . ucfirst($test);
3635
$actualResult = $worksheet->getSheetState();
3736
self::assertSame(
3837
$expectedResult,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xls;
6+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class HiddenWorksheetTest extends TestCase
11+
{
12+
/**
13+
* @var Spreadsheet
14+
*/
15+
private $spreadsheet;
16+
17+
protected function setup(): void
18+
{
19+
$filename = 'tests/data/Reader/XLS/HiddenSheet.xls';
20+
$reader = new Xls();
21+
$this->spreadsheet = $reader->load($filename);
22+
}
23+
24+
public function testPageSetup(): void
25+
{
26+
$assertions = $this->worksheetAssertions();
27+
28+
foreach ($this->spreadsheet->getAllSheets() as $worksheet) {
29+
if (!array_key_exists($worksheet->getTitle(), $assertions)) {
30+
continue;
31+
}
32+
33+
$sheetAssertions = $assertions[$worksheet->getTitle()];
34+
foreach ($sheetAssertions as $test => $expectedResult) {
35+
$actualResult = $worksheet->getSheetState();
36+
self::assertSame(
37+
$expectedResult,
38+
$actualResult,
39+
"Failed asserting sheet state {$expectedResult} for Worksheet '{$worksheet->getTitle()}' {$test}"
40+
);
41+
}
42+
}
43+
}
44+
45+
private function worksheetAssertions(): array
46+
{
47+
return [
48+
'Sheet1' => [
49+
'sheetState' => Worksheet::SHEETSTATE_VISIBLE,
50+
],
51+
'Sheet2' => [
52+
'sheetState' => Worksheet::SHEETSTATE_HIDDEN,
53+
],
54+
];
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
6+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class HiddenWorksheetTest extends TestCase
11+
{
12+
/**
13+
* @var Spreadsheet
14+
*/
15+
private $spreadsheet;
16+
17+
protected function setup(): void
18+
{
19+
$filename = 'tests/data/Reader/XLSX/HiddenSheet.xlsx';
20+
$reader = new Xlsx();
21+
$this->spreadsheet = $reader->load($filename);
22+
}
23+
24+
public function testPageSetup(): void
25+
{
26+
$assertions = $this->worksheetAssertions();
27+
28+
foreach ($this->spreadsheet->getAllSheets() as $worksheet) {
29+
if (!array_key_exists($worksheet->getTitle(), $assertions)) {
30+
continue;
31+
}
32+
33+
$sheetAssertions = $assertions[$worksheet->getTitle()];
34+
foreach ($sheetAssertions as $test => $expectedResult) {
35+
$actualResult = $worksheet->getSheetState();
36+
self::assertSame(
37+
$expectedResult,
38+
$actualResult,
39+
"Failed asserting sheet state {$expectedResult} for Worksheet '{$worksheet->getTitle()}' {$test}"
40+
);
41+
}
42+
}
43+
}
44+
45+
private function worksheetAssertions(): array
46+
{
47+
return [
48+
'Sheet1' => [
49+
'sheetState' => Worksheet::SHEETSTATE_VISIBLE,
50+
],
51+
'Sheet2' => [
52+
'sheetState' => Worksheet::SHEETSTATE_HIDDEN,
53+
],
54+
];
55+
}
56+
}
1.83 KB
Binary file not shown.

tests/data/Reader/XLS/HiddenSheet.xls

25 KB
Binary file not shown.
8.45 KB
Binary file not shown.

0 commit comments

Comments
 (0)