Repository: PHPOffice/PhpSpreadsheet (php)

Issue:
Possible bug in ReferenceHelper::InsertNewBefore
This is:

```
- [X] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)
```

### What is the expected behavior?
That the styles of all of the newly inserted column rows will be updated

### What is the current behavior?
The styles of the last row of the inserted columns is not updated. This appears to be due to the following line of code:

458:  for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) {

which is seemingly terminating the loop one row too early

### What are the steps to reproduce?
Add one column to a table with two rows in it using insertNewColumnBefore()

```php
    $sheet->insertNewColumnBefore($sheet->getHighestColumn(), 1);
```


### Which versions of PhpSpreadsheet and PHP are affected?
1.11 and php 7.3


Pull request description:
Copy Styles after insertNewColumnBefore
Fix #1425 which went stale 5 years ago, and which I've reopened. Style was not being set properly for last row. Also, Style was not being set at all for insertNewColumnBefore('B').

This is:

- [x] a bugfix
- [ ] a new feature
- [ ] refactoring
- [ ] additional unit tests

Checklist:

- [ ] Changes are covered by unit tests
  - [ ] Changes are covered by existing unit tests
  - [x] New unit tests have been added
- [x] Code style is respected
- [x] Commit message explains **why** the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
- [ ] CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
- [ ] Documentation is updated as necessary



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f68ecc7c2..3420e60a4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
 - Micro-optimization for excelToDateTimeObject. [Issue #4438](https://github.com/PHPOffice/PhpSpreadsheet/issues/4438) [PR #4442](https://github.com/PHPOffice/PhpSpreadsheet/pull/4442)
 - Removing Columns/Rows Containing Merged Cells. [Issue #282](https://github.com/PHPOffice/PhpSpreadsheet/issues/282) [PR #4465](https://github.com/PHPOffice/PhpSpreadsheet/pull/4465)
 - Print Area and Row Break. [Issue #1275](https://github.com/PHPOffice/PhpSpreadsheet/issues/1275) [PR #4450](https://github.com/PHPOffice/PhpSpreadsheet/pull/4450)
+- Copy Styles after insertNewColumnBefore. [Issue #1425](https://github.com/PHPOffice/PhpSpreadsheet/issues/1425) [PR #4468](https://github.com/PHPOffice/PhpSpreadsheet/pull/4468)
 - 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)
 - ODS Handling of Ceiling and Floor. [Issue #407](https://github.com/PHPOffice/PhpSpreadsheet/issues/407) [PR #4466](https://github.com/PHPOffice/PhpSpreadsheet/pull/4466)
 
diff --git a/src/PhpSpreadsheet/ReferenceHelper.php b/src/PhpSpreadsheet/ReferenceHelper.php
index bfafc971b..1abcb8496 100644
--- a/src/PhpSpreadsheet/ReferenceHelper.php
+++ b/src/PhpSpreadsheet/ReferenceHelper.php
@@ -506,7 +506,7 @@ class ReferenceHelper
         $highestColumn = $worksheet->getHighestColumn();
         $highestRow = $worksheet->getHighestRow();
 
-        if ($numberOfColumns > 0 && $beforeColumn - 2 > 0) {
+        if ($numberOfColumns > 0 && $beforeColumn > 1) {
             $this->duplicateStylesByColumn($worksheet, $beforeColumn, $beforeRow, $highestRow, $numberOfColumns);
         }
 
@@ -1242,7 +1242,7 @@ class ReferenceHelper
     private function duplicateStylesByColumn(Worksheet $worksheet, int $beforeColumn, int $beforeRow, int $highestRow, int $numberOfColumns): void
     {
         $beforeColumnName = Coordinate::stringFromColumnIndex($beforeColumn - 1);
-        for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) {
+        for ($i = $beforeRow; $i <= $highestRow; ++$i) {
             // Style
             $coordinate = $beforeColumnName . $i;
             if ($worksheet->cellExists($coordinate)) {


Test patch:
diff --git a/tests/PhpSpreadsheetTests/Worksheet/Issue1425Test.php b/tests/PhpSpreadsheetTests/Worksheet/Issue1425Test.php
new file mode 100644
index 000000000..df1cf3691
--- /dev/null
+++ b/tests/PhpSpreadsheetTests/Worksheet/Issue1425Test.php
@@ -0,0 +1,57 @@
+<?php
+
+declare(strict_types=1);
+
+namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
+
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Style\Fill;
+use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
+use PHPUnit\Framework\TestCase;
+
+class Issue1425Test extends TestCase
+{
+    private static function setBackground(Worksheet $sheet, string $cells, string $color = 'ffffff00'): void
+    {
+        $sheet->getStyle($cells)
+            ->getFill()
+            ->setFillType(Fill::FILL_SOLID);
+        $sheet->getStyle($cells)
+            ->getFill()
+            ->getStartColor()
+            ->setArgb($color);
+    }
+
+    /**
+     * Not extending styles to all cells after insertNewColumnBefore.
+     */
+    public function testIssue4125(): void
+    {
+        $spreadsheet = new Spreadsheet();
+        $sheet = $spreadsheet->getActiveSheet();
+        $sheet->setTitle('Before');
+        $sheet->getCell('A1')->setValue(1);
+        $sheet->getCell('A2')->setValue(2);
+        $sheet->getCell('B1')->setValue(3);
+        $sheet->getCell('B2')->setValue(4);
+        self::setBackground($sheet, 'A1:A2');
+        self::setBackground($sheet, 'B1:B2', 'FF00FFFF');
+
+        $sheet->insertNewColumnBefore('B', 1);
+
+        $expected = [
+            'A1' => 'ffffff00',
+            'A2' => 'ffffff00',
+            'B1' => 'ffffff00',
+            'B2' => 'ffffff00',
+            'C1' => 'FF00FFFF',
+            'C2' => 'FF00FFFF',
+        ];
+        foreach ($expected as $key => $value) {
+            $color = $sheet->getStyle($key)
+                ->getFill()->getStartColor()->getArgb();
+            self::assertSame($value, $color, "error in cell $key");
+        }
+        $spreadsheet->disconnectWorksheets();
+    }
+}