forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Permit Max Column for Row Breaks (PHPOffice#3345)
* Permit Max Column for Row Breaks Fix PHPOffice#3143. Page break was dropped. Difference between good and bad was the use of attribute `max` in `brk` tag in the good spreadsheet. However, `max` was *not* required in a similar spreadsheet. So the reason for the problem isn't completely explained. Nevertheless, it can't really hurt to capture the `max` value on read (if present) and generate it on write. This resolves the issue. User is also permitted to specify max column when setting a row break programatically. I am not yet in position to document when that might be a good idea. * Case-sensitive Directory Name Not a problem on my Windows system. * Update Documentation and Add Tests Change is necessitated by probable Excel bug. * Unhappy With Initial Implementation I kind of shoe-horned it in. Better to create a new PageBreak class, which will make it easier to accomodate any future surprises about page break handling. The only difficulty with the new approach is making sure getBreaks maintains backwards compatibility. New tests will ensure that.
- Loading branch information
Showing
10 changed files
with
353 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheet\Worksheet; | ||
|
||
use PhpOffice\PhpSpreadsheet\Calculation\Functions; | ||
use PhpOffice\PhpSpreadsheet\Cell\CellAddress; | ||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; | ||
|
||
class PageBreak | ||
{ | ||
/** @var int */ | ||
private $breakType; | ||
|
||
/** @var string */ | ||
private $coordinate; | ||
|
||
/** @var int */ | ||
private $maxColOrRow; | ||
|
||
/** @param array|CellAddress|string $coordinate */ | ||
public function __construct(int $breakType, $coordinate, int $maxColOrRow = -1) | ||
{ | ||
$coordinate = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate)); | ||
$this->breakType = $breakType; | ||
$this->coordinate = $coordinate; | ||
$this->maxColOrRow = $maxColOrRow; | ||
} | ||
|
||
public function getBreakType(): int | ||
{ | ||
return $this->breakType; | ||
} | ||
|
||
public function getCoordinate(): string | ||
{ | ||
return $this->coordinate; | ||
} | ||
|
||
public function getMaxColOrRow(): int | ||
{ | ||
return $this->maxColOrRow; | ||
} | ||
|
||
public function getColumnInt(): int | ||
{ | ||
return Coordinate::indexesFromString($this->coordinate)[0]; | ||
} | ||
|
||
public function getRow(): int | ||
{ | ||
return Coordinate::indexesFromString($this->coordinate)[1]; | ||
} | ||
|
||
public function getColumnString(): string | ||
{ | ||
return Coordinate::indexesFromString($this->coordinate)[2]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; | ||
|
||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; | ||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RowBreakTest extends TestCase | ||
{ | ||
public function testReadAndWriteRowBreak(): void | ||
{ | ||
$file = 'tests/data/Reader/XLSX/issue.3143a.xlsx'; | ||
$reader = new XlsxReader(); | ||
$spreadsheet = $reader->load($file); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$writer = new XlsxWriter($spreadsheet); | ||
$writerWorksheet = new XlsxWriter\Worksheet($writer); | ||
$data = $writerWorksheet->writeWorksheet($sheet, []); | ||
$expected = '<rowBreaks count="1" manualBreakCount="1"><brk id="25" man="1" max="16383"/></rowBreaks>'; | ||
self::assertStringContainsString($expected, $data); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public function testWriteRowBreakInPrintAreaWithMax(): void | ||
{ | ||
// This test specifies max for setBreak and appears correct. | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
for ($row = 1; $row < 60; ++$row) { | ||
for ($column = 'A'; $column !== 'L'; ++$column) { | ||
$cell = $column . $row; | ||
$sheet->getCell($cell)->setValue($cell); | ||
} | ||
} | ||
$sheet->getPageSetup()->setPrintArea('B2:J55'); | ||
$sheet->setBreak('A25', Worksheet::BREAK_ROW, Worksheet::BREAK_ROW_MAX_COLUMN); | ||
$writer = new XlsxWriter($spreadsheet); | ||
$writerWorksheet = new XlsxWriter\Worksheet($writer); | ||
$data = $writerWorksheet->writeWorksheet($sheet, []); | ||
$expected = '<rowBreaks count="1" manualBreakCount="1"><brk id="25" man="1" max="16383"/></rowBreaks>'; | ||
self::assertStringContainsString($expected, $data); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public function testWriteRowBreakInPrintAreaWithoutMax(): void | ||
{ | ||
// This test does not specify max for setBreak, | ||
// and appears incorrect. Probable Excel bug. | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
for ($row = 1; $row < 60; ++$row) { | ||
for ($column = 'A'; $column !== 'L'; ++$column) { | ||
$cell = $column . $row; | ||
$sheet->getCell($cell)->setValue($cell); | ||
} | ||
} | ||
$sheet->getPageSetup()->setPrintArea('B2:J55'); | ||
$sheet->setBreak('A25', Worksheet::BREAK_ROW); | ||
$writer = new XlsxWriter($spreadsheet); | ||
$writerWorksheet = new XlsxWriter\Worksheet($writer); | ||
$data = $writerWorksheet->writeWorksheet($sheet, []); | ||
$expected = '<rowBreaks count="1" manualBreakCount="1"><brk id="25" man="1"/></rowBreaks>'; | ||
self::assertStringContainsString($expected, $data); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
} |
Oops, something went wrong.