Skip to content

Commit

Permalink
Xlsx Reader and Print/Show Gridlines
Browse files Browse the repository at this point in the history
Fix PHPOffice#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.
  • Loading branch information
oleibman committed Jul 17, 2024
1 parent 1b68270 commit 2a0090b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/PhpSpreadsheet/Reader/Xlsx/SheetViewOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@ private function sheetFormat(SimpleXMLElement $sheetFormatPrx): void
private function printOptions(SimpleXMLElement $printOptionsx): void
{
$printOptions = $printOptionsx->attributes() ?? [];
if (isset($printOptions['gridLinesSet']) && self::boolean((string) $printOptions['gridLinesSet'])) {
$this->worksheet->setShowGridlines(true);
}
// Spec is weird. gridLines (default false)
// and gridLinesSet (default true) must both be true.
if (isset($printOptions['gridLines']) && self::boolean((string) $printOptions['gridLines'])) {
$this->worksheet->setPrintGridlines(true);
if (!isset($printOptions['gridLinesSet']) || self::boolean((string) $printOptions['gridLinesSet'])) {
$this->worksheet->setPrintGridlines(true);
}
}
if (isset($printOptions['horizontalCentered']) && self::boolean((string) $printOptions['horizontalCentered'])) {
$this->worksheet->getPageSetup()->setHorizontalCentered(true);
Expand Down
49 changes: 49 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/GridlinesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class GridlinesTest extends AbstractFunctional
{
/**
* @dataProvider loadDataProvider
*/
public function testGridlines(bool $display, bool $print): void
{
$spreadsheet = new Spreadsheet();
$sheet1 = $spreadsheet->getActiveSheet();
$sheet2 = $spreadsheet->createSheet();
$sheet1->setShowGridlines($display);
$sheet1->setPrintGridlines($print);
$sheet1->fromArray(
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
);
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
$spreadsheet->disconnectWorksheets();
$rsheet1 = $reloadedSpreadsheet->getSheet(0);
$rsheet2 = $reloadedSpreadsheet->getSheet(1);
self::assertSame($display, $rsheet1->getShowGridlines());
self::assertSame($print, $rsheet1->getPrintGridlines());
self::assertTrue($rsheet2->getShowGridlines());
self::assertFalse($rsheet2->getPrintGridlines());
$reloadedSpreadsheet->disconnectWorksheets();
}

public static function loadDataProvider(): array
{
return [
[true, true],
[true, false],
[false, true],
[false, false],
];
}
}

0 comments on commit 2a0090b

Please sign in to comment.