Skip to content

Commit

Permalink
Not Finding Rels File (#3555)
Browse files Browse the repository at this point in the history
Fix #3552. Inserting rows or columns can cause page break arrays to be out of order. Sort them in Worksheet functions getBreaks, getRowBreaks, and getColumnBreaks before returning to caller.
  • Loading branch information
oleibman authored May 11, 2023
1 parent b3112e4 commit ff286a6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1806,9 +1806,15 @@ public function setBreakByColumnAndRow($columnIndex, $row, $break)
public function getBreaks()
{
$breaks = [];
/** @var callable */
$compareFunction = [self::class, 'compareRowBreaks'];
uksort($this->rowBreaks, $compareFunction);
foreach ($this->rowBreaks as $break) {
$breaks[$break->getCoordinate()] = self::BREAK_ROW;
}
/** @var callable */
$compareFunction = [self::class, 'compareColumnBreaks'];
uksort($this->columnBreaks, $compareFunction);
foreach ($this->columnBreaks as $break) {
$breaks[$break->getCoordinate()] = self::BREAK_COLUMN;
}
Expand All @@ -1823,16 +1829,40 @@ public function getBreaks()
*/
public function getRowBreaks()
{
/** @var callable */
$compareFunction = [self::class, 'compareRowBreaks'];
uksort($this->rowBreaks, $compareFunction);

return $this->rowBreaks;
}

protected static function compareRowBreaks(string $coordinate1, string $coordinate2): int
{
$row1 = Coordinate::indexesFromString($coordinate1)[1];
$row2 = Coordinate::indexesFromString($coordinate2)[1];

return $row1 - $row2;
}

protected static function compareColumnBreaks(string $coordinate1, string $coordinate2): int
{
$column1 = Coordinate::indexesFromString($coordinate1)[0];
$column2 = Coordinate::indexesFromString($coordinate2)[0];

return $column1 - $column2;
}

/**
* Get row breaks.
* Get column breaks.
*
* @return PageBreak[]
*/
public function getColumnBreaks()
{
/** @var callable */
$compareFunction = [self::class, 'compareColumnBreaks'];
uksort($this->columnBreaks, $compareFunction);

return $this->columnBreaks;
}

Expand Down
43 changes: 43 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3552Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class Issue3552Test extends AbstractFunctional
{
/**
* @var string
*/
private static $testbook = 'tests/data/Reader/XLSX/issue.3552.xlsx';

public function testRowBreaks(): void
{
$reader = new Xlsx();
$spreadsheet = $reader->load(self::$testbook);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame(['A92', 'A184', 'A276', 'A368', 'A417', 'A511', 'A554'], array_keys($sheet->getRowBreaks()));
$sheet->insertNewRowBefore(397, 1);
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
$spreadsheet->disconnectWorksheets();
$reloadedSheet = $reloadedSpreadsheet->getActiveSheet();
self::assertSame(['A92', 'A184', 'A276', 'A368', 'A418', 'A512', 'A555'], array_keys($reloadedSheet->getRowBreaks()));
$reloadedSpreadsheet->disconnectWorksheets();
}

public function testColumnBreaks(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setBreak('Z1', Worksheet::BREAK_COLUMN);
$sheet->setBreak('H1', Worksheet::BREAK_COLUMN);
$sheet->setBreak('P1', Worksheet::BREAK_COLUMN);
self::assertSame(['H1', 'P1', 'Z1'], array_keys($sheet->getColumnBreaks()));
$sheet->insertNewColumnBefore('N', 2);
self::assertSame(['H1', 'R1', 'AB1'], array_keys($sheet->getColumnBreaks()));
$spreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/XLSX/issue.3552.xlsx
Binary file not shown.

0 comments on commit ff286a6

Please sign in to comment.