Skip to content

Commit

Permalink
Worksheet applyStylesFromArray Retain Active Cell
Browse files Browse the repository at this point in the history
Fix PHPOffice#4128. PR PHPOffice#4073 introduced applyStylesFromArray method, which allowed setting styles without affecting selectedCells or activeSheet. The first use of this method was in Cell setValueExplicit to set quotePrefix appropriately. The new method did not preserve activeCell. I'm not sure why that should matter, but this seems to have caused a problem for Excel 2016. This seems to be a bug in Excel, one which is fixed in newer releases. However, PhpSpreadsheet can avoid the problem by preserving activeCell as well as selectedCells and activeSheet. This PR makes that change.
  • Loading branch information
oleibman committed Aug 7, 2024
1 parent 3b15055 commit 4500f5a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3682,7 +3682,9 @@ public function applyStylesFromArray(string $coordinate, array $styleArray): boo
}
$activeSheetIndex = $spreadsheet->getActiveSheetIndex();
$originalSelected = $this->selectedCells;
$originalActive = $this->activeCell;
$this->getStyle($coordinate)->applyFromArray($styleArray);
$this->activeCell = $originalActive;
$this->selectedCells = $originalSelected;
if ($activeSheetIndex >= 0) {
$spreadsheet->setActiveSheetIndex($activeSheetIndex);
Expand Down
23 changes: 23 additions & 0 deletions tests/PhpSpreadsheetTests/Worksheet/Issue4128Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Worksheet;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;

class Issue4128Test extends TestCase
{
public function testIssue4128(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('A1', $sheet->getActiveCell());
self::assertSame('A1', $sheet->getSelectedCells());
$sheet->setCellValue('D1', 'MyDate');
self::assertSame('A1', $sheet->getActiveCell());
self::assertSame('A1', $sheet->getSelectedCells());
$spreadsheet->disconnectWorksheets();
}
}

0 comments on commit 4500f5a

Please sign in to comment.