Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/Xlsx/RowBreakTest.php
T
oleibman 0d236eb274 Print Area and Row Break
Fix #1275, which had been closed as stale, and is now reopened pending the implementation of this PR. If there is a page break inside a defined print area, Excel may not render the print correctly unless the xml `brk` tag contains a `max` attribute. Libre Office renders it correctly. This seems like a bug in Excel (https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oe376/b32ae11b-dee7-4dcb-9b46-a0feb32ce94f states that Office ignores min and max). PR #3345 (issue #3143) already addressed this problem by allowing the user to explicitly specify a `max` property in the PageBreak object. This PR eliminates the need for the user to make use of that kludge, by adding `max` to the xml whenever a page break is specified on a sheet with a defined print area. Xlsx Reader will now ignore the `max` attribute for row breaks, since it is no longer needed; it already ignores it for column breaks. The user may still set the `max` property if desired, just in case the new treatment is not adequate (I have not found a case where that is true). Two existing unit tests are very marginally changed because of this PR.
2025-04-21 23:52:08 -07:00

74 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
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="11"/></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.
// See issue #1275, which now has a fix.
// And I agree that the fix probably indicates an 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" max="11"/></rowBreaks>';
self::assertStringContainsString($expected, $data);
$spreadsheet->disconnectWorksheets();
}
}