Skip to content

Commit 4500f5a

Browse files
committed
Worksheet applyStylesFromArray Retain Active Cell
Fix #4128. PR #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.
1 parent 3b15055 commit 4500f5a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3682,7 +3682,9 @@ public function applyStylesFromArray(string $coordinate, array $styleArray): boo
36823682
}
36833683
$activeSheetIndex = $spreadsheet->getActiveSheetIndex();
36843684
$originalSelected = $this->selectedCells;
3685+
$originalActive = $this->activeCell;
36853686
$this->getStyle($coordinate)->applyFromArray($styleArray);
3687+
$this->activeCell = $originalActive;
36863688
$this->selectedCells = $originalSelected;
36873689
if ($activeSheetIndex >= 0) {
36883690
$spreadsheet->setActiveSheetIndex($activeSheetIndex);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue4128Test extends TestCase
11+
{
12+
public function testIssue4128(): void
13+
{
14+
$spreadsheet = new Spreadsheet();
15+
$sheet = $spreadsheet->getActiveSheet();
16+
self::assertSame('A1', $sheet->getActiveCell());
17+
self::assertSame('A1', $sheet->getSelectedCells());
18+
$sheet->setCellValue('D1', 'MyDate');
19+
self::assertSame('A1', $sheet->getActiveCell());
20+
self::assertSame('A1', $sheet->getSelectedCells());
21+
$spreadsheet->disconnectWorksheets();
22+
}
23+
}

0 commit comments

Comments
 (0)