Skip to content

Commit bbe6b80

Browse files
author
MarkBaker
committed
Some additional unit tests for the ReferenceHelper
And a bugfix when deleting cells that contain hyperlinks (the hperlinks weren't being deleted, so were being "inherited" by whatever cell moved to that address)
1 parent 6f84780 commit bbe6b80

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3939

4040
### Fixed
4141

42+
- Fix bug when deleting cells with hyperlinks, where the hyperlink was then being "inherited" by whatever cell moved to that cell address.
4243
- Fix bug in Conditional Formatting in the Xls Writer that resulted in a broken file when there were multiple conditional ranges in a worksheet.
4344
- Fix Conditional Formatting in the Xls Writer to work with rules that contain string literals, cell references and formulae.
4445
- Fix for setting Active Sheet to the first loaded worksheet when bookViews element isn't defined [Issue #2666](https://github.com/PHPOffice/PhpSpreadsheet/issues/2666) [PR #2669](https://github.com/PHPOffice/PhpSpreadsheet/pull/2669)

src/PhpSpreadsheet/ReferenceHelper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ protected function adjustHyperlinks($worksheet, $numberOfColumns, $numberOfRows)
189189

190190
foreach ($aHyperlinkCollection as $cellAddress => $value) {
191191
$newReference = $this->updateCellReference($cellAddress);
192-
if ($cellAddress !== $newReference) {
192+
if ($this->cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === true) {
193+
$worksheet->setHyperlink($cellAddress, null);
194+
} elseif ($cellAddress !== $newReference) {
193195
$worksheet->setHyperlink($newReference, $value);
194196
$worksheet->setHyperlink($cellAddress, null);
195197
}

tests/PhpSpreadsheetTests/ReferenceHelperTest.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace PhpOffice\PhpSpreadsheetTests;
44

55
use PhpOffice\PhpSpreadsheet\Cell\DataType;
6+
use PhpOffice\PhpSpreadsheet\Cell\Hyperlink;
7+
use PhpOffice\PhpSpreadsheet\Comment;
68
use PhpOffice\PhpSpreadsheet\ReferenceHelper;
79
use PhpOffice\PhpSpreadsheet\Spreadsheet;
10+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
811
use PHPUnit\Framework\TestCase;
912

1013
class ReferenceHelperTest extends TestCase
@@ -177,4 +180,120 @@ public function testRemoveColumnShiftsCorrectColumnValueIntoRemovedColumnCoordin
177180
self::assertNull($cells[1][1]);
178181
self::assertArrayNotHasKey(2, $cells[1]);
179182
}
183+
184+
public function testInsertRowsWithPageBreaks(): void
185+
{
186+
$spreadsheet = new Spreadsheet();
187+
$sheet = $spreadsheet->getActiveSheet();
188+
$sheet->fromArray([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], null, 'A1', true);
189+
$sheet->setBreak('A2', Worksheet::BREAK_ROW);
190+
$sheet->setBreak('A5', Worksheet::BREAK_ROW);
191+
192+
$sheet->insertNewRowBefore(2, 2);
193+
194+
$breaks = $sheet->getBreaks();
195+
ksort($breaks);
196+
self::assertSame(['A4' => Worksheet::BREAK_ROW, 'A7' => Worksheet::BREAK_ROW], $breaks);
197+
}
198+
199+
public function testDeleteRowsWithPageBreaks(): void
200+
{
201+
$spreadsheet = new Spreadsheet();
202+
$sheet = $spreadsheet->getActiveSheet();
203+
$sheet->fromArray([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], null, 'A1', true);
204+
$sheet->setBreak('A2', Worksheet::BREAK_ROW);
205+
$sheet->setBreak('A5', Worksheet::BREAK_ROW);
206+
207+
$sheet->removeRow(2, 2);
208+
209+
$breaks = $sheet->getBreaks();
210+
self::assertSame(['A3' => Worksheet::BREAK_ROW], $breaks);
211+
}
212+
213+
public function testInsertRowsWithComments(): void
214+
{
215+
$spreadsheet = new Spreadsheet();
216+
$sheet = $spreadsheet->getActiveSheet();
217+
$sheet->fromArray([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], null, 'A1', true);
218+
$sheet->getComment('A2')->getText()->createText('First Comment');
219+
$sheet->getComment('A5')->getText()->createText('Second Comment');
220+
221+
$sheet->insertNewRowBefore(2, 2);
222+
223+
$comments = array_map(
224+
function (Comment $value) {
225+
return $value->getText()->getPlainText();
226+
},
227+
$sheet->getComments()
228+
);
229+
230+
self::assertSame(['A4' => 'First Comment', 'A7' => 'Second Comment'], $comments);
231+
}
232+
233+
public function testDeleteRowsWithComments(): void
234+
{
235+
$spreadsheet = new Spreadsheet();
236+
$sheet = $spreadsheet->getActiveSheet();
237+
$sheet->fromArray([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], null, 'A1', true);
238+
$sheet->getComment('A2')->getText()->createText('First Comment');
239+
$sheet->getComment('A5')->getText()->createText('Second Comment');
240+
241+
$sheet->removeRow(2, 2);
242+
243+
$comments = array_map(
244+
function (Comment $value) {
245+
return $value->getText()->getPlainText();
246+
},
247+
$sheet->getComments()
248+
);
249+
250+
self::assertSame(['A3' => 'Second Comment'], $comments);
251+
}
252+
253+
public function testInsertRowsWithHyperlinks(): void
254+
{
255+
$spreadsheet = new Spreadsheet();
256+
$sheet = $spreadsheet->getActiveSheet();
257+
$sheet->fromArray([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], null, 'A1', true);
258+
$sheet->getCell('A2')->getHyperlink()->setUrl('https://github.com/PHPOffice/PhpSpreadsheet');
259+
$sheet->getCell('A5')->getHyperlink()->setUrl('https://phpspreadsheet.readthedocs.io/en/latest/');
260+
261+
$sheet->insertNewRowBefore(2, 2);
262+
263+
$hyperlinks = array_map(
264+
function (Hyperlink $value) {
265+
return $value->getUrl();
266+
},
267+
$sheet->getHyperlinkCollection()
268+
);
269+
ksort($hyperlinks);
270+
271+
self::assertSame(
272+
[
273+
'A4' => 'https://github.com/PHPOffice/PhpSpreadsheet',
274+
'A7' => 'https://phpspreadsheet.readthedocs.io/en/latest/',
275+
],
276+
$hyperlinks
277+
);
278+
}
279+
280+
public function testDeleteRowsWithHyperlinks(): void
281+
{
282+
$spreadsheet = new Spreadsheet();
283+
$sheet = $spreadsheet->getActiveSheet();
284+
$sheet->fromArray([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], null, 'A1', true);
285+
$sheet->getCell('A2')->getHyperlink()->setUrl('https://github.com/PHPOffice/PhpSpreadsheet');
286+
$sheet->getCell('A5')->getHyperlink()->setUrl('https://phpspreadsheet.readthedocs.io/en/latest/');
287+
288+
$sheet->removeRow(2, 2);
289+
290+
$hyperlinks = array_map(
291+
function (Hyperlink $value) {
292+
return $value->getUrl();
293+
},
294+
$sheet->getHyperlinkCollection()
295+
);
296+
297+
self::assertSame(['A3' => 'https://phpspreadsheet.readthedocs.io/en/latest/'], $hyperlinks);
298+
}
180299
}

0 commit comments

Comments
 (0)