|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; |
| 6 | + |
| 7 | +use PhpOffice\PhpSpreadsheet\IOFactory; |
| 8 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 9 | +use PhpOffice\PhpSpreadsheet\Style\Conditional; |
| 10 | +use PhpOffice\PhpSpreadsheet\Style\Fill; |
| 11 | +use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; |
| 12 | + |
| 13 | +class ConditionalPriorityTest extends AbstractFunctional |
| 14 | +{ |
| 15 | + public function testConditionalPriority(): void |
| 16 | + { |
| 17 | + $filename = 'tests/data/Reader/XLSX/issue.4312c.xlsx'; |
| 18 | + $reader = IOFactory::createReader('Xlsx'); |
| 19 | + $spreadsheet = $reader->load($filename); |
| 20 | + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx'); |
| 21 | + $spreadsheet->disconnectWorksheets(); |
| 22 | + $worksheet = $reloadedSpreadsheet->getActiveSheet(); |
| 23 | + $priorities = []; |
| 24 | + foreach ($worksheet->getConditionalStylesCollection() as $conditionalStyles) { |
| 25 | + foreach ($conditionalStyles as $conditional) { |
| 26 | + $priorities[] = $conditional->getPriority(); |
| 27 | + } |
| 28 | + } |
| 29 | + $expected = [27, 2, 3, 4, 1, 22, 14, 5, 6, 7, 20]; |
| 30 | + self::assertSame($expected, $priorities); |
| 31 | + $reloadedSpreadsheet->disconnectWorksheets(); |
| 32 | + } |
| 33 | + |
| 34 | + public function testZeroPriority(): void |
| 35 | + { |
| 36 | + $spreadsheet = new Spreadsheet(); |
| 37 | + $sheet = $spreadsheet->getActiveSheet(); |
| 38 | + $sheet->fromArray([ |
| 39 | + [1, 1, 1, 1], |
| 40 | + [2, 2, 2, 2], |
| 41 | + [3, 3, 3, 3], |
| 42 | + [4, 4, 4, 4], |
| 43 | + [5, 5, 5, 5], |
| 44 | + ]); |
| 45 | + |
| 46 | + $range = 'A1:A5'; |
| 47 | + $styles = []; |
| 48 | + $new = new Conditional(); |
| 49 | + $new->setConditionType(Conditional::CONDITION_CELLIS) |
| 50 | + ->setOperatorType(Conditional::OPERATOR_EQUAL) |
| 51 | + ->setPriority(30) |
| 52 | + ->setConditions(['3']) |
| 53 | + ->getStyle() |
| 54 | + ->getFill() |
| 55 | + ->setFillType(Fill::FILL_SOLID) |
| 56 | + ->getStartColor() |
| 57 | + ->setArgb('FFC00000'); |
| 58 | + $styles[] = $new; |
| 59 | + $sheet->setConditionalStyles($range, $styles); |
| 60 | + |
| 61 | + $range = 'B1:B5'; |
| 62 | + $styles = []; |
| 63 | + $new = new Conditional(); |
| 64 | + $new->setConditionType(Conditional::CONDITION_EXPRESSION) |
| 65 | + ->setConditions('=MOD(A1,2)=0') |
| 66 | + ->getStyle() |
| 67 | + ->getFill() |
| 68 | + ->setFillType(Fill::FILL_SOLID) |
| 69 | + ->getStartColor() |
| 70 | + ->setArgb('FF00B0F0'); |
| 71 | + $styles[] = $new; |
| 72 | + $new = new Conditional(); |
| 73 | + $new->setConditionType(Conditional::CONDITION_CELLIS) |
| 74 | + ->setOperatorType(Conditional::OPERATOR_EQUAL) |
| 75 | + ->setPriority(40) |
| 76 | + ->setConditions(['4']) |
| 77 | + ->getStyle() |
| 78 | + ->getFill() |
| 79 | + ->setFillType(Fill::FILL_SOLID) |
| 80 | + ->getStartColor() |
| 81 | + ->setArgb('FFFFC000'); |
| 82 | + $styles[] = $new; |
| 83 | + $sheet->setConditionalStyles($range, $styles); |
| 84 | + |
| 85 | + $range = 'C1:C5'; |
| 86 | + $styles = []; |
| 87 | + $new = new Conditional(); |
| 88 | + $new->setConditionType(Conditional::CONDITION_CELLIS) |
| 89 | + ->setOperatorType(Conditional::OPERATOR_EQUAL) |
| 90 | + ->setPriority(20) |
| 91 | + ->setConditions(['2']) |
| 92 | + ->getStyle() |
| 93 | + ->getFill() |
| 94 | + ->setFillType(Fill::FILL_SOLID) |
| 95 | + ->getStartColor() |
| 96 | + ->setArgb('FFFFFF00'); |
| 97 | + $styles[] = $new; |
| 98 | + $new = new Conditional(); |
| 99 | + $new->setConditionType(Conditional::CONDITION_CELLIS) |
| 100 | + ->setOperatorType(Conditional::OPERATOR_EQUAL) |
| 101 | + ->setConditions(['5']) |
| 102 | + ->getStyle() |
| 103 | + ->getFill() |
| 104 | + ->setFillType(Fill::FILL_SOLID) |
| 105 | + ->getStartColor() |
| 106 | + ->setArgb('FF008080'); |
| 107 | + $styles[] = $new; |
| 108 | + $sheet->setConditionalStyles($range, $styles); |
| 109 | + |
| 110 | + $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx'); |
| 111 | + $spreadsheet->disconnectWorksheets(); |
| 112 | + $worksheet = $reloadedSpreadsheet->getActiveSheet(); |
| 113 | + $priorities = []; |
| 114 | + foreach ($worksheet->getConditionalStylesCollection() as $conditionalStyles) { |
| 115 | + foreach ($conditionalStyles as $conditional) { |
| 116 | + $priorities[] = $conditional->getPriority(); |
| 117 | + } |
| 118 | + } |
| 119 | + // B1:B5 is written in order 41, 40, but Reader sorts them |
| 120 | + $expected = [30, 40, 41, 20, 42]; |
| 121 | + self::assertSame($expected, $priorities); |
| 122 | + $styles = $worksheet->getConditionalStyles('B1:B5'); |
| 123 | + self::assertSame(Conditional::CONDITION_CELLIS, $styles[0]->getConditionType()); |
| 124 | + self::assertSame(40, $styles[0]->getPriority()); |
| 125 | + self::assertSame(Conditional::CONDITION_EXPRESSION, $styles[1]->getConditionType()); |
| 126 | + self::assertSame(41, $styles[1]->getPriority()); |
| 127 | + $reloadedSpreadsheet->disconnectWorksheets(); |
| 128 | + } |
| 129 | +} |
0 commit comments