Skip to content

Commit b5b83ab

Browse files
authored
Adjust Both Coordinates for Two-Cell Anchors (#2909)
Fix #2908. When support for two-cell anchors was added for drawings, we neglected to adjust the second cell address when rows or columns are added or deleted. It also appears that "twoCell" and "oneCell" were introduced as lower-case literals when support for the editAs attribute was subsequently introduced.
1 parent a895721 commit b5b83ab

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed

src/PhpSpreadsheet/ReferenceHelper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,12 @@ function ($coordinate) use ($cellCollection) {
525525
if ($objDrawing->getCoordinates() != $newReference) {
526526
$objDrawing->setCoordinates($newReference);
527527
}
528+
if ($objDrawing->getCoordinates2() !== '') {
529+
$newReference = $this->updateCellReference($objDrawing->getCoordinates2());
530+
if ($objDrawing->getCoordinates2() != $newReference) {
531+
$objDrawing->setCoordinates2($newReference);
532+
}
533+
}
528534
}
529535

530536
// Update workbook: define names

src/PhpSpreadsheet/Worksheet/BaseDrawing.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
class BaseDrawing implements IComparable
1010
{
1111
const EDIT_AS_ABSOLUTE = 'absolute';
12-
const EDIT_AS_ONECELL = 'onecell';
13-
const EDIT_AS_TWOCELL = 'twocell';
12+
const EDIT_AS_ONECELL = 'oneCell';
13+
const EDIT_AS_TWOCELL = 'twoCell';
1414
private const VALID_EDIT_AS = [
1515
self::EDIT_AS_ABSOLUTE,
1616
self::EDIT_AS_ONECELL,
@@ -530,6 +530,6 @@ public function setEditAs(string $editAs): self
530530

531531
public function validEditAs(): bool
532532
{
533-
return in_array($this->editAs, self::VALID_EDIT_AS);
533+
return in_array($this->editAs, self::VALID_EDIT_AS, true);
534534
}
535535
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
6+
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
7+
8+
class DrawingsInsertRowsTest extends AbstractFunctional
9+
{
10+
/**
11+
* Test save and load XLSX file with drawing on 2nd worksheet.
12+
*/
13+
public function testSaveAfterInsertingRows(): void
14+
{
15+
// Read spreadsheet from file
16+
$inputFilename = 'tests/data/Writer/XLSX/issue.2908.xlsx';
17+
$reader = new Xlsx();
18+
$spreadsheet = $reader->load($inputFilename);
19+
$sheet = $spreadsheet->getActiveSheet();
20+
$drawingCollection = $sheet->getDrawingCollection();
21+
self::assertCount(1, $drawingCollection);
22+
$drawing = $drawingCollection[0];
23+
self::assertNotNull($drawing);
24+
self::assertSame('D10', $drawing->getCoordinates());
25+
self::assertSame('F11', $drawing->getCoordinates2());
26+
self::assertSame('oneCell', $drawing->getEditAs());
27+
28+
$sheet->insertNewRowBefore(5);
29+
$sheet->insertNewRowBefore(6);
30+
31+
// Save spreadsheet to file and read it back
32+
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
33+
$spreadsheet->disconnectWorksheets();
34+
$rsheet = $reloadedSpreadsheet->getActiveSheet();
35+
$drawingCollection2 = $rsheet->getDrawingCollection();
36+
self::assertCount(1, $drawingCollection2);
37+
$drawing2 = $drawingCollection2[0];
38+
self::assertNotNull($drawing2);
39+
self::assertSame('D12', $drawing2->getCoordinates());
40+
self::assertSame('F13', $drawing2->getCoordinates2());
41+
self::assertSame('oneCell', $drawing2->getEditAs());
42+
43+
$reloadedSpreadsheet->disconnectWorksheets();
44+
}
45+
}

tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,10 @@ public function providerEditAs(): array
554554
{
555555
return [
556556
'absolute' => ['absolute'],
557-
'onecell' => ['onecell'],
558-
'twocell' => ['twocell'],
559-
'unset (will be treated as twocell)' => [''],
560-
'unknown (will be treated as twocell)' => ['unknown', ''],
557+
'onecell' => ['oneCell'],
558+
'twocell' => ['twoCell'],
559+
'unset (will be treated as twoCell)' => [''],
560+
'unknown (will be treated as twoCell)' => ['unknown', ''],
561561
];
562562
}
563563

118 KB
Binary file not shown.

0 commit comments

Comments
 (0)