Allow single-cell checks on conditional styles, even when the style is configured for a range of cells (#2483)

* Allow single-cell checks on conditional styles, even when the style is configured for a range of cells
This commit is contained in:
Mark Baker 2022-01-05 13:39:50 +01:00 committed by GitHub
parent 778e24cf73
commit b13b2a0d59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 124 additions and 6 deletions

View File

@ -1432,32 +1432,59 @@ class Worksheet implements IComparable
/**
* Get conditional styles for a cell.
*
* @param string $coordinate eg: 'A1'
* @param string $coordinate eg: 'A1' or 'A1:A3'.
* If a single cell is referenced, then the array of conditional styles will be returned if the cell is
* included in a conditional style range.
* If a range of cells is specified, then the styles will only be returned if the range matches the entire
* range of the conditional.
*
* @return Conditional[]
*/
public function getConditionalStyles($coordinate)
{
$coordinate = strtoupper($coordinate);
if (!isset($this->conditionalStylesCollection[$coordinate])) {
$this->conditionalStylesCollection[$coordinate] = [];
if (strpos($coordinate, ':') !== false) {
return $this->conditionalStylesCollection[$coordinate] ?? [];
}
return $this->conditionalStylesCollection[$coordinate];
$cell = $this->getCell($coordinate);
foreach (array_keys($this->conditionalStylesCollection) as $conditionalRange) {
if ($cell->isInRange($conditionalRange)) {
return $this->conditionalStylesCollection[$conditionalRange];
}
}
return [];
}
/**
* Do conditional styles exist for this cell?
*
* @param string $coordinate eg: 'A1'
* @param string $coordinate eg: 'A1' or 'A1:A3'.
* If a single cell is specified, then this method will return true if that cell is included in a
* conditional style range.
* If a range of cells is specified, then true will only be returned if the range matches the entire
* range of the conditional.
*
* @return bool
*/
public function conditionalStylesExists($coordinate)
{
$coordinate = strtoupper($coordinate);
if (strpos($coordinate, ':') !== false) {
return isset($this->conditionalStylesCollection[strtoupper($coordinate)]);
}
$cell = $this->getCell($coordinate);
foreach (array_keys($this->conditionalStylesCollection) as $conditionalRange) {
if ($cell->isInRange($conditionalRange)) {
return true;
}
}
return false;
}
/**
* Removes conditional styles for a cell.
*

View File

@ -0,0 +1,91 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PHPUnit\Framework\TestCase;
class ConditionalStyleTest extends TestCase
{
/**
* @var Spreadsheet
*/
protected $spreadsheet;
protected function setUp(): void
{
parent::setUp();
$this->spreadsheet = new Spreadsheet();
$conditional1 = new Conditional();
$conditional1->setConditionType(Conditional::CONDITION_CELLIS);
$conditional1->setOperatorType(Conditional::OPERATOR_LESSTHAN);
$conditional1->addCondition('0');
$conditional1->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_RED);
$conditional1->getStyle()->getFont()->setBold(true);
$conditional2 = new Conditional();
$conditional2->setConditionType(Conditional::CONDITION_CELLIS);
$conditional2->setOperatorType(Conditional::OPERATOR_EQUAL);
$conditional2->addCondition('0');
$conditional2->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_YELLOW);
$conditional2->getStyle()->getFont()->setBold(true);
$conditional3 = new Conditional();
$conditional3->setConditionType(Conditional::CONDITION_CELLIS);
$conditional3->setOperatorType(Conditional::OPERATOR_GREATERTHAN);
$conditional3->addCondition('0');
$conditional3->getStyle()->getFont()->getColor()->setARGB(Color::COLOR_GREEN);
$conditional3->getStyle()->getFont()->setBold(true);
$conditionalStyles = $this->spreadsheet->getActiveSheet()->getStyle('A1:C3')->getConditionalStyles();
$conditionalStyles[] = $conditional1;
$conditionalStyles[] = $conditional2;
$conditionalStyles[] = $conditional3;
$this->spreadsheet->getActiveSheet()->getStyle('A1:C3')->setConditionalStyles($conditionalStyles);
$this->spreadsheet->getActiveSheet()
->duplicateConditionalStyle(
$this->spreadsheet->getActiveSheet()->getConditionalStyles('A1:C3'),
'F1'
);
}
/**
* @dataProvider cellConditionalStylesProvider
*/
public function testCellHasConditionalStyles(string $cellReference, bool $expectedHasConditionalStyles): void
{
$cellHasConditionalStyles = $this->spreadsheet->getActiveSheet()->conditionalStylesExists($cellReference);
self::assertSame($expectedHasConditionalStyles, $cellHasConditionalStyles);
}
/**
* @dataProvider cellConditionalStylesProvider
*/
public function testCellGetConditionalStyles(string $cellReference, bool $expectedGetConditionalStyles): void
{
$cellHasConditionalStyles = $this->spreadsheet->getActiveSheet()->getConditionalStyles($cellReference);
self::assertSame($expectedGetConditionalStyles, !empty($cellHasConditionalStyles));
}
public function cellConditionalStylesProvider(): array
{
return [
['A1', true],
['B2', true],
['B4', false],
['A1:C3', true],
['A1:B2', false],
['F1', true],
['F2', false],
['A1:F1', false],
];
}
}