Skip to content
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

Added Conditional Formatting: ColorScale for Xlsx #3738

Merged
merged 8 commits into from
Sep 30, 2023
Merged
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
64 changes: 64 additions & 0 deletions samples/ConditionalFormatting/cond08_colorscale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalColorScale;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalFormatValueObject;

require __DIR__ . '/../Header.php';

// Create new Spreadsheet object
$helper->log('Create new Spreadsheet object');
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// Set document properties
$helper->log('Set document properties');
$spreadsheet->getProperties()->setCreator('Owen Leibman')
->setLastModifiedBy('Owen Leibman')
->setTitle('PhpSpreadsheet Test Document')
->setSubject('PhpSpreadsheet Test Document')
->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.')
->setKeywords('office PhpSpreadsheet php')
->setCategory('Test result file');

// Create the worksheet
$helper->log('Add data');
$sheet
->setCellValue('A1', 1)
->setCellValue('A2', 2)
->setCellValue('A3', 8)
->setCellValue('A4', 4)
->setCellValue('A5', 5)
->setCellValue('A6', 6)
->setCellValue('A7', 7)
->setCellValue('A8', 3)
->setCellValue('A9', 9)
->setCellValue('A10', 10);

// Set conditional formatting rules and styles
$helper->log('Define conditional formatting using Color Scales');

$cellRange = 'A1:A10';
$condition1 = new Conditional();
$condition1->setConditionType(Conditional::CONDITION_COLORSCALE);
$colorScale = new ConditionalColorScale();
$condition1->setColorScale($colorScale);
$colorScale
->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject('min'))
->setMidpointConditionalFormatValueObject(new ConditionalFormatValueObject('percentile', '40'))
->setMaximumConditionalFormatValueObject(new ConditionalFormatValueObject('max'))
->setMinimumColor(new Color('FFF8696B'))
->setMidpointColor(new Color('FFFFEB84'))
->setMaximumColor(new Color('FF63BE7B'));

$conditionalStyles = [$condition1];

$sheet
->getStyle($cellRange)
->setConditionalStyles($conditionalStyles);
$sheet->setSelectedCells('B1');

// Save
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -790,10 +790,10 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
// Setting Conditional Styles adjusts selected cells, so we need to execute this
// before reading the sheet view data to get the actual selected cells
if (!$this->readDataOnly && ($xmlSheet->conditionalFormatting)) {
(new ConditionalStyles($docSheet, $xmlSheet, $dxfs))->load();
(new ConditionalStyles($docSheet, $xmlSheet, $dxfs, $this->styleReader))->load();
}
if (!$this->readDataOnly && $xmlSheet->extLst) {
(new ConditionalStyles($docSheet, $xmlSheet, $dxfs))->loadFromExt($this->styleReader);
(new ConditionalStyles($docSheet, $xmlSheet, $dxfs, $this->styleReader))->loadFromExt();
}
if (isset($xmlSheetMain->sheetViews, $xmlSheetMain->sheetViews->sheetView)) {
$sheetViews = new SheetViews($xmlSheetMain->sheetViews->sheetView, $docSheet);
Expand Down
64 changes: 54 additions & 10 deletions src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Styles as StyleReader;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalColorScale;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalDataBar;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalFormattingRuleExtension;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalFormatValueObject;
Expand All @@ -25,11 +27,14 @@ class ConditionalStyles

private array $dxfs;

public function __construct(Worksheet $workSheet, SimpleXMLElement $worksheetXml, array $dxfs = [])
private StyleReader $styleReader;

public function __construct(Worksheet $workSheet, SimpleXMLElement $worksheetXml, array $dxfs, StyleReader $styleReader)
{
$this->worksheet = $workSheet;
$this->worksheetXml = $worksheetXml;
$this->dxfs = $dxfs;
$this->styleReader = $styleReader;
}

public function load(): void
Expand All @@ -45,13 +50,13 @@ public function load(): void
$this->worksheet->setSelectedCells($selectedCells);
}

