ConditionalStopIfTrueTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Functional;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. class ConditionalStopIfTrueTest extends AbstractFunctional
  5. {
  6. const COLOR_GREEN = 'FF99FF66';
  7. const COLOR_RED = 'FFFF5050';
  8. public function providerFormats(): array
  9. {
  10. return [
  11. ['Xlsx'],
  12. ];
  13. }
  14. /**
  15. * @dataProvider providerFormats
  16. *
  17. * @param string $format
  18. */
  19. public function testConditionalStopIfTrue($format): void
  20. {
  21. $pCoordinate = 'A1:A3';
  22. // if blank cell -> no styling
  23. $condition0 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
  24. $condition0->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_EXPRESSION);
  25. $condition0->addCondition('LEN(TRIM(A1))=0');
  26. $condition0->setStopIfTrue(true); // ! stop here
  27. // if value below 0.6 (matches also blank cells!) -> red background
  28. $condition1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
  29. $condition1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS);
  30. $condition1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_LESSTHAN);
  31. $condition1->addCondition(0.6);
  32. $condition1->getStyle()->getFill()
  33. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  34. ->getEndColor()->setARGB(self::COLOR_RED);
  35. // if value above 0.6 -> green background
  36. $condition2 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
  37. $condition2->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS);
  38. $condition2->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_GREATERTHAN);
  39. $condition2->addCondition(0.6);
  40. $condition2->getStyle()->getFill()
  41. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  42. ->getEndColor()->setARGB(self::COLOR_GREEN);
  43. $spreadsheet = new Spreadsheet();
  44. $spreadsheet->getActiveSheet()->getCell('A1')->setValue(0.7);
  45. $spreadsheet->getActiveSheet()->getCell('A2')->setValue('');
  46. $spreadsheet->getActiveSheet()->getCell('A3')->setValue(0.4);
  47. // put all three conditions in sheet
  48. $conditionalStyles = [];
  49. array_push($conditionalStyles, $condition0);
  50. array_push($conditionalStyles, $condition1);
  51. array_push($conditionalStyles, $condition2);
  52. $spreadsheet->getActiveSheet()->setConditionalStyles($pCoordinate, $conditionalStyles);
  53. $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
  54. // see if we successfully written "StopIfTrue"
  55. $newConditionalStyles = $reloadedSpreadsheet->getActiveSheet()->getConditionalStyles($pCoordinate);
  56. self::assertTrue($newConditionalStyles[0]->getStopIfTrue(), 'StopIfTrue should be set (=true) on first condition');
  57. self::assertFalse($newConditionalStyles[1]->getStopIfTrue(), 'StopIfTrue should not be set (=false) on second condition');
  58. self::assertFalse($newConditionalStyles[2]->getStopIfTrue(), 'StopIfTrue should not be set (=false) on third condition');
  59. }
  60. }