Skip to content

Commit 6443416

Browse files
authored
Xls Reader Vertical Break and Writer Page Order (#3306)
Fix #3055. Xls Reader can set vertical break specifying row 0, causing an exception. It is doubtful that Excel needs a row for a vertical break; code is changed to use row 1 if the input file specifies row 0 (or lower). Code in question has not been exercised in unit test suite. Similarly, code to set horizontal break (which probably does not have a bug) is not exercised in test suite. Finally, page order in Writer incorrectly uses value in opposite way that Reader does. A new sample is added to illustrate that these are all handled correctly; it is easier to verify this by visually comparing the source spreadsheet and the copy made from it. A unit test is also added for the same spreadsheet to formally assert that the 3 properties in question are both read and written correctly.
1 parent 1194b25 commit 6443416

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

samples/Basic/50_xlsverticalbreak.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
require __DIR__ . '/../Header.php';
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xls;
6+
7+
$helper->log('Read spreadsheet');
8+
$reader = new Xls();
9+
$spreadsheet = $reader->load(__DIR__ . '/../templates/50_xlsverticalbreak.xls');
10+
11+
// Save
12+
$helper->write($spreadsheet, __FILE__, ['Xls']);
13+
$spreadsheet->disconnectWorksheets();
27 KB
Binary file not shown.

src/PhpSpreadsheet/Reader/Xls.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3211,10 +3211,10 @@ private function readVerticalPageBreaks(): void
32113211
for ($i = 0; $i < $nm; ++$i) {
32123212
$c = self::getUInt2d($recordData, 2 + 6 * $i);
32133213
$rf = self::getUInt2d($recordData, 2 + 6 * $i + 2);
3214-
$rl = self::getUInt2d($recordData, 2 + 6 * $i + 4);
3214+
//$rl = self::getUInt2d($recordData, 2 + 6 * $i + 4);
32153215

32163216
// not sure why two row indexes are necessary?
3217-
$this->phpSheet->setBreak([$c + 1, $rf], Worksheet::BREAK_COLUMN);
3217+
$this->phpSheet->setBreak([$c + 1, ($rf > 0) ? $rf : 1], Worksheet::BREAK_COLUMN);
32183218
}
32193219
}
32203220
}

src/PhpSpreadsheet/Writer/Xls/Worksheet.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1677,7 +1677,7 @@ private function writeSetup(): void
16771677

16781678
// Order of printing pages
16791679
$fLeftToRight = $this->phpSheet->getPageSetup()->getPageOrder() === PageSetup::PAGEORDER_DOWN_THEN_OVER
1680-
? 0x1 : 0x0;
1680+
? 0x0 : 0x1;
16811681
// Page orientation
16821682
$fLandscape = ($this->phpSheet->getPageSetup()->getOrientation() == PageSetup::ORIENTATION_LANDSCAPE)
16831683
? 0x0 : 0x1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xls;
6+
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
7+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
8+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
9+
10+
class PageBreakTest extends AbstractFunctional
11+
{
12+
public function testPageBreak(): void
13+
{
14+
$filename = 'samples/templates/50_xlsverticalbreak.xls';
15+
$reader = new Xls();
16+
$spreadsheet = $reader->load($filename);
17+
$sheet = $spreadsheet->getActiveSheet();
18+
$pageSetup = $sheet->getPageSetup();
19+
self::assertSame(PageSetup::PAGEORDER_DOWN_THEN_OVER, $pageSetup->getPageOrder());
20+
$breaks = $sheet->getBreaks();
21+
self::assertCount(2, $breaks);
22+
self::assertSame(Worksheet::BREAK_ROW, $breaks['A5']);
23+
self::assertSame(Worksheet::BREAK_COLUMN, $breaks['H1']);
24+
$newSpreadsheet = $this->writeAndReload($spreadsheet, 'Xls');
25+
$spreadsheet->disconnectWorksheets();
26+
27+
$newSheet = $newSpreadsheet->getActiveSheet();
28+
$newPageSetup = $newSheet->getPageSetup();
29+
self::assertSame(PageSetup::PAGEORDER_DOWN_THEN_OVER, $newPageSetup->getPageOrder());
30+
$newBreaks = $newSheet->getBreaks();
31+
self::assertCount(2, $newBreaks);
32+
self::assertSame(Worksheet::BREAK_ROW, $newBreaks['A5']);
33+
self::assertSame(Worksheet::BREAK_COLUMN, $newBreaks['H1']);
34+
$newSpreadsheet->disconnectWorksheets();
35+
}
36+
}

0 commit comments

Comments
 (0)