mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-24 18:18:25 +00:00
4a04499bff
* Allow single-cell checks on conditional styles, even when the style is configured for a range of cells * Work on the CellMatcher logic to evaluate Conditionals for a cell based on its value, and identify which conditional styles should be applied * Refactor style merging and cell matching for conditional formatting into separate classes; this should make it easier to test, and easier to extend for other CF expressions subsequently * Added support for containsErrors and notContainsErrors * Initial work on a wizard to help simplify created Conditional Formatting rules, to ensure that the correct expressions are set * Further work on extending the Conditional Formatting rules to cover more of the options that are available in MS Excel * Prevent phpcs-fixer from removing class @method annotations, used to identify the signature for magic methods used in Wizard classes * Implement `fromConditional()`` method to allow the creation of a CF Wizard from an existing Conditional * Ensure that xlsx Reader picks up the timePeriod attribute for DatesOccurring CF Rules * Allow Duplicates/Uniques CF Rules to be recognised in the Xlsx Reader * Basic Xlsx reading of CF Rules/Styles from <extLst><ext><ConditinalFormattings> element, and not just the <ConditinalFormatting> element of the worksheet * Add some validation for operands passed to the CF Wizards - remove any leading ``=` from formulae, because they'll be embedded into other formulae - unwrap any string literals from quotes, because that's also handled internally Handle cross-worksheet cell references in cellReferences and Formulae/Expressions * re-baseline phpstan * Update Change Log with details of the CF Improvements
86 lines
2.5 KiB
PHP
86 lines
2.5 KiB
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Color;
|
|
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
use PhpOffice\PhpSpreadsheet\Style\Style;
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
|
|
// Create new Spreadsheet object
|
|
$helper->log('Create new Spreadsheet object');
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
// Set document properties
|
|
$helper->log('Set document properties');
|
|
$spreadsheet->getProperties()->setCreator('Mark Baker')
|
|
->setLastModifiedBy('Mark Baker')
|
|
->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');
|
|
$spreadsheet->setActiveSheetIndex(0);
|
|
$spreadsheet->getActiveSheet()
|
|
->setCellValue('A1', 'Duplicates Comparison');
|
|
|
|
$dataArray = [
|
|
[1, 0, 3],
|
|
[2, 1, 1],
|
|
[3, 1, 4],
|
|
[4, 2, 1],
|
|
[5, 3, 5],
|
|
[6, 5, 9],
|
|
[7, 8, 2],
|
|
[8, 13, 6],
|
|
[9, 21, 5],
|
|
[10, 34, 3],
|
|
[11, 55, 5],
|
|
];
|
|
|
|
$spreadsheet->getActiveSheet()
|
|
->fromArray($dataArray, null, 'A2', true);
|
|
|
|
// Set title row bold
|
|
$helper->log('Set title row bold');
|
|
$spreadsheet->getActiveSheet()->getStyle('A1:C1')->getFont()->setBold(true);
|
|
|
|
// Define some styles for our Conditionals
|
|
$helper->log('Define some styles for our Conditionals');
|
|
$greenStyle = new Style(false, true);
|
|
$greenStyle->getFill()
|
|
->setFillType(Fill::FILL_SOLID)
|
|
->getEndColor()->setARGB(Color::COLOR_GREEN);
|
|
$yellowStyle = new Style(false, true);
|
|
$yellowStyle->getFill()
|
|
->setFillType(Fill::FILL_SOLID)
|
|
->getEndColor()->setARGB(Color::COLOR_YELLOW);
|
|
|
|
// Set conditional formatting rules and styles
|
|
$helper->log('Define conditional formatting and set styles');
|
|
|
|
// Set rules for Duplicates Comparison
|
|
$cellRange = 'A2:C12';
|
|
$conditionalStyles = [];
|
|
$wizardFactory = new Wizard($cellRange);
|
|
/** @var Wizard\Duplicates $duplicatesWizard */
|
|
$duplicatesWizard = $wizardFactory->newRule(Wizard::DUPLICATES);
|
|
|
|
$duplicatesWizard->setStyle($yellowStyle);
|
|
$conditionalStyles[] = $duplicatesWizard->getConditional();
|
|
|
|
$duplicatesWizard->unique()
|
|
->setStyle($greenStyle);
|
|
$conditionalStyles[] = $duplicatesWizard->getConditional();
|
|
|
|
$spreadsheet->getActiveSheet()
|
|
->getStyle($duplicatesWizard->getCellRange())
|
|
->setConditionalStyles($conditionalStyles);
|
|
|
|
// Save
|
|
$helper->write($spreadsheet, __FILE__);
|