mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-30 20:18:00 +00:00
Xls Conditional Format Improvements
While researching another problem, I noticed that font color was not working as expected for Xls Conditional Formats, at least not when a "non-standard" color is used. In such cases, the color might wind up being rendered as black. The reason is as follows. Xls Writer includes a color palette which is dynamically generated from the (non-Conditional) styles used in the workbook. Any colors used in the workbook are indexes to this dynamic palette. However, Conditional colors use a static palette found in class ColorMap to determine the index, so the determination of index will often not find a match, and, if a match is found, it is not necessarily correct. (Also, the ColorMap method was case-sensitive and needs to be insensitive.) In order to correct this, the `addColor` method in Xls Writer Workbook needs to be accessible to the Conditional logic which is found in Xls Writer Worksheet. This is accomplished by passing the Workbook in the Worksheet's constructor, and changing the method to public, and changing Conditional Font to use this method rather than ColorMap. The logic for Conditional Fill colors is similarly changed. Although Xls Conditional Fill has appeared to just not work, I was finally able to figure out the problem. Excel Xls Conditional Fill with fill type Solid requires that the fill color be specified as startColor, and that endColor be omitted. Our conditional samples used endColor, and are now changed to use startColor instead; the same is true for our online documentation, and for some tests. Xlsx continues to work as expected, and now Xls does at least some of the time. If the condition is one that Excel Xls does not recognize (e.g. cell contains), it will, of course, not work. A surprising situation that also doesn't work is the use of ISODD or ISEVEN in formulas. Those are "add-in functions" which are handled differently than other functions, and I'm not sure how to support them. I will document this in issue #3403. Samples 08_Conditional_Formatting(_2) had produced corrupt Xls versions. This turned out to be because the code was using hash codes to avoid having to write out duplicate conditionals; this is often a good idea, but not in this case. Allowing the duplicates fixes the corruption problem. Conditional Border colors also ought to figure in this change, but the current code does not support Border colors, and I have not yet been able to figure out how to implement it (BIFF format can be very messy to figure out). With this change, I could delete ColorMap altogether. However, it is a public class with a static public method, so maybe someone is using it for a purpose I'm not familiar with. I will just deprecate it.
This commit is contained in:
@@ -43,7 +43,7 @@ $conditional->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERA
|
||||
$conditional->addCondition(80);
|
||||
$conditional->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKGREEN);
|
||||
$conditional->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
|
||||
$conditional->getStyle()->getFill()->getEndColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
|
||||
$conditional->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
|
||||
|
||||
$conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('A1:A10')->getConditionalStyles();
|
||||
$conditionalStyles[] = $conditional;
|
||||
@@ -63,7 +63,7 @@ $wizard = $wizardFactory->newRule(\PhpOffice\PhpSpreadsheet\Style\ConditionalFor
|
||||
$wizard->greaterThan(80);
|
||||
$wizard->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKGREEN);
|
||||
$wizard->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
|
||||
$wizard->getStyle()->getFill()->getEndColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
|
||||
$wizard->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
|
||||
|
||||
$conditional = $wizard->getConditional();
|
||||
```
|
||||
@@ -84,7 +84,7 @@ $conditional2->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPER
|
||||
$conditional2->addCondition(10);
|
||||
$conditional2->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKRED);
|
||||
$conditional2->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
|
||||
$conditional2->getStyle()->getFill()->getEndColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
|
||||
$conditional2->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
|
||||
|
||||
$conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('A1:A10')->getConditionalStyles();
|
||||
$conditionalStyles[] = $conditional2;
|
||||
@@ -98,7 +98,7 @@ $wizard = $wizardFactory->newRule(\PhpOffice\PhpSpreadsheet\Style\ConditionalFor
|
||||
$wizard->lessThan(10);
|
||||
$wizard->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKGREEN);
|
||||
$wizard->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
|
||||
$wizard->getStyle()->getFill()->getEndColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
|
||||
$wizard->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
|
||||
|
||||
$conditional = $wizard->getConditional();
|
||||
```
|
||||
|
||||
@@ -74,22 +74,16 @@ $yellowStyle = new Style(false, true);
|
||||
$yellowStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getStartColor()->setARGB(Color::COLOR_YELLOW);
|
||||
$yellowStyle->getFill()
|
||||
->getEndColor()->setARGB(Color::COLOR_YELLOW);
|
||||
$yellowStyle->getFont()->setColor(new Color(Color::COLOR_BLUE));
|
||||
$greenStyle = new Style(false, true);
|
||||
$greenStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getStartColor()->setARGB(Color::COLOR_GREEN);
|
||||
$greenStyle->getFill()
|
||||
->getEndColor()->setARGB(Color::COLOR_GREEN);
|
||||
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
|
||||
$redStyle = new Style(false, true);
|
||||
$redStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getStartColor()->setARGB(Color::COLOR_RED);
|
||||
$redStyle->getFill()
|
||||
->getEndColor()->setARGB(Color::COLOR_RED);
|
||||
$redStyle->getFont()->setColor(new Color(Color::COLOR_GREEN));
|
||||
|
||||
// Set conditional formatting rules and styles
|
||||
|
||||
@@ -73,17 +73,17 @@ $helper->log('Define some styles for our Conditionals');
|
||||
$yellowStyle = new Style(false, true);
|
||||
$yellowStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_YELLOW);
|
||||
->getStartColor()->setARGB(Color::COLOR_YELLOW);
|
||||
$yellowStyle->getFont()->setColor(new Color(Color::COLOR_BLUE));
|
||||
$greenStyle = new Style(false, true);
|
||||
$greenStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_GREEN);
|
||||
->getStartColor()->setARGB(Color::COLOR_GREEN);
|
||||
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
|
||||
$redStyle = new Style(false, true);
|
||||
$redStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_RED);
|
||||
->getStartColor()->setARGB(Color::COLOR_RED);
|
||||
$redStyle->getFont()->setColor(new Color(Color::COLOR_GREEN));
|
||||
|
||||
// Set conditional formatting rules and styles
|
||||
|
||||
@@ -45,12 +45,12 @@ $helper->log('Define some styles for our Conditionals');
|
||||
$greenStyle = new Style(false, true);
|
||||
$greenStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_GREEN);
|
||||
->getStartColor()->setARGB(Color::COLOR_GREEN);
|
||||
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
|
||||
$redStyle = new Style(false, true);
|
||||
$redStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_RED);
|
||||
->getStartColor()->setARGB(Color::COLOR_RED);
|
||||
$redStyle->getFont()->setColor(new Color(Color::COLOR_GREEN));
|
||||
|
||||
// Set conditional formatting rules and styles
|
||||
|
||||
@@ -48,12 +48,12 @@ $helper->log('Define some styles for our Conditionals');
|
||||
$greenStyle = new Style(false, true);
|
||||
$greenStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_GREEN);
|
||||
->getStartColor()->setARGB(Color::COLOR_GREEN);
|
||||
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
|
||||
$redStyle = new Style(false, true);
|
||||
$redStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_RED);
|
||||
->getStartColor()->setARGB(Color::COLOR_RED);
|
||||
$redStyle->getFont()->setColor(new Color(Color::COLOR_GREEN));
|
||||
|
||||
// Set conditional formatting rules and styles
|
||||
|
||||
@@ -111,7 +111,7 @@ $helper->log('Define some styles for our Conditionals');
|
||||
$yellowStyle = new Style(false, true);
|
||||
$yellowStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_YELLOW);
|
||||
->getStartColor()->setARGB(Color::COLOR_YELLOW);
|
||||
$yellowStyle->getFont()->setColor(new Color(Color::COLOR_BLUE));
|
||||
|
||||
// Set conditional formatting rules and styles
|
||||
|
||||
@@ -54,12 +54,12 @@ $helper->log('Define some styles for our Conditionals');
|
||||
$yellowStyle = new Style(false, true);
|
||||
$yellowStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_YELLOW);
|
||||
->getStartColor()->setARGB(Color::COLOR_YELLOW);
|
||||
$yellowStyle->getFont()->setColor(new Color(Color::COLOR_BLUE));
|
||||
$greenStyle = new Style(false, true);
|
||||
$greenStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_GREEN);
|
||||
->getStartColor()->setARGB(Color::COLOR_GREEN);
|
||||
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
|
||||
|
||||
// Set conditional formatting rules and styles
|
||||
|
||||
@@ -73,12 +73,12 @@ $helper->log('Define some styles for our Conditionals');
|
||||
$yellowStyle = new Style(false, true);
|
||||
$yellowStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_YELLOW);
|
||||
->getStartColor()->setARGB(Color::COLOR_YELLOW);
|
||||
$yellowStyle->getFont()->setColor(new Color(Color::COLOR_BLUE));
|
||||
$greenStyle = new Style(false, true);
|
||||
$greenStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_GREEN);
|
||||
->getStartColor()->setARGB(Color::COLOR_GREEN);
|
||||
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
|
||||
|
||||
$greenStyleMoney = clone $greenStyle;
|
||||
|
||||
@@ -7493,11 +7493,15 @@ class Xls extends BaseReader
|
||||
|
||||
// bit: 0-6; mask: 0x007F; type
|
||||
$color1 = (0x007F & $fillColors) >> 0;
|
||||
$style->getFill()->getStartColor()->setRGB(Xls\Color::map($color1, $this->palette, $this->version)['rgb']);
|
||||
|
||||
// bit: 7-13; mask: 0x3F80; type
|
||||
$color2 = (0x3F80 & $fillColors) >> 7;
|
||||
$style->getFill()->getEndColor()->setRGB(Xls\Color::map($color2, $this->palette, $this->version)['rgb']);
|
||||
if ($fillPattern === Fill::FILL_SOLID) {
|
||||
$style->getFill()->getStartColor()->setRGB(Xls\Color::map($color2, $this->palette, $this->version)['rgb']);
|
||||
} else {
|
||||
$style->getFill()->getStartColor()->setRGB(Xls\Color::map($color1, $this->palette, $this->version)['rgb']);
|
||||
$style->getFill()->getEndColor()->setRGB(Xls\Color::map($color2, $this->palette, $this->version)['rgb']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -119,7 +119,7 @@ class Xls extends BaseWriter
|
||||
// Initialise worksheet writers
|
||||
$countSheets = $this->spreadsheet->getSheetCount();
|
||||
for ($i = 0; $i < $countSheets; ++$i) {
|
||||
$this->writerWorksheets[$i] = new Worksheet($this->strTotal, $this->strUnique, $this->strTable, $this->colors, $this->parser, $this->preCalculateFormulas, $this->spreadsheet->getSheet($i));
|
||||
$this->writerWorksheets[$i] = new Worksheet($this->strTotal, $this->strUnique, $this->strTable, $this->colors, $this->parser, $this->preCalculateFormulas, $this->spreadsheet->getSheet($i), $this->writerWorkbook);
|
||||
}
|
||||
|
||||
// build Escher objects. Escher objects for workbooks needs to be build before Escher object for workbook.
|
||||
|
||||
@@ -4,6 +4,14 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Xls\Style;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Style\Color;
|
||||
|
||||
/*
|
||||
* Static array incorrectly used by Xls Writer for Conditional Styles.
|
||||
*
|
||||
* @deprecated since version 2.2
|
||||
*
|
||||
* @codecoverageignore
|
||||
*/
|
||||
|
||||
class ColorMap
|
||||
{
|
||||
/**
|
||||
@@ -70,7 +78,7 @@ class ColorMap
|
||||
|
||||
public static function lookup(Color $color, int $defaultIndex = 0x00): int
|
||||
{
|
||||
$colorRgb = $color->getRGB();
|
||||
$colorRgb = strtoupper($color->getRGB());
|
||||
if (is_string($colorRgb) && array_key_exists("#{$colorRgb}", self::$colorMap)) {
|
||||
return self::$colorMap["#{$colorRgb}"];
|
||||
}
|
||||
|
||||
@@ -272,7 +272,7 @@ class Workbook extends BIFFwriter
|
||||
*
|
||||
* @return int Color index
|
||||
*/
|
||||
private function addColor(string $rgb): int
|
||||
public function addColor(string $rgb, int $default = 0): int
|
||||
{
|
||||
if (!isset($this->colors[$rgb])) {
|
||||
$color
|
||||
@@ -298,7 +298,7 @@ class Workbook extends BIFFwriter
|
||||
$this->colors[$rgb] = $colorIndex;
|
||||
} else {
|
||||
// no room for more custom colors, just map to black
|
||||
$colorIndex = 0;
|
||||
$colorIndex = $default;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -154,6 +154,8 @@ class Worksheet extends BIFFwriter
|
||||
|
||||
private int $printHeaders;
|
||||
|
||||
private ?Workbook $writerWorkbook;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
@@ -165,7 +167,7 @@ class Worksheet extends BIFFwriter
|
||||
* @param bool $preCalculateFormulas Flag indicating whether formulas should be calculated or just written
|
||||
* @param \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $phpSheet The worksheet to write
|
||||
*/
|
||||
public function __construct(int &$str_total, int &$str_unique, array &$str_table, array &$colors, Parser $parser, bool $preCalculateFormulas, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $phpSheet)
|
||||
public function __construct(int &$str_total, int &$str_unique, array &$str_table, array &$colors, Parser $parser, bool $preCalculateFormulas, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $phpSheet, ?Workbook $writerWorkbook = null)
|
||||
{
|
||||
// It needs to call its parent's constructor explicitly
|
||||
parent::__construct();
|
||||
@@ -208,6 +210,7 @@ class Worksheet extends BIFFwriter
|
||||
if ($this->lastColumnIndex > 255) {
|
||||
$this->lastColumnIndex = 255;
|
||||
}
|
||||
$this->writerWorkbook = $writerWorkbook;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -491,8 +494,6 @@ class Worksheet extends BIFFwriter
|
||||
|
||||
$arrConditionalStyles = $this->phpSheet->getConditionalStylesCollection();
|
||||
if (!empty($arrConditionalStyles)) {
|
||||
$arrConditional = [];
|
||||
|
||||
// Write ConditionalFormattingTable records
|
||||
foreach ($arrConditionalStyles as $cellCoordinate => $conditionalStyles) {
|
||||
$cfHeaderWritten = false;
|
||||
@@ -506,10 +507,7 @@ class Worksheet extends BIFFwriter
|
||||
if ($cfHeaderWritten === false) {
|
||||
$cfHeaderWritten = $this->writeCFHeader($cellCoordinate, $conditionalStyles);
|
||||
}
|
||||
if ($cfHeaderWritten === true && !isset($arrConditional[$conditional->getHashCode()])) {
|
||||
// This hash code has been handled
|
||||
$arrConditional[$conditional->getHashCode()] = true;
|
||||
|
||||
if ($cfHeaderWritten === true) {
|
||||
// Write CFRULE record
|
||||
$this->writeCFRule($conditionalFormulaHelper, $conditional, $cellCoordinate);
|
||||
}
|
||||
@@ -2971,8 +2969,7 @@ class Worksheet extends BIFFwriter
|
||||
// Not used (3)
|
||||
$dataBlockFont .= pack('vC', 0x0000, 0x00);
|
||||
// Font color index
|
||||
$colorIdx = Style\ColorMap::lookup($conditional->getStyle()->getFont()->getColor(), 0x00);
|
||||
|
||||
$colorIdx = $this->workbookColorIndex($conditional->getStyle()->getFont()->getColor()->getRgb(), 0);
|
||||
$dataBlockFont .= pack('V', $colorIdx);
|
||||
// Not used (4)
|
||||
$dataBlockFont .= pack('V', 0x00000000);
|
||||
@@ -3043,9 +3040,9 @@ class Worksheet extends BIFFwriter
|
||||
// Fill Pattern Style
|
||||
$blockFillPatternStyle = Style\CellFill::style($conditional->getStyle()->getFill());
|
||||
// Background Color
|
||||
$colorIdxBg = Style\ColorMap::lookup($conditional->getStyle()->getFill()->getStartColor(), 0x41);
|
||||
$colorIdxBg = $this->workbookColorIndex($conditional->getStyle()->getFill()->getStartColor()->getRgb(), 0x41);
|
||||
// Foreground Color
|
||||
$colorIdxFg = Style\ColorMap::lookup($conditional->getStyle()->getFill()->getEndColor(), 0x40);
|
||||
$colorIdxFg = $this->workbookColorIndex($conditional->getStyle()->getFill()->getEndColor()->getRgb(), 0x40);
|
||||
|
||||
$dataBlockFill = pack('v', $blockFillPatternStyle);
|
||||
$dataBlockFill .= pack('v', $colorIdxFg | ($colorIdxBg << 7));
|
||||
@@ -3142,4 +3139,9 @@ class Worksheet extends BIFFwriter
|
||||
|
||||
return $dataBlockProtection;
|
||||
}
|
||||
|
||||
private function workbookColorIndex(?string $rgb, int $default): int
|
||||
{
|
||||
return (empty($rgb) || $this->writerWorkbook === null) ? $default : $this->writerWorkbook->addColor($rgb, $default);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,24 +54,24 @@ class XlfnFunctionsTest extends \PHPUnit\Framework\TestCase
|
||||
$condition0->setConditionType(Conditional::CONDITION_EXPRESSION);
|
||||
$condition0->addCondition('ABS(B3)<2');
|
||||
$condition0->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
|
||||
$condition0->getStyle()->getFill()->getEndColor()->setARGB(Color::COLOR_RED);
|
||||
$condition0->getStyle()->getFill()->getStartColor()->setARGB(Color::COLOR_RED);
|
||||
$condition1 = new Conditional();
|
||||
$condition1->setConditionType(Conditional::CONDITION_EXPRESSION);
|
||||
$condition1->addCondition('ABS(B3)>2');
|
||||
$condition1->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
|
||||
$condition1->getStyle()->getFill()->getEndColor()->setARGB(Color::COLOR_GREEN);
|
||||
$condition1->getStyle()->getFill()->getStartColor()->setARGB(Color::COLOR_GREEN);
|
||||
$cond = [$condition0, $condition1];
|
||||
$sheet->getStyle('B3:B5')->setConditionalStyles($cond);
|
||||
$condition0 = new Conditional();
|
||||
$condition0->setConditionType(Conditional::CONDITION_EXPRESSION);
|
||||
$condition0->addCondition('ISOWEEKNUM(A3)<10');
|
||||
$condition0->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
|
||||
$condition0->getStyle()->getFill()->getEndColor()->setARGB(Color::COLOR_RED);
|
||||
$condition0->getStyle()->getFill()->getStartColor()->setARGB(Color::COLOR_RED);
|
||||
$condition1 = new Conditional();
|
||||
$condition1->setConditionType(Conditional::CONDITION_EXPRESSION);
|
||||
$condition1->addCondition('ISOWEEKNUM(A3)>40');
|
||||
$condition1->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
|
||||
$condition1->getStyle()->getFill()->getEndColor()->setARGB(Color::COLOR_GREEN);
|
||||
$condition1->getStyle()->getFill()->getStartColor()->setARGB(Color::COLOR_GREEN);
|
||||
$cond = [$condition0, $condition1];
|
||||
$sheet->getStyle('A3:A5')->setConditionalStyles($cond);
|
||||
$sheet->setSelectedCell('B1');
|
||||
|
||||
@@ -195,15 +195,15 @@ class CellTest extends TestCase
|
||||
$yellowStyle = new Style(false, true);
|
||||
$yellowStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_YELLOW);
|
||||
->getStartColor()->setARGB(Color::COLOR_YELLOW);
|
||||
$greenStyle = new Style(false, true);
|
||||
$greenStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_GREEN);
|
||||
->getStartColor()->setARGB(Color::COLOR_GREEN);
|
||||
$redStyle = new Style(false, true);
|
||||
$redStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_RED);
|
||||
->getStartColor()->setARGB(Color::COLOR_RED);
|
||||
|
||||
$conditionalStyles = [];
|
||||
$wizardFactory = new Wizard($cellRange);
|
||||
@@ -228,22 +228,22 @@ class CellTest extends TestCase
|
||||
$style = $sheet->getCell('A1')->getAppliedStyle();
|
||||
self::assertTrue($style->getFont()->getBold());
|
||||
self::assertEquals($redStyle->getFill()->getFillType(), $style->getFill()->getFillType());
|
||||
self::assertEquals($redStyle->getFill()->getEndColor()->getARGB(), $style->getFill()->getEndColor()->getARGB());
|
||||
self::assertEquals($redStyle->getFill()->getStartColor()->getARGB(), $style->getFill()->getStartColor()->getARGB());
|
||||
|
||||
$style = $sheet->getCell('A2')->getAppliedStyle();
|
||||
self::assertTrue($style->getFont()->getBold());
|
||||
self::assertEquals($yellowStyle->getFill()->getFillType(), $style->getFill()->getFillType());
|
||||
self::assertEquals(
|
||||
$yellowStyle->getFill()->getEndColor()->getARGB(),
|
||||
$style->getFill()->getEndColor()->getARGB()
|
||||
$yellowStyle->getFill()->getStartColor()->getARGB(),
|
||||
$style->getFill()->getStartColor()->getARGB()
|
||||
);
|
||||
|
||||
$style = $sheet->getCell('A3')->getAppliedStyle();
|
||||
self::assertTrue($style->getFont()->getBold());
|
||||
self::assertEquals($greenStyle->getFill()->getFillType(), $style->getFill()->getFillType());
|
||||
self::assertEquals(
|
||||
$greenStyle->getFill()->getEndColor()->getARGB(),
|
||||
$style->getFill()->getEndColor()->getARGB()
|
||||
$greenStyle->getFill()->getStartColor()->getARGB(),
|
||||
$style->getFill()->getStartColor()->getARGB()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -266,11 +266,11 @@ class CellTest extends TestCase
|
||||
$yellowStyle = new Style(false, true);
|
||||
$yellowStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_YELLOW);
|
||||
->getStartColor()->setARGB(Color::COLOR_YELLOW);
|
||||
$redStyle = new Style(false, true);
|
||||
$redStyle->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(Color::COLOR_RED);
|
||||
->getStartColor()->setARGB(Color::COLOR_RED);
|
||||
|
||||
$conditionalCellRange = 'A1:C1';
|
||||
$conditionalStyles = [];
|
||||
@@ -294,7 +294,7 @@ class CellTest extends TestCase
|
||||
self::assertTrue($style->getFont()->getBold());
|
||||
self::assertEquals($fillStyle, $style->getFill()->getFillType());
|
||||
if ($fillStyle === Fill::FILL_SOLID) {
|
||||
self::assertEquals($fillColor, $style->getFill()->getEndColor()->getARGB());
|
||||
self::assertEquals($fillColor, $style->getFill()->getStartColor()->getARGB());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class ConditionalStopIfTrueTest extends AbstractFunctional
|
||||
$condition1->addCondition(0.6);
|
||||
$condition1->getStyle()->getFill()
|
||||
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(self::COLOR_RED);
|
||||
->getStartColor()->setARGB(self::COLOR_RED);
|
||||
|
||||
// if value above 0.6 -> green background
|
||||
$condition2 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
|
||||
@@ -47,7 +47,7 @@ class ConditionalStopIfTrueTest extends AbstractFunctional
|
||||
$condition2->addCondition(0.6);
|
||||
$condition2->getStyle()->getFill()
|
||||
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB(self::COLOR_GREEN);
|
||||
->getStartColor()->setARGB(self::COLOR_GREEN);
|
||||
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$spreadsheet->getActiveSheet()->getCell('A1')->setValue(0.7);
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xls;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class IsOddTest extends TestCase
|
||||
{
|
||||
public function testIsOdd(): void
|
||||
{
|
||||
// Excel Xls treats ISODD and ISEVEN as 'Add-in functions'
|
||||
// PhpSpreadsheet does not currently handle writing them.
|
||||
// It should, however, read them correctly.
|
||||
$filename = 'tests/data/Reader/XLS/isodd.xls';
|
||||
$reader = new Xls();
|
||||
$spreadsheet = $reader->load($filename);
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
self::assertSame('=ISODD(5)', $sheet->getCell('A1')->getValue());
|
||||
self::assertTrue($sheet->getCell('A1')->getCalculatedValue());
|
||||
self::assertSame('=ISEVEN(5)', $sheet->getCell('B1')->getValue());
|
||||
self::assertFalse($sheet->getCell('B1')->getCalculatedValue());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ class Xlsx2Test extends TestCase
|
||||
$cond1 = new Conditional();
|
||||
$cond1->setConditionType(Conditional::CONDITION_CONTAINSBLANKS);
|
||||
$cond1->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
|
||||
$cond1->getStyle()->getFill()->getEndColor()->setARGB(Color::COLOR_RED);
|
||||
$cond1->getStyle()->getFill()->getStartColor()->setARGB(Color::COLOR_RED);
|
||||
$cond = [$cond1];
|
||||
$sheet->getStyle('A1:A6')->setConditionalStyles($cond);
|
||||
$writer = IOFactory::createWriter($spreadshee1, 'Xlsx');
|
||||
|
||||
@@ -35,7 +35,7 @@ class ConditionalBoolTest extends TestCase
|
||||
$condition1->addCondition(false);
|
||||
$condition1->getStyle()->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB('FFFFFF00');
|
||||
->getStartColor()->setARGB('FFFFFF00');
|
||||
$conditionalStyles = $sheet->getStyle('A1:A10')->getConditionalStyles();
|
||||
$conditionalStyles[] = $condition1;
|
||||
$sheet->getStyle('A1:A20')->setConditionalStyles($conditionalStyles);
|
||||
@@ -53,7 +53,7 @@ class ConditionalBoolTest extends TestCase
|
||||
$condition1->addCondition(true);
|
||||
$condition1->getStyle()->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB('FF00FF00');
|
||||
->getStartColor()->setARGB('FF00FF00');
|
||||
$conditionalStyles = $sheet->getStyle('A1:A10')->getConditionalStyles();
|
||||
$conditionalStyles[] = $condition1;
|
||||
$sheet->getStyle('A1:A20')->setConditionalStyles($conditionalStyles);
|
||||
|
||||
@@ -18,7 +18,7 @@ class ConditionalTest extends TestCase
|
||||
$condition1->addCondition(0.6);
|
||||
$condition1->getStyle()->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB('FFFF0000');
|
||||
->getStartColor()->setARGB('FFFF0000');
|
||||
$conditionclone = clone $condition1;
|
||||
self::AssertEquals($condition1, $conditionclone);
|
||||
self::AssertEquals($condition1->getStyle(), $conditionclone->getStyle());
|
||||
@@ -33,21 +33,21 @@ class ConditionalTest extends TestCase
|
||||
$condition1->addCondition(0.6);
|
||||
$condition1->getStyle()->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB('FFFF0000');
|
||||
->getStartColor()->setARGB('FFFF0000');
|
||||
$condition2 = new Conditional();
|
||||
$condition2->setConditionType(Conditional::CONDITION_CELLIS);
|
||||
$condition2->setOperatorType(Conditional::OPERATOR_LESSTHAN);
|
||||
$condition2->setConditions(0.6);
|
||||
$condition2->getStyle()->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB('FFFF0000');
|
||||
->getStartColor()->setARGB('FFFF0000');
|
||||
$condition3 = new Conditional();
|
||||
$condition3->setConditionType(Conditional::CONDITION_CELLIS);
|
||||
$condition3->setOperatorType(Conditional::OPERATOR_LESSTHAN);
|
||||
$condition3->setConditions([0.6]);
|
||||
$condition3->getStyle()->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getEndColor()->setARGB('FFFF0000');
|
||||
->getStartColor()->setARGB('FFFF0000');
|
||||
self::AssertEquals($condition1, $condition2);
|
||||
self::AssertEquals($condition1, $condition3);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Conditional;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
||||
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
|
||||
|
||||
class ConditionalFontColorTest extends AbstractFunctional
|
||||
{
|
||||
public function testConditionalFontColor(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$sheet->getCell('A1')->setValue(10);
|
||||
$condition1 = new Conditional();
|
||||
$condition1->setConditionType(Conditional::CONDITION_CELLIS);
|
||||
$condition1->setOperatorType(Conditional::OPERATOR_LESSTHAN);
|
||||
$condition1->addCondition(20);
|
||||
$condition1->getStyle()->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getStartColor()->setARGB('FF2345FA');
|
||||
$condition1->getStyle()->getFont()
|
||||
->setBold(true);
|
||||
$condition1->getStyle()->getFont()->getColor()
|
||||
->setARGB('FFFF8193');
|
||||
$conditionalStyles[] = $condition1;
|
||||
$sheet->getStyle('A1')->setConditionalStyles($conditionalStyles);
|
||||
|
||||
$robj = $this->writeAndReload($spreadsheet, 'Xls');
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
$sheet0 = $robj->setActiveSheetIndex(0);
|
||||
$conditionals = $sheet0->getConditionalStylesCollection();
|
||||
self::assertCount(1, $conditionals);
|
||||
self::assertArrayHasKey('A1', $conditionals);
|
||||
$font = $conditionals['A1'][0]->getStyle()->getFont();
|
||||
self::assertSame('FFFF8193', $font->getColor()->getARGB());
|
||||
$fill = $conditionals['A1'][0]->getStyle()->getFill();
|
||||
self::assertSame('FF2345FA', $fill->getStartColor()->getARGB());
|
||||
self::assertSame(Fill::FILL_SOLID, $fill->getFillType());
|
||||
$robj->disconnectWorksheets();
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user