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.
This commit is contained in:
oleibman 2022-06-29 09:20:33 -07:00 committed by GitHub
parent a89572107a
commit b5b83abc0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 7 deletions

View File

@ -525,6 +525,12 @@ class ReferenceHelper
if ($objDrawing->getCoordinates() != $newReference) { if ($objDrawing->getCoordinates() != $newReference) {
$objDrawing->setCoordinates($newReference); $objDrawing->setCoordinates($newReference);
} }
if ($objDrawing->getCoordinates2() !== '') {
$newReference = $this->updateCellReference($objDrawing->getCoordinates2());
if ($objDrawing->getCoordinates2() != $newReference) {
$objDrawing->setCoordinates2($newReference);
}
}
} }
// Update workbook: define names // Update workbook: define names

View File

@ -9,8 +9,8 @@ use PhpOffice\PhpSpreadsheet\IComparable;
class BaseDrawing implements IComparable class BaseDrawing implements IComparable
{ {
const EDIT_AS_ABSOLUTE = 'absolute'; const EDIT_AS_ABSOLUTE = 'absolute';
const EDIT_AS_ONECELL = 'onecell'; const EDIT_AS_ONECELL = 'oneCell';
const EDIT_AS_TWOCELL = 'twocell'; const EDIT_AS_TWOCELL = 'twoCell';
private const VALID_EDIT_AS = [ private const VALID_EDIT_AS = [
self::EDIT_AS_ABSOLUTE, self::EDIT_AS_ABSOLUTE,
self::EDIT_AS_ONECELL, self::EDIT_AS_ONECELL,
@ -530,6 +530,6 @@ class BaseDrawing implements IComparable
public function validEditAs(): bool public function validEditAs(): bool
{ {
return in_array($this->editAs, self::VALID_EDIT_AS); return in_array($this->editAs, self::VALID_EDIT_AS, true);
} }
} }

View File

@ -0,0 +1,45 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
class DrawingsInsertRowsTest extends AbstractFunctional
{
/**
* Test save and load XLSX file with drawing on 2nd worksheet.
*/
public function testSaveAfterInsertingRows(): void
{
// Read spreadsheet from file
$inputFilename = 'tests/data/Writer/XLSX/issue.2908.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($inputFilename);
$sheet = $spreadsheet->getActiveSheet();
$drawingCollection = $sheet->getDrawingCollection();
self::assertCount(1, $drawingCollection);
$drawing = $drawingCollection[0];
self::assertNotNull($drawing);
self::assertSame('D10', $drawing->getCoordinates());
self::assertSame('F11', $drawing->getCoordinates2());
self::assertSame('oneCell', $drawing->getEditAs());
$sheet->insertNewRowBefore(5);
$sheet->insertNewRowBefore(6);
// Save spreadsheet to file and read it back
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
$spreadsheet->disconnectWorksheets();
$rsheet = $reloadedSpreadsheet->getActiveSheet();
$drawingCollection2 = $rsheet->getDrawingCollection();
self::assertCount(1, $drawingCollection2);
$drawing2 = $drawingCollection2[0];
self::assertNotNull($drawing2);
self::assertSame('D12', $drawing2->getCoordinates());
self::assertSame('F13', $drawing2->getCoordinates2());
self::assertSame('oneCell', $drawing2->getEditAs());
$reloadedSpreadsheet->disconnectWorksheets();
}
}

View File

@ -554,10 +554,10 @@ class DrawingsTest extends AbstractFunctional
{ {
return [ return [
'absolute' => ['absolute'], 'absolute' => ['absolute'],
'onecell' => ['onecell'], 'onecell' => ['oneCell'],
'twocell' => ['twocell'], 'twocell' => ['twoCell'],
'unset (will be treated as twocell)' => [''], 'unset (will be treated as twoCell)' => [''],
'unknown (will be treated as twocell)' => ['unknown', ''], 'unknown (will be treated as twoCell)' => ['unknown', ''],
]; ];
} }

Binary file not shown.