Skip to content

splitRange and ProtectedRange #4580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/PhpSpreadsheet/Cell/Coordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
*
Expand All @@ -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<string>> 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.
*
Expand Down
14 changes: 14 additions & 0 deletions src/PhpSpreadsheet/Worksheet/ProtectedRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpOffice\PhpSpreadsheet\Worksheet;

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

class ProtectedRange
{
private string $name = '';
Expand Down Expand Up @@ -42,4 +44,16 @@ public function getSecurityDescriptor(): string
{
return $this->securityDescriptor;
}

/**
* Split range into coordinate strings.
*
* @return array<array<string>> 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);
}
}
28 changes: 28 additions & 0 deletions tests/PhpSpreadsheetTests/Worksheet/Issue1457Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Worksheet;

use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;

class Issue1457Test extends TestCase
{
public function testIssue1457(): void
{
$sheet = new Worksheet();
$sheet->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());
}
}