Skip to content

Commit 2a0090b

Browse files
committed
Xlsx Reader and Print/Show Gridlines
Fix #912, opened in Feb. 2019, and closed as stale in Apr. 2019, and which I have re-opened to be closed properly by this PR. Another "better late than never". Original issue says that print options should not affect ShowGridlines, which seems true enough. Aside from that, the existing code isn't quite correct anyhow. Excel looks for 2 attributes, one of which must be explicitly set to true and the other of which must not be explicitly set to false, in order to determine whether PrintGridlines should be set. PhpSpreadsheet is changed to do the same. This could be treated as a BC break for the unusual situation described in the issue, but it seems more like a bug fix to me.
1 parent 1b68270 commit 2a0090b

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/PhpSpreadsheet/Reader/Xlsx/SheetViewOptions.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,12 @@ private function sheetFormat(SimpleXMLElement $sheetFormatPrx): void
122122
private function printOptions(SimpleXMLElement $printOptionsx): void
123123
{
124124
$printOptions = $printOptionsx->attributes() ?? [];
125-
if (isset($printOptions['gridLinesSet']) && self::boolean((string) $printOptions['gridLinesSet'])) {
126-
$this->worksheet->setShowGridlines(true);
127-
}
125+
// Spec is weird. gridLines (default false)
126+
// and gridLinesSet (default true) must both be true.
128127
if (isset($printOptions['gridLines']) && self::boolean((string) $printOptions['gridLines'])) {
129-
$this->worksheet->setPrintGridlines(true);
128+
if (!isset($printOptions['gridLinesSet']) || self::boolean((string) $printOptions['gridLinesSet'])) {
129+
$this->worksheet->setPrintGridlines(true);
130+
}
130131
}
131132
if (isset($printOptions['horizontalCentered']) && self::boolean((string) $printOptions['horizontalCentered'])) {
132133
$this->worksheet->getPageSetup()->setHorizontalCentered(true);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
9+
10+
class GridlinesTest extends AbstractFunctional
11+
{
12+
/**
13+
* @dataProvider loadDataProvider
14+
*/
15+
public function testGridlines(bool $display, bool $print): void
16+
{
17+
$spreadsheet = new Spreadsheet();
18+
$sheet1 = $spreadsheet->getActiveSheet();
19+
$sheet2 = $spreadsheet->createSheet();
20+
$sheet1->setShowGridlines($display);
21+
$sheet1->setPrintGridlines($print);
22+
$sheet1->fromArray(
23+
[
24+
[1, 2, 3],
25+
[4, 5, 6],
26+
[7, 8, 9],
27+
]
28+
);
29+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
30+
$spreadsheet->disconnectWorksheets();
31+
$rsheet1 = $reloadedSpreadsheet->getSheet(0);
32+
$rsheet2 = $reloadedSpreadsheet->getSheet(1);
33+
self::assertSame($display, $rsheet1->getShowGridlines());
34+
self::assertSame($print, $rsheet1->getPrintGridlines());
35+
self::assertTrue($rsheet2->getShowGridlines());
36+
self::assertFalse($rsheet2->getPrintGridlines());
37+
$reloadedSpreadsheet->disconnectWorksheets();
38+
}
39+
40+
public static function loadDataProvider(): array
41+
{
42+
return [
43+
[true, true],
44+
[true, false],
45+
[false, true],
46+
[false, false],
47+
];
48+
}
49+
}

0 commit comments

Comments
 (0)