Skip to content

Commit 85a9a39

Browse files
authored
Merge pull request #4468 from oleibman/issue1425
Copy Styles after insertNewColumnBefore
2 parents e16571e + a6e2e71 commit 85a9a39

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3232
- Micro-optimization for excelToDateTimeObject. [Issue #4438](https://github.com/PHPOffice/PhpSpreadsheet/issues/4438) [PR #4442](https://github.com/PHPOffice/PhpSpreadsheet/pull/4442)
3333
- Removing Columns/Rows Containing Merged Cells. [Issue #282](https://github.com/PHPOffice/PhpSpreadsheet/issues/282) [PR #4465](https://github.com/PHPOffice/PhpSpreadsheet/pull/4465)
3434
- Print Area and Row Break. [Issue #1275](https://github.com/PHPOffice/PhpSpreadsheet/issues/1275) [PR #4450](https://github.com/PHPOffice/PhpSpreadsheet/pull/4450)
35+
- Copy Styles after insertNewColumnBefore. [Issue #1425](https://github.com/PHPOffice/PhpSpreadsheet/issues/1425) [PR #4468](https://github.com/PHPOffice/PhpSpreadsheet/pull/4468)
3536
- Xls Writer Treat Hyperlink Starting with # as Internal. [Issue #56](https://github.com/PHPOffice/PhpSpreadsheet/issues/56) [PR #4453](https://github.com/PHPOffice/PhpSpreadsheet/pull/4453)
3637
- ODS Handling of Ceiling and Floor. [Issue #407](https://github.com/PHPOffice/PhpSpreadsheet/issues/407) [PR #4466](https://github.com/PHPOffice/PhpSpreadsheet/pull/4466)
3738

src/PhpSpreadsheet/ReferenceHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ public function insertNewBefore(
506506
$highestColumn = $worksheet->getHighestColumn();
507507
$highestRow = $worksheet->getHighestRow();
508508

509-
if ($numberOfColumns > 0 && $beforeColumn - 2 > 0) {
509+
if ($numberOfColumns > 0 && $beforeColumn > 1) {
510510
$this->duplicateStylesByColumn($worksheet, $beforeColumn, $beforeRow, $highestRow, $numberOfColumns);
511511
}
512512

@@ -1242,7 +1242,7 @@ private function adjustTableDelete(int $startCol, int $numberOfColumns, int $ran
12421242
private function duplicateStylesByColumn(Worksheet $worksheet, int $beforeColumn, int $beforeRow, int $highestRow, int $numberOfColumns): void
12431243
{
12441244
$beforeColumnName = Coordinate::stringFromColumnIndex($beforeColumn - 1);
1245-
for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) {
1245+
for ($i = $beforeRow; $i <= $highestRow; ++$i) {
12461246
// Style
12471247
$coordinate = $beforeColumnName . $i;
12481248
if ($worksheet->cellExists($coordinate)) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PhpOffice\PhpSpreadsheet\Style\Fill;
9+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class Issue1425Test extends TestCase
13+
{
14+
private static function setBackground(Worksheet $sheet, string $cells, string $color = 'ffffff00'): void
15+
{
16+
$sheet->getStyle($cells)
17+
->getFill()
18+
->setFillType(Fill::FILL_SOLID);
19+
$sheet->getStyle($cells)
20+
->getFill()
21+
->getStartColor()
22+
->setArgb($color);
23+
}
24+
25+
/**
26+
* Not extending styles to all cells after insertNewColumnBefore.
27+
*/
28+
public function testIssue4125(): void
29+
{
30+
$spreadsheet = new Spreadsheet();
31+
$sheet = $spreadsheet->getActiveSheet();
32+
$sheet->setTitle('Before');
33+
$sheet->getCell('A1')->setValue(1);
34+
$sheet->getCell('A2')->setValue(2);
35+
$sheet->getCell('B1')->setValue(3);
36+
$sheet->getCell('B2')->setValue(4);
37+
self::setBackground($sheet, 'A1:A2');
38+
self::setBackground($sheet, 'B1:B2', 'FF00FFFF');
39+
40+
$sheet->insertNewColumnBefore('B', 1);
41+
42+
$expected = [
43+
'A1' => 'ffffff00',
44+
'A2' => 'ffffff00',
45+
'B1' => 'ffffff00',
46+
'B2' => 'ffffff00',
47+
'C1' => 'FF00FFFF',
48+
'C2' => 'FF00FFFF',
49+
];
50+
foreach ($expected as $key => $value) {
51+
$color = $sheet->getStyle($key)
52+
->getFill()->getStartColor()->getArgb();
53+
self::assertSame($value, $color, "error in cell $key");
54+
}
55+
$spreadsheet->disconnectWorksheets();
56+
}
57+
}

0 commit comments

Comments
 (0)