-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle Booleans in Conditional Styles (#2654)
You can set up a conditional style to, say, apply to cells equal to boolean values. For such conditions, the Excel XML specifies `TRUE` or `FALSE`. It is noteworthy that false matches empty cells as well as FALSE, but not 0; similarly TRUE does not match 1. The Xlsx Writer just casts these values to string, which will not work properly. The Xlsx Reader treats the values as strings, so it won't work properly either. This PR corrects both. Also the doc blocks in Style/Conditional allow bool in some places, but not in others; these are corrected but no executable code is changed there.
- Loading branch information
Showing
6 changed files
with
121 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Style; | ||
|
||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader; | ||
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 ConditionalBoolTest extends TestCase | ||
{ | ||
/** @var string */ | ||
private $outfile = ''; | ||
|
||
protected function tearDown(): void | ||
{ | ||
if ($this->outfile !== '') { | ||
unlink($this->outfile); | ||
$this->outfile = ''; | ||
} | ||
} | ||
|
||
public function testBool(): void | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
|
||
$sheet = $spreadsheet->getActiveSheet(); | ||
$condition1 = new Conditional(); | ||
$condition1->setConditionType(Conditional::CONDITION_CELLIS); | ||
$condition1->setOperatorType(Conditional::OPERATOR_EQUAL); | ||
$condition1->addCondition(false); | ||
$condition1->getStyle()->getFill() | ||
->setFillType(Fill::FILL_SOLID) | ||
->getEndColor()->setARGB('FFFFFF00'); | ||
$conditionalStyles = $sheet->getStyle('A1:A10')->getConditionalStyles(); | ||
$conditionalStyles[] = $condition1; | ||
$sheet->getStyle('A1:A20')->setConditionalStyles($conditionalStyles); | ||
$sheet->setCellValue('A1', 1); | ||
$sheet->setCellValue('A2', true); | ||
$sheet->setCellValue('A3', false); | ||
$sheet->setCellValue('A4', 0.6); | ||
$sheet->setCellValue('A6', 0); | ||
$sheet->setSelectedCell('B1'); | ||
|
||
$sheet = $spreadsheet->createSheet(); | ||
$condition1 = new Conditional(); | ||
$condition1->setConditionType(Conditional::CONDITION_CELLIS); | ||
$condition1->setOperatorType(Conditional::OPERATOR_EQUAL); | ||
$condition1->addCondition(true); | ||
$condition1->getStyle()->getFill() | ||
->setFillType(Fill::FILL_SOLID) | ||
->getEndColor()->setARGB('FF00FF00'); | ||
$conditionalStyles = $sheet->getStyle('A1:A10')->getConditionalStyles(); | ||
$conditionalStyles[] = $condition1; | ||
$sheet->getStyle('A1:A20')->setConditionalStyles($conditionalStyles); | ||
$sheet->setCellValue('A1', 1); | ||
$sheet->setCellValue('A2', true); | ||
$sheet->setCellValue('A3', false); | ||
$sheet->setCellValue('A4', 0.6); | ||
$sheet->setCellValue('A6', 0); | ||
$sheet->setSelectedCell('B1'); | ||
|
||
$writer = new XlsxWriter($spreadsheet); | ||
$this->outfile = File::temporaryFilename(); | ||
$writer->save($this->outfile); | ||
$spreadsheet->disconnectWorksheets(); | ||
|
||
$file = 'zip://' . $this->outfile . '#xl/worksheets/sheet1.xml'; | ||
$contents = file_get_contents($file); | ||
self::assertNotFalse($contents); | ||
self::assertStringContainsString('<formula>FALSE</formula>', $contents); | ||
$file = 'zip://' . $this->outfile . '#xl/worksheets/sheet2.xml'; | ||
$contents = file_get_contents($file); | ||
self::assertNotFalse($contents); | ||
self::assertStringContainsString('<formula>TRUE</formula>', $contents); | ||
|
||
$reader = new XlsxReader(); | ||
$spreadsheet2 = $reader->load($this->outfile); | ||
$sheet1 = $spreadsheet2->getSheet(0); | ||
$condArray = $sheet1->getStyle('A1:A20')->getConditionalStyles(); | ||
self::assertNotEmpty($condArray); | ||
$cond1 = $condArray[0]; | ||
self::assertSame(Conditional::CONDITION_CELLIS, $cond1->getConditionType()); | ||
self::assertSame(Conditional::OPERATOR_EQUAL, $cond1->getOperatorType()); | ||
self::assertFalse(($cond1->getConditions())[0]); | ||
$sheet2 = $spreadsheet2->getSheet(1); | ||
$condArray = $sheet2->getStyle('A1:A20')->getConditionalStyles(); | ||
self::assertNotEmpty($condArray); | ||
$cond1 = $condArray[0]; | ||
self::assertSame(Conditional::CONDITION_CELLIS, $cond1->getConditionType()); | ||
self::assertSame(Conditional::OPERATOR_EQUAL, $cond1->getOperatorType()); | ||
self::assertTrue(($cond1->getConditions())[0]); | ||
$spreadsheet2->disconnectWorksheets(); | ||
} | ||
} |