Skip to content

Commit 3174773

Browse files
authored
Merge pull request #4580 from oleibman/issue1457
splitRange and ProtectedRange
2 parents efa0c0f + ae1b517 commit 3174773

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org). Thia is a
99

1010
### Added
1111

12-
- Nothing yet.
12+
- Add Conditional Formatting with IconSet (Xlsx only). [Issue #4560](https://github.com/PHPOffice/PhpSpreadsheet/issues/4560) [PR #4574](https://github.com/PHPOffice/PhpSpreadsheet/pull/4574)
13+
- Copy cell adjusting formula. [Issue #1203](https://github.com/PHPOffice/PhpSpreadsheet/issues/1203) [PR #4577](https://github.com/PHPOffice/PhpSpreadsheet/pull/4577)
14+
- splitRange and ProtectedRange. [Issue #1457](https://github.com/PHPOffice/PhpSpreadsheet/issues/1457) [PR #4580](https://github.com/PHPOffice/PhpSpreadsheet/pull/4580)
1315

1416
### Removed
1517

@@ -29,7 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). Thia is a
2931

3032
### Fixed
3133

32-
- Nothing yet.
34+
- Google-only formulas exported from Google Sheets. [Issue #1637](https://github.com/PHPOffice/PhpSpreadsheet/issues/1637) [PR #4579](https://github.com/PHPOffice/PhpSpreadsheet/pull/4579)
3335

3436
## 2025-08-10 - 5.0.0
3537

src/PhpSpreadsheet/Cell/Coordinate.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public static function absoluteCoordinate(string $cellAddress): string
136136
}
137137

138138
/**
139-
* Split range into coordinate strings.
139+
* Split range into coordinate strings, using comma for union
140+
* and ignoring intersection (space).
140141
*
141142
* @param string $range e.g. 'B4:D9' or 'B4:D9,H2:O11' or 'B4'
142143
*
@@ -160,6 +161,28 @@ public static function splitRange(string $range): array
160161
return $outArray;
161162
}
162163

164+
/**
165+
* Split range into coordinate strings, resolving unions and intersections.
166+
*
167+
* @param string $range e.g. 'B4:D9' or 'B4:D9,H2:O11' or 'B4'
168+
* @param bool $unionIsComma true=comma is union, space is intersection
169+
* false=space is union, comma is intersection
170+
*
171+
* @return array<array<string>> Array containing one or more arrays containing one or two coordinate strings
172+
* e.g. ['B4','D9'] or [['B4','D9'], ['H2','O11']]
173+
* or ['B4']
174+
*/
175+
public static function allRanges(string $range, bool $unionIsComma = true): array
176+
{
177+
if (!$unionIsComma) {
178+
$range = str_replace([',', ' ', "\0"], ["\0", ',', ' '], $range);
179+
}
180+
181+
return self::splitRange(
182+
self::resolveUnionAndIntersection($range)
183+
);
184+
}
185+
163186
/**
164187
* Build range from coordinate strings.
165188
*

src/PhpSpreadsheet/Worksheet/ProtectedRange.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Worksheet;
44

5+
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6+
57
class ProtectedRange
68
{
79
private string $name = '';
@@ -42,4 +44,16 @@ public function getSecurityDescriptor(): string
4244
{
4345
return $this->securityDescriptor;
4446
}
47+
48+
/**
49+
* Split range into coordinate strings.
50+
*
51+
* @return array<array<string>> Array containing one or more arrays containing one or two coordinate strings
52+
* e.g. ['B4','D9'] or [['B4','D9'], ['H2','O11']]
53+
* or ['B4']
54+
*/
55+
public function allRanges(): array
56+
{
57+
return Coordinate::allRanges($this->sqref, false);
58+
}
4559
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
6+
7+
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue1457Test extends TestCase
11+
{
12+
public function testIssue1457(): void
13+
{
14+
$sheet = new Worksheet();
15+
$sheet->protectCells('C14:O15 C161:O1081 C16:H160 J16:O160 Q5');
16+
$protectedRanges = $sheet->getProtectedCellRanges();
17+
self::assertCount(1, $protectedRanges);
18+
$range0 = reset($protectedRanges);
19+
$expected = [
20+
['C14', 'O15'],
21+
['C161', 'O1081'],
22+
['C16', 'H160'],
23+
['J16', 'O160'],
24+
['Q5'],
25+
];
26+
self::assertSame($expected, $range0->allRanges());
27+
}
28+
}

0 commit comments

Comments
 (0)