mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-22 16:19:19 +00:00
a3489b5d89
No source changes. Act on some items that have come up in recent discussions. - Sample 33_Chart_create_line creates a stacked line chart. According to @MarkBaker, the stacking is done on the wrong variable, and, even were that not the case, stacking is unusual for line charts. Since this is our primary sample showing how to create a line chart, remove the stacking. Another sample, with a more appropriate choice of chart (33_Chart_create_bar_stacked), still shows how to create a stacked chart. - The test for reading a styled cell from Html is flawed. It sets a date format for a string date/time, but the format is applied only to numeric data, so the format, although set correctly, is ineffective. Keep the test, but add some explanation in the assertion, and add some new more effective tests, also with explanations in the assertions. - Wrong namespace used for Writer/Xlsx/ConditionalFillTest.
91 lines
3.7 KiB
PHP
91 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\File;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional;
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ConditionalFillTest extends TestCase
|
|
{
|
|
public function testFill(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->getCell('A1')->setValue(10);
|
|
$sheet->getCell('A2')->setValue(20);
|
|
$sheet->getCell('A3')->setValue(30);
|
|
$sheet->getCell('A4')->setValue(40);
|
|
|
|
$sheet->getStyle('A1')->getFill()->setFillType(Fill::FILL_SOLID);
|
|
$sheet->getStyle('A1')->getFill()->getStartColor()->setARGB('FFFF0000');
|
|
$sheet->getStyle('A2')->getFill()->setFillType(Fill::FILL_SOLID);
|
|
// Need to specify StartColor for desired effect
|
|
$sheet->getStyle('A2')->getFill()->getEndColor()->setARGB('FF00FF00');
|
|
|
|
$conditional1 = new Conditional();
|
|
$conditional1->setConditionType(Conditional::CONDITION_CELLIS);
|
|
$conditional1->setOperatorType(Conditional::OPERATOR_GREATERTHAN);
|
|
$conditional1->addCondition(35);
|
|
$conditional1->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
|
|
$conditional1->getStyle()->getFill()->getEndColor()->setARGB('FF0000FF');
|
|
|
|
$conditional2 = new Conditional();
|
|
$conditional2->setConditionType(Conditional::CONDITION_CELLIS);
|
|
$conditional2->setOperatorType(Conditional::OPERATOR_LESSTHAN);
|
|
$conditional2->addCondition(35);
|
|
$conditional2->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
|
|
// Before issue37303202, had needed to specify EndColor for desired effect.
|
|
// This was the opposite of non-Conditional style.
|
|
$conditional2->getStyle()->getFill()->getStartColor()->setARGB('FFFFFF00');
|
|
|
|
$conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('A3:A4')->getConditionalStyles();
|
|
$conditionalStyles[] = $conditional1;
|
|
$conditionalStyles[] = $conditional2;
|
|
|
|
$spreadsheet->getActiveSheet()->getStyle('A3:A4')->setConditionalStyles($conditionalStyles);
|
|
$sheet->setSelectedCells('C1');
|
|
|
|
$outfile = File::temporaryFilename();
|
|
$writer = new XlsxWriter($spreadsheet);
|
|
$writer->save($outfile);
|
|
$spreadsheet->disconnectWorksheets();
|
|
|
|
$file = 'zip://';
|
|
$file .= $outfile;
|
|
$file .= '#xl/styles.xml';
|
|
$data = file_get_contents($file);
|
|
unlink($outfile);
|
|
|
|
$expected = '<fill>'
|
|
. '<patternFill patternType="solid">'
|
|
. '<fgColor rgb="FFFF0000"/>'
|
|
. '<bgColor rgb="FF000000"/>'
|
|
. '</patternFill>'
|
|
. '</fill>';
|
|
self::assertStringContainsString($expected, $data, 'style for A1');
|
|
$expected = '<fill>'
|
|
. '<patternFill patternType="solid">'
|
|
. '<fgColor rgb="FFFFFFFF"/>'
|
|
. '<bgColor rgb="FF00FF00"/>'
|
|
. '</patternFill>'
|
|
. '</fill>';
|
|
self::assertStringContainsString($expected, $data, 'style for A2');
|
|
$expected = '<dxf><fill>'
|
|
. '<patternFill patternType="solid">'
|
|
. '<bgColor rgb="FF0000FF"/>'
|
|
. '</patternFill>'
|
|
. '</fill>';
|
|
self::assertStringContainsString($expected, $data, 'conditional 1');
|
|
$expected = '<dxf><fill>'
|
|
. '<patternFill patternType="solid">'
|
|
. '<bgColor rgb="FFFFFF00"/>'
|
|
. '</patternFill>'
|
|
. '</fill>';
|
|
self::assertStringContainsString($expected, $data, 'conditional 2');
|
|
}
|
|
}
|