public function loadFromExt(StyleReader $styleReader): void
public function loadFromExt(): void
{
$selectedCells = $this->worksheet->getSelectedCells();

$this->ns = $this->worksheetXml->getNamespaces(true);
$this->setConditionalsFromExt(
$this->readConditionalsFromExt($this->worksheetXml->extLst, $styleReader)
$this->readConditionalsFromExt($this->worksheetXml->extLst)
);

$this->worksheet->setSelectedCells($selectedCells);
Expand All @@ -68,7 +73,7 @@ private function setConditionalsFromExt(array $conditionals): void
}
}

private function readConditionalsFromExt(SimpleXMLElement $extLst, StyleReader $styleReader): array
private function readConditionalsFromExt(SimpleXMLElement $extLst): array
{
$conditionals = [];
if (!isset($extLst->ext)) {
Expand Down Expand Up @@ -110,7 +115,7 @@ private function readConditionalsFromExt(SimpleXMLElement $extLst, StyleReader $
$priority = (int) $attributes->priority;

$conditional = $this->readConditionalRuleFromExt($extCfRuleXml, $attributes);
$cfStyle = $this->readStyleFromExt($extCfRuleXml, $styleReader);
$cfStyle = $this->readStyleFromExt($extCfRuleXml);
$conditional->setStyle($cfStyle);
$conditionals[$sqref][$priority] = $conditional;
}
Expand Down Expand Up @@ -146,17 +151,17 @@ private function readConditionalRuleFromExt(SimpleXMLElement $cfRuleXml, SimpleX
return $conditional;
}

private function readStyleFromExt(SimpleXMLElement $extCfRuleXml, StyleReader $styleReader): Style
private function readStyleFromExt(SimpleXMLElement $extCfRuleXml): Style
{
$cfStyle = new Style(false, true);
if ($extCfRuleXml->dxf) {
$styleXML = $extCfRuleXml->dxf->children();

if ($styleXML->borders) {
$styleReader->readBorderStyle($cfStyle->getBorders(), $styleXML->borders);
$this->styleReader->readBorderStyle($cfStyle->getBorders(), $styleXML->borders);
}
if ($styleXML->fill) {
$styleReader->readFillStyle($cfStyle->getFill(), $styleXML->fill);
$this->styleReader->readFillStyle($cfStyle->getFill(), $styleXML->fill);
}
}

Expand Down Expand Up @@ -198,6 +203,7 @@ private function readStyleRules(array $cfRules, SimpleXMLElement $extLst): array
$conditionalFormattingRuleExtensions = ConditionalFormattingRuleExtension::parseExtLstXml($extLst);
$conditionalStyles = [];

/** @var SimpleXMLElement $cfRule */
foreach ($cfRules as $cfRule) {
$objConditional = new Conditional();
$objConditional->setConditionType((string) $cfRule['type']);
Expand Down Expand Up @@ -231,7 +237,11 @@ private function readStyleRules(array $cfRules, SimpleXMLElement $extLst): array

if (isset($cfRule->dataBar)) {
$objConditional->setDataBar(
$this->readDataBarOfConditionalRule($cfRule, $conditionalFormattingRuleExtensions) // @phpstan-ignore-line
$this->readDataBarOfConditionalRule($cfRule, $conditionalFormattingRuleExtensions)
);
} elseif (isset($cfRule->colorScale)) {
$objConditional->setColorScale(
$this->readColorScale($cfRule)
);
} elseif (isset($cfRule['dxfId'])) {
$objConditional->setStyle(clone $this->dxfs[(int) ($cfRule['dxfId'])]);
Expand Down Expand Up @@ -270,14 +280,48 @@ private function readDataBarOfConditionalRule($cfRule, array $conditionalFormatt

//color
if (isset($cfRule->dataBar->color)) {
$dataBar->setColor((string) $cfRule->dataBar->color['rgb']);
$dataBar->setColor($this->styleReader->readColor($cfRule->dataBar->color));
}
//extLst
$this->readDataBarExtLstOfConditionalRule($dataBar, $cfRule, $conditionalFormattingRuleExtensions);

return $dataBar;
}

private function readColorScale(simpleXMLElement|stdClass $cfRule): ConditionalColorScale
{
$colorScale = new ConditionalColorScale();
$types = [];
foreach ($cfRule->colorScale->cfvo as $cfvoXml) {
$attr = $cfvoXml->attributes() ?? [];
$type = (string) ($attr['type'] ?? '');
$types[] = $type;
$val = $attr['val'] ?? null;
if ($type === 'min') {
$colorScale->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject($type, $val));
} elseif ($type === 'percentile') {
$colorScale->setMidpointConditionalFormatValueObject(new ConditionalFormatValueObject($type, $val));
} elseif ($type === 'max') {
$colorScale->setMaximumConditionalFormatValueObject(new ConditionalFormatValueObject($type, $val));
}
}
$idx = 0;
foreach ($cfRule->colorScale->color as $color) {
$type = $types[$idx];
$rgb = $this->styleReader->readColor($color);
if ($type === 'min') {
$colorScale->setMinimumColor(new Color($rgb));
} elseif ($type === 'percentile') {
$colorScale->setMidpointColor(new Color($rgb));
} elseif ($type === 'max') {
$colorScale->setMaximumColor(new Color($rgb));
}
++$idx;
}

return $colorScale;
}

/**
* @param SimpleXMLElement|stdClass $cfRule
*/
Expand Down
44 changes: 21 additions & 23 deletions src/PhpSpreadsheet/Style/Conditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheet\Style;

use PhpOffice\PhpSpreadsheet\IComparable;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalColorScale;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalDataBar;

class Conditional implements IComparable
Expand All @@ -11,6 +12,7 @@ class Conditional implements IComparable
const CONDITION_NONE = 'none';
const CONDITION_BEGINSWITH = 'beginsWith';
const CONDITION_CELLIS = 'cellIs';
const CONDITION_COLORSCALE = 'colorScale';
const CONDITION_CONTAINSBLANKS = 'containsBlanks';
const CONDITION_CONTAINSERRORS = 'containsErrors';
const CONDITION_CONTAINSTEXT = 'containsText';
Expand All @@ -27,6 +29,7 @@ class Conditional implements IComparable
private const CONDITION_TYPES = [
self::CONDITION_BEGINSWITH,
self::CONDITION_CELLIS,
self::CONDITION_COLORSCALE,
self::CONDITION_CONTAINSBLANKS,
self::CONDITION_CONTAINSERRORS,
self::CONDITION_CONTAINSTEXT,
Expand Down Expand Up @@ -91,10 +94,8 @@ class Conditional implements IComparable

/**
* Stop on this condition, if it matches.
*
* @var bool
*/
private $stopIfTrue = false;
private bool $stopIfTrue = false;

/**
* Condition.
Expand All @@ -103,18 +104,13 @@ class Conditional implements IComparable
*/
private $condition = [];

/**
* @var ConditionalDataBar
*/
private $dataBar;
private ?ConditionalDataBar $dataBar = null;

private ?ConditionalColorScale $colorScale = null;

/**
* Style.
*/
private Style $style;

/** @var bool */
private $noFormatSet = false;
private bool $noFormatSet = false;

/**
* Create a new Conditional.
Expand Down Expand Up @@ -296,28 +292,30 @@ public function setStyle(Style $style): static
return $this;
}

/**
* get DataBar.
*
* @return null|ConditionalDataBar
*/
public function getDataBar()
public function getDataBar(): ?ConditionalDataBar
{
return $this->dataBar;
}

/**
* set DataBar.
*
* @return $this
*/
public function setDataBar(ConditionalDataBar $dataBar): static
{
$this->dataBar = $dataBar;

return $this;
}

public function getColorScale(): ?ConditionalColorScale
{
return $this->colorScale;
}

public function setColorScale(ConditionalColorScale $colorScale): static
{
$this->colorScale = $colorScale;

return $this;
}

/**
* Get hash code.
*
Expand Down
Loading