01_Basic_Comparisons.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. use PhpOffice\PhpSpreadsheet\Style\Color;
  5. use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
  6. use PhpOffice\PhpSpreadsheet\Style\Fill;
  7. use PhpOffice\PhpSpreadsheet\Style\Style;
  8. require __DIR__ . '/../Header.php';
  9. // Create new Spreadsheet object
  10. $helper->log('Create new Spreadsheet object');
  11. $spreadsheet = new Spreadsheet();
  12. // Set document properties
  13. $helper->log('Set document properties');
  14. $spreadsheet->getProperties()->setCreator('Mark Baker')
  15. ->setLastModifiedBy('Mark Baker')
  16. ->setTitle('PhpSpreadsheet Test Document')
  17. ->setSubject('PhpSpreadsheet Test Document')
  18. ->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.')
  19. ->setKeywords('office PhpSpreadsheet php')
  20. ->setCategory('Test result file');
  21. // Create the worksheet
  22. $helper->log('Add data');
  23. $spreadsheet->setActiveSheetIndex(0);
  24. $spreadsheet->getActiveSheet()
  25. ->setCellValue('A1', 'Literal Value Comparison')
  26. ->setCellValue('A9', 'Value Comparison with Absolute Cell Reference $H$9')
  27. ->setCellValue('A17', 'Value Comparison with Relative Cell References')
  28. ->setCellValue('A23', 'Value Comparison with Formula based on AVERAGE() ± STDEV()')
  29. ->setCellValue('A30', 'Literal String Value Comparison');
  30. $dataArray = [
  31. [-2, -1, 0, 1, 2],
  32. [-1, 0, 1, 2, 3],
  33. [0, 1, 2, 3, 4],
  34. [1, 2, 3, 4, 5],
  35. ];
  36. $betweenDataArray = [
  37. [2, 7, 6],
  38. [9, 5, 1],
  39. [4, 3, 8],
  40. ];
  41. $stringArray = [
  42. ['I'],
  43. ['Love'],
  44. ['PHP'],
  45. ];
  46. $spreadsheet->getActiveSheet()
  47. ->fromArray($dataArray, null, 'A2', true)
  48. ->fromArray($dataArray, null, 'A10', true)
  49. ->fromArray($betweenDataArray, null, 'A18', true)
  50. ->fromArray($dataArray, null, 'A24', true)
  51. ->fromArray($stringArray, null, 'A31', true)
  52. ->setCellValue('H9', 1);
  53. // Set title row bold
  54. $helper->log('Set title row bold');
  55. $spreadsheet->getActiveSheet()->getStyle('A1:E1')->getFont()->setBold(true);
  56. $spreadsheet->getActiveSheet()->getStyle('A9:E9')->getFont()->setBold(true);
  57. $spreadsheet->getActiveSheet()->getStyle('A17:E17')->getFont()->setBold(true);
  58. $spreadsheet->getActiveSheet()->getStyle('A23:E23')->getFont()->setBold(true);
  59. $spreadsheet->getActiveSheet()->getStyle('A30:E30')->getFont()->setBold(true);
  60. // Define some styles for our Conditionals
  61. $helper->log('Define some styles for our Conditionals');
  62. $yellowStyle = new Style(false, true);
  63. $yellowStyle->getFill()
  64. ->setFillType(Fill::FILL_SOLID)
  65. ->getStartColor()->setARGB(Color::COLOR_YELLOW);
  66. $yellowStyle->getFill()
  67. ->getEndColor()->setARGB(Color::COLOR_YELLOW);
  68. $yellowStyle->getFont()->setColor(new Color(Color::COLOR_BLUE));
  69. $greenStyle = new Style(false, true);
  70. $greenStyle->getFill()
  71. ->setFillType(Fill::FILL_SOLID)
  72. ->getStartColor()->setARGB(Color::COLOR_GREEN);
  73. $greenStyle->getFill()
  74. ->getEndColor()->setARGB(Color::COLOR_GREEN);
  75. $greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
  76. $redStyle = new Style(false, true);
  77. $redStyle->getFill()
  78. ->setFillType(Fill::FILL_SOLID)
  79. ->getStartColor()->setARGB(Color::COLOR_RED);
  80. $redStyle->getFill()
  81. ->getEndColor()->setARGB(Color::COLOR_RED);
  82. $redStyle->getFont()->setColor(new Color(Color::COLOR_GREEN));
  83. // Set conditional formatting rules and styles
  84. $helper->log('Define conditional formatting and set styles');
  85. // Set rules for Literal Value Comparison
  86. $cellRange = 'A2:E5';
  87. $conditionalStyles = [];
  88. $wizardFactory = new Wizard($cellRange);
  89. /** @var Wizard\CellValue $cellWizard */
  90. $cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE);
  91. $cellWizard->equals(0)
  92. ->setStyle($yellowStyle);
  93. $conditionalStyles[] = $cellWizard->getConditional();
  94. $cellWizard->greaterThan(0)
  95. ->setStyle($greenStyle);
  96. $conditionalStyles[] = $cellWizard->getConditional();
  97. $cellWizard->lessThan(0)
  98. ->setStyle($redStyle);
  99. $conditionalStyles[] = $cellWizard->getConditional();
  100. $spreadsheet->getActiveSheet()
  101. ->getStyle($cellWizard->getCellRange())
  102. ->setConditionalStyles($conditionalStyles);
  103. // Set rules for Value Comparison with Absolute Cell Reference $H$9
  104. $cellRange = 'A10:E13';
  105. $conditionalStyles = [];
  106. $wizardFactory = new Wizard($cellRange);
  107. /** @var Wizard\CellValue $cellWizard */
  108. $cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE);
  109. $cellWizard->equals('$H$9', Wizard::VALUE_TYPE_CELL)
  110. ->setStyle($yellowStyle);
  111. $conditionalStyles[] = $cellWizard->getConditional();
  112. $cellWizard->greaterThan('$H$9', Wizard::VALUE_TYPE_CELL)
  113. ->setStyle($greenStyle);
  114. $conditionalStyles[] = $cellWizard->getConditional();
  115. $cellWizard->lessThan('$H$9', Wizard::VALUE_TYPE_CELL)
  116. ->setStyle($redStyle);
  117. $conditionalStyles[] = $cellWizard->getConditional();
  118. $spreadsheet->getActiveSheet()
  119. ->getStyle($cellWizard->getCellRange())
  120. ->setConditionalStyles($conditionalStyles);
  121. // Set rules for Value Comparison with Relative Cell References
  122. $cellRange = 'A18:A20';
  123. $conditionalStyles = [];
  124. $wizardFactory = new Wizard($cellRange);
  125. /** @var Wizard\CellValue $cellWizard */
  126. $cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE);
  127. $cellWizard->between('$B1', Wizard::VALUE_TYPE_CELL)
  128. ->and('$C1', Wizard::VALUE_TYPE_CELL)
  129. ->setStyle($greenStyle);
  130. $conditionalStyles[] = $cellWizard->getConditional();
  131. $spreadsheet->getActiveSheet()
  132. ->getStyle($cellWizard->getCellRange())
  133. ->setConditionalStyles($conditionalStyles);
  134. // Set rules for Value Comparison with Formula
  135. $cellRange = 'A24:E27';
  136. $formulaRange = implode(
  137. ':',
  138. array_map(
  139. [Coordinate::class, 'absoluteCoordinate'],
  140. Coordinate::splitRange($cellRange)[0]
  141. )
  142. );
  143. $conditionalStyles = [];
  144. $wizardFactory = new Wizard($cellRange);
  145. /** @var Wizard\CellValue $cellWizard */
  146. $cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE);
  147. $cellWizard->between('AVERAGE(' . $formulaRange . ')-STDEV(' . $formulaRange . ')', Wizard::VALUE_TYPE_FORMULA)
  148. ->and('AVERAGE(' . $formulaRange . ')+STDEV(' . $formulaRange . ')', Wizard::VALUE_TYPE_FORMULA)
  149. ->setStyle($yellowStyle);
  150. $conditionalStyles[] = $cellWizard->getConditional();
  151. $cellWizard->greaterThan('AVERAGE(' . $formulaRange . ')+STDEV(' . $formulaRange . ')', Wizard::VALUE_TYPE_FORMULA)
  152. ->setStyle($greenStyle);
  153. $conditionalStyles[] = $cellWizard->getConditional();
  154. $cellWizard->lessThan('AVERAGE(' . $formulaRange . ')-STDEV(' . $formulaRange . ')', Wizard::VALUE_TYPE_FORMULA)
  155. ->setStyle($redStyle);
  156. $conditionalStyles[] = $cellWizard->getConditional();
  157. $spreadsheet->getActiveSheet()
  158. ->getStyle($cellWizard->getCellRange())
  159. ->setConditionalStyles($conditionalStyles);
  160. // Set rules for Value Comparison with String Literal
  161. $cellRange = 'A31:A33';
  162. $formulaRange = implode(
  163. ':',
  164. array_map(
  165. [Coordinate::class, 'absoluteCoordinate'],
  166. Coordinate::splitRange($cellRange)[0]
  167. )
  168. );
  169. $conditionalStyles = [];
  170. $wizardFactory = new Wizard($cellRange);
  171. /** @var Wizard\CellValue $cellWizard */
  172. $cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE);
  173. $cellWizard->equals('LOVE')
  174. ->setStyle($redStyle);
  175. $conditionalStyles[] = $cellWizard->getConditional();
  176. $cellWizard->equals('PHP')
  177. ->setStyle($greenStyle);
  178. $conditionalStyles[] = $cellWizard->getConditional();
  179. $spreadsheet->getActiveSheet()
  180. ->getStyle($cellWizard->getCellRange())
  181. ->setConditionalStyles($conditionalStyles);
  182. // Save
  183. $helper->write($spreadsheet, __FILE__);