From b05d07a365be2ceaa7a5062adaf07b8b80b7d158 Mon Sep 17 00:00:00 2001 From: Einar Lielmanis Date: Sun, 15 Jul 2018 06:19:54 +0300 Subject: [PATCH] Chained operations on cell ranges involving borders are now possible Fixes #428 Fixes #578 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Style/Style.php | 3 + .../Style/BorderRangeTest.php | 73 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 tests/PhpSpreadsheetTests/Style/BorderRangeTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index a1a8d4dd..9e252623 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Cell formats with escaped spaces were causing incorrect date formatting - [#557](https://github.com/PHPOffice/PhpSpreadsheet/issues/557) - Could not open CSV file containing HTML fragment - [#564](https://github.com/PHPOffice/PhpSpreadsheet/issues/564) - Exclude the vendor folder in migration - [#481](https://github.com/PHPOffice/PhpSpreadsheet/issues/481) +- Chained operations on cell ranges involving borders operated on last cell only [#428](https://github.com/PHPOffice/PhpSpreadsheet/issues/428) ## [1.3.1] - 2018-06-12 diff --git a/src/PhpSpreadsheet/Style/Style.php b/src/PhpSpreadsheet/Style/Style.php index c872976e..3a8ebaad 100644 --- a/src/PhpSpreadsheet/Style/Style.php +++ b/src/PhpSpreadsheet/Style/Style.php @@ -333,6 +333,9 @@ class Style extends Supervisor } } + // restore initial cell selection range + $this->getActiveSheet()->getStyle($pRange); + return $this; } diff --git a/tests/PhpSpreadsheetTests/Style/BorderRangeTest.php b/tests/PhpSpreadsheetTests/Style/BorderRangeTest.php new file mode 100644 index 00000000..115e830d --- /dev/null +++ b/tests/PhpSpreadsheetTests/Style/BorderRangeTest.php @@ -0,0 +1,73 @@ +getActiveSheet(); + + $argb = 'FFFF0000'; + $color = new Color($argb); + + $sheet->getStyle('A1:C1')->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN)->setColor($color); + $sheet->getStyle('A1:A3')->getBorders()->getLeft()->setBorderStyle(Border::BORDER_THIN)->setColor($color); + $sheet->getStyle('C1:C3')->getBorders()->getRight()->setBorderStyle(Border::BORDER_THIN)->setColor($color); + $sheet->getStyle('A3:C3')->getBorders()->getBottom()->setBorderStyle(Border::BORDER_THIN)->setColor($color); + + // upper row + $expectations = [ + // cell => Left/Right/Top/Bottom + 'A1' => 'LT', + 'B1' => 'T', + 'C1' => 'RT', + 'A2' => 'L', + 'B2' => '', + 'C2' => 'R', + 'A3' => 'LB', + 'B3' => 'B', + 'C3' => 'RB', + ]; + $sides = [ + 'L' => 'Left', + 'R' => 'Right', + 'T' => 'Top', + 'B' => 'Bottom', + ]; + + foreach ($expectations as $cell => $borders) { + $bs = $sheet->getStyle($cell)->getBorders(); + foreach ($sides as $sidekey => $side) { + $assertion = "setBorderStyle on a range of cells, $cell $side"; + $func = "get$side"; + $b = $bs->$func(); // boo + + if (strpos($borders, $sidekey) === false) { + self::assertSame(Border::BORDER_NONE, $b->getBorderStyle(), $assertion); + } else { + self::assertSame(Border::BORDER_THIN, $b->getBorderStyle(), $assertion); + self::assertSame($argb, $b->getColor()->getARGB(), $assertion); + } + } + } + } + + public function testBorderRangeDirectly() + { + // testcase for the underlying problem directly + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $style = $sheet->getStyle('A1:C1')->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN); + self::assertSame('A1:C1', $style->getSelectedCells(), 'getSelectedCells should not change after a style operation on a border range'); + } +}