diff --git a/src/PhpSpreadsheet/Cell/Coordinate.php b/src/PhpSpreadsheet/Cell/Coordinate.php index 03c475fb66..227f2c1170 100644 --- a/src/PhpSpreadsheet/Cell/Coordinate.php +++ b/src/PhpSpreadsheet/Cell/Coordinate.php @@ -136,7 +136,8 @@ public static function absoluteCoordinate(string $cellAddress): string } /** - * Split range into coordinate strings. + * Split range into coordinate strings, using comma for union + * and ignoring intersection (space). * * @param string $range e.g. 'B4:D9' or 'B4:D9,H2:O11' or 'B4' * @@ -160,6 +161,28 @@ public static function splitRange(string $range): array return $outArray; } + /** + * Split range into coordinate strings, resolving unions and intersections. + * + * @param string $range e.g. 'B4:D9' or 'B4:D9,H2:O11' or 'B4' + * @param bool $unionIsComma true=comma is union, space is intersection + * false=space is union, comma is intersection + * + * @return array> Array containing one or more arrays containing one or two coordinate strings + * e.g. ['B4','D9'] or [['B4','D9'], ['H2','O11']] + * or ['B4'] + */ + public static function allRanges(string $range, bool $unionIsComma = true): array + { + if (!$unionIsComma) { + $range = str_replace([',', ' ', "\0"], ["\0", ',', ' '], $range); + } + + return self::splitRange( + self::resolveUnionAndIntersection($range) + ); + } + /** * Build range from coordinate strings. * diff --git a/src/PhpSpreadsheet/Worksheet/ProtectedRange.php b/src/PhpSpreadsheet/Worksheet/ProtectedRange.php index bd4197628f..13c0d50bf1 100644 --- a/src/PhpSpreadsheet/Worksheet/ProtectedRange.php +++ b/src/PhpSpreadsheet/Worksheet/ProtectedRange.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheet\Worksheet; +use PhpOffice\PhpSpreadsheet\Cell\Coordinate; + class ProtectedRange { private string $name = ''; @@ -42,4 +44,16 @@ public function getSecurityDescriptor(): string { return $this->securityDescriptor; } + + /** + * Split range into coordinate strings. + * + * @return array> Array containing one or more arrays containing one or two coordinate strings + * e.g. ['B4','D9'] or [['B4','D9'], ['H2','O11']] + * or ['B4'] + */ + public function allRanges(): array + { + return Coordinate::allRanges($this->sqref, false); + } } diff --git a/tests/PhpSpreadsheetTests/Worksheet/Issue1457Test.php b/tests/PhpSpreadsheetTests/Worksheet/Issue1457Test.php new file mode 100644 index 0000000000..dbdead3745 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Worksheet/Issue1457Test.php @@ -0,0 +1,28 @@ +protectCells('C14:O15 C161:O1081 C16:H160 J16:O160 Q5'); + $protectedRanges = $sheet->getProtectedCellRanges(); + self::assertCount(1, $protectedRanges); + $range0 = reset($protectedRanges); + $expected = [ + ['C14', 'O15'], + ['C161', 'O1081'], + ['C16', 'H160'], + ['J16', 'O160'], + ['Q5'], + ]; + self::assertSame($expected, $range0->allRanges()); + } +}