02_Text_Comparisons.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. use PhpOffice\PhpSpreadsheet\Style\Color;
  4. use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
  5. use PhpOffice\PhpSpreadsheet\Style\Fill;
  6. use PhpOffice\PhpSpreadsheet\Style\Style;
  7. require __DIR__ . '/../Header.php';
  8. // Create new Spreadsheet object
  9. $helper->log('Create new Spreadsheet object');
  10. $spreadsheet = new Spreadsheet();
  11. // Set document properties
  12. $helper->log('Set document properties');
  13. $spreadsheet->getProperties()->setCreator('Mark Baker')
  14. ->setLastModifiedBy('Mark Baker')
  15. ->setTitle('PhpSpreadsheet Test Document')
  16. ->setSubject('PhpSpreadsheet Test Document')
  17. ->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.')
  18. ->setKeywords('office PhpSpreadsheet php')
  19. ->setCategory('Test result file');
  20. // Create the worksheet
  21. $helper->log('Add data');
  22. $spreadsheet->setActiveSheetIndex(0);
  23. $spreadsheet->getActiveSheet()
  24. ->setCellValue('A1', 'Value Begins With Literal')
  25. ->setCellValue('A7', 'Value Ends With Literal')
  26. ->setCellValue('A13', 'Value Contains Literal')
  27. ->setCellValue('A19', "Value Doesn't Contain Literal")
  28. ->setCellValue('E1', 'Value Begins With using Cell Reference')
  29. ->setCellValue('E7', 'Value Ends With using Cell Reference')
  30. ->setCellValue('E13', 'Value Contains using Cell Reference')
  31. ->setCellValue('E19', "Value Doesn't Contain using Cell Reference")
  32. ->setCellValue('A25', 'Simple Comparison using Concatenation Formula');
  33. $dataArray = [
  34. ['HELLO', 'WORLD'],
  35. ['MELLOW', 'YELLOW'],
  36. ['SLEEPY', 'HOLLOW'],
  37. ];
  38. $spreadsheet->getActiveSheet()
  39. ->fromArray($dataArray, null, 'A2', true)
  40. ->fromArray($dataArray, null, 'A8', true)
  41. ->fromArray($dataArray, null, 'A14', true)
  42. ->fromArray($dataArray, null, 'A20', true)
  43. ->fromArray($dataArray, null, 'E2', true)
  44. ->fromArray($dataArray, null, 'E8', true)
  45. ->fromArray($dataArray, null, 'E14', true)
  46. ->fromArray($dataArray, null, 'E20', true)
  47. ->fromArray($dataArray, null, 'A26', true)
  48. ->setCellValue('D1', 'H')
  49. ->setCellValue('D7', 'OW')
  50. ->setCellValue('D13', 'LL')
  51. ->setCellValue('D19', 'EL')
  52. ->setCellValue('C26', 'HELLO WORLD')
  53. ->setCellValue('C27', 'SOYLENT GREEN')
  54. ->setCellValue('C28', 'SLEEPY HOLLOW');
  55. // Set title row bold
  56. $helper->log('Set title row bold');
  57. $spreadsheet->getActiveSheet()->getStyle('A1:G1')->getFont()->setBold(true);
  58. $spreadsheet->getActiveSheet()->getStyle('A7:G7')->getFont()->setBold(true);
  59. $spreadsheet->getActiveSheet()->getStyle('A13:G13')->getFont()->setBold(true);
  60. $spreadsheet->getActiveSheet()->getStyle('A19:G19')->getFont()->setBold(true);
  61. $spreadsheet->getActiveSheet()->getStyle('A25:C25')->getFont()->setBold(true);
  62. // Define some styles for our Conditionals
  63. $helper->log('Define some styles for our Conditionals');
  64. $yellowStyle = new Style(false, true);
  65. $yellowStyle->getFill()
  66. ->setFillType(Fill::FILL_SOLID)
  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. ->getEndColor()->setARGB(Color::COLOR_GREEN);
  73. $greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
  74. $redStyle = new Style(false, true);
  75. $redStyle->getFill()
  76. ->setFillType(Fill::FILL_SOLID)
  77. ->getEndColor()->setARGB(Color::COLOR_RED);
  78. $redStyle->getFont()->setColor(new Color(Color::COLOR_GREEN));
  79. // Set conditional formatting rules and styles
  80. $helper->log('Define conditional formatting and set styles');
  81. // Set rules for Literal Value Begins With
  82. $cellRange = 'A2:B4';
  83. $conditionalStyles = [];
  84. $wizardFactory = new Wizard($cellRange);
  85. /** @var Wizard\TextValue $textWizard */
  86. $textWizard = $wizardFactory->newRule(Wizard::TEXT_VALUE);
  87. $textWizard->beginsWith('H')
  88. ->setStyle($yellowStyle);
  89. $conditionalStyles[] = $textWizard->getConditional();
  90. $spreadsheet->getActiveSheet()
  91. ->getStyle($textWizard->getCellRange())
  92. ->setConditionalStyles($conditionalStyles);
  93. // Set rules for Value Begins With using Cell Reference
  94. $cellRange = 'E2:F4';
  95. $conditionalStyles = [];
  96. $wizardFactory = new Wizard($cellRange);
  97. /** @var Wizard\TextValue $textWizard */
  98. $textWizard = $wizardFactory->newRule(Wizard::TEXT_VALUE);
  99. $textWizard->beginsWith('$D$1', Wizard::VALUE_TYPE_CELL)
  100. ->setStyle($yellowStyle);
  101. $conditionalStyles[] = $textWizard->getConditional();
  102. $spreadsheet->getActiveSheet()
  103. ->getStyle($textWizard->getCellRange())
  104. ->setConditionalStyles($conditionalStyles);
  105. // Set rules for Literal Value Ends With
  106. $cellRange = 'A8:B10';
  107. $conditionalStyles = [];
  108. $wizardFactory = new Wizard($cellRange);
  109. /** @var Wizard\TextValue $textWizard */
  110. $textWizard = $wizardFactory->newRule(Wizard::TEXT_VALUE);
  111. $textWizard->endsWith('OW')
  112. ->setStyle($yellowStyle);
  113. $conditionalStyles[] = $textWizard->getConditional();
  114. $spreadsheet->getActiveSheet()
  115. ->getStyle($textWizard->getCellRange())
  116. ->setConditionalStyles($conditionalStyles);
  117. // Set rules for Value Ends With using Cell Reference
  118. $cellRange = 'E8:F10';
  119. $conditionalStyles = [];
  120. $wizardFactory = new Wizard($cellRange);
  121. /** @var Wizard\TextValue $textWizard */
  122. $textWizard = $wizardFactory->newRule(Wizard::TEXT_VALUE);
  123. $textWizard->endsWith('$D$7', Wizard::VALUE_TYPE_CELL)
  124. ->setStyle($yellowStyle);
  125. $conditionalStyles[] = $textWizard->getConditional();
  126. $spreadsheet->getActiveSheet()
  127. ->getStyle($textWizard->getCellRange())
  128. ->setConditionalStyles($conditionalStyles);
  129. // Set rules for Literal Value Contains
  130. $cellRange = 'A14:B16';
  131. $conditionalStyles = [];
  132. $wizardFactory = new Wizard($cellRange);
  133. /** @var Wizard\TextValue $textWizard */
  134. $textWizard = $wizardFactory->newRule(Wizard::TEXT_VALUE);
  135. $textWizard->contains('LL')
  136. ->setStyle($greenStyle);
  137. $conditionalStyles[] = $textWizard->getConditional();
  138. $spreadsheet->getActiveSheet()
  139. ->getStyle($textWizard->getCellRange())
  140. ->setConditionalStyles($conditionalStyles);
  141. // Set rules for Value Contains using Cell Reference
  142. $cellRange = 'E14:F16';
  143. $conditionalStyles = [];
  144. $wizardFactory = new Wizard($cellRange);
  145. /** @var Wizard\TextValue $textWizard */
  146. $textWizard = $wizardFactory->newRule(Wizard::TEXT_VALUE);
  147. $textWizard->contains('$D$13', Wizard::VALUE_TYPE_CELL)
  148. ->setStyle($greenStyle);
  149. $conditionalStyles[] = $textWizard->getConditional();
  150. $spreadsheet->getActiveSheet()
  151. ->getStyle($textWizard->getCellRange())
  152. ->setConditionalStyles($conditionalStyles);
  153. // Set rules for Literal Value Does Not Contain
  154. $cellRange = 'A20:B22';
  155. $conditionalStyles = [];
  156. $wizardFactory = new Wizard($cellRange);
  157. /** @var Wizard\TextValue $textWizard */
  158. $textWizard = $wizardFactory->newRule(Wizard::TEXT_VALUE);
  159. $textWizard->doesNotContain('EL')
  160. ->setStyle($redStyle);
  161. $conditionalStyles[] = $textWizard->getConditional();
  162. $spreadsheet->getActiveSheet()
  163. ->getStyle($textWizard->getCellRange())
  164. ->setConditionalStyles($conditionalStyles);
  165. // Set rules for Value Contains using Cell Reference
  166. $cellRange = 'E20:F22';
  167. $conditionalStyles = [];
  168. $wizardFactory = new Wizard($cellRange);
  169. /** @var Wizard\TextValue $textWizard */
  170. $textWizard = $wizardFactory->newRule(Wizard::TEXT_VALUE);
  171. $textWizard->doesNotContain('$D$19', Wizard::VALUE_TYPE_CELL)
  172. ->setStyle($redStyle);
  173. $conditionalStyles[] = $textWizard->getConditional();
  174. $spreadsheet->getActiveSheet()
  175. ->getStyle($textWizard->getCellRange())
  176. ->setConditionalStyles($conditionalStyles);
  177. // Set rules for Simple Comparison using Concatenation Formula
  178. $cellRange = 'C26:C28';
  179. $conditionalStyles = [];
  180. $wizardFactory = new Wizard($cellRange);
  181. /** @var Wizard\CellValue $cellWizard */
  182. $cellWizard = $wizardFactory->newRule(Wizard::CELL_VALUE);
  183. $cellWizard->equals('CONCATENATE($A1," ",$B1)', Wizard::VALUE_TYPE_FORMULA)
  184. ->setStyle($yellowStyle);
  185. $conditionalStyles[] = $cellWizard->getConditional();
  186. $spreadsheet->getActiveSheet()
  187. ->getStyle($cellWizard->getCellRange())
  188. ->setConditionalStyles($conditionalStyles);
  189. $spreadsheet->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
  190. // Save
  191. $helper->write($spreadsheet, __FILE__);