13_Calculation.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. mt_srand(1234567890);
  5. require __DIR__ . '/../Header.php';
  6. // List functions
  7. $helper->log('List implemented functions');
  8. $calc = Calculation::getInstance();
  9. print_r($calc->getImplementedFunctionNames());
  10. // Create new Spreadsheet object
  11. $helper->log('Create new Spreadsheet object');
  12. $spreadsheet = new Spreadsheet();
  13. // Add some data, we will use some formulas here
  14. $helper->log('Add some data and formulas');
  15. $spreadsheet->getActiveSheet()->setCellValue('A14', 'Count:')
  16. ->setCellValue('A15', 'Sum:')
  17. ->setCellValue('A16', 'Max:')
  18. ->setCellValue('A17', 'Min:')
  19. ->setCellValue('A18', 'Average:')
  20. ->setCellValue('A19', 'Median:')
  21. ->setCellValue('A20', 'Mode:');
  22. $spreadsheet->getActiveSheet()->setCellValue('A22', 'CountA:')
  23. ->setCellValue('A23', 'MaxA:')
  24. ->setCellValue('A24', 'MinA:');
  25. $spreadsheet->getActiveSheet()->setCellValue('A26', 'StDev:')
  26. ->setCellValue('A27', 'StDevA:')
  27. ->setCellValue('A28', 'StDevP:')
  28. ->setCellValue('A29', 'StDevPA:');
  29. $spreadsheet->getActiveSheet()->setCellValue('A31', 'DevSq:')
  30. ->setCellValue('A32', 'Var:')
  31. ->setCellValue('A33', 'VarA:')
  32. ->setCellValue('A34', 'VarP:')
  33. ->setCellValue('A35', 'VarPA:');
  34. $spreadsheet->getActiveSheet()->setCellValue('A37', 'Date:');
  35. $spreadsheet->getActiveSheet()->setCellValue('B1', 'Range 1')
  36. ->setCellValue('B2', 2)
  37. ->setCellValue('B3', 8)
  38. ->setCellValue('B4', 10)
  39. ->setCellValue('B5', true)
  40. ->setCellValue('B6', false)
  41. ->setCellValue('B7', 'Text String')
  42. ->setCellValue('B9', '22')
  43. ->setCellValue('B10', 4)
  44. ->setCellValue('B11', 6)
  45. ->setCellValue('B12', 12);
  46. $spreadsheet->getActiveSheet()->setCellValue('B14', '=COUNT(B2:B12)')
  47. ->setCellValue('B15', '=SUM(B2:B12)')
  48. ->setCellValue('B16', '=MAX(B2:B12)')
  49. ->setCellValue('B17', '=MIN(B2:B12)')
  50. ->setCellValue('B18', '=AVERAGE(B2:B12)')
  51. ->setCellValue('B19', '=MEDIAN(B2:B12)')
  52. ->setCellValue('B20', '=MODE(B2:B12)');
  53. $spreadsheet->getActiveSheet()->setCellValue('B22', '=COUNTA(B2:B12)')
  54. ->setCellValue('B23', '=MAXA(B2:B12)')
  55. ->setCellValue('B24', '=MINA(B2:B12)');
  56. $spreadsheet->getActiveSheet()->setCellValue('B26', '=STDEV(B2:B12)')
  57. ->setCellValue('B27', '=STDEVA(B2:B12)')
  58. ->setCellValue('B28', '=STDEVP(B2:B12)')
  59. ->setCellValue('B29', '=STDEVPA(B2:B12)');
  60. $spreadsheet->getActiveSheet()->setCellValue('B31', '=DEVSQ(B2:B12)')
  61. ->setCellValue('B32', '=VAR(B2:B12)')
  62. ->setCellValue('B33', '=VARA(B2:B12)')
  63. ->setCellValue('B34', '=VARP(B2:B12)')
  64. ->setCellValue('B35', '=VARPA(B2:B12)');
  65. $spreadsheet->getActiveSheet()->setCellValue('B37', '=DATE(2007, 12, 21)')
  66. ->setCellValue('B38', '=DATEDIF( DATE(2007, 12, 21), DATE(2007, 12, 22), "D" )')
  67. ->setCellValue('B39', '=DATEVALUE("01-Feb-2006 10:06 AM")')
  68. ->setCellValue('B40', '=DAY( DATE(2006, 1, 2) )')
  69. ->setCellValue('B41', '=DAYS360( DATE(2002, 2, 3), DATE(2005, 5, 31) )');
  70. $spreadsheet->getActiveSheet()->setCellValue('C1', 'Range 2')
  71. ->setCellValue('C2', 1)
  72. ->setCellValue('C3', 2)
  73. ->setCellValue('C4', 2)
  74. ->setCellValue('C5', 3)
  75. ->setCellValue('C6', 3)
  76. ->setCellValue('C7', 3)
  77. ->setCellValue('C8', '0')
  78. ->setCellValue('C9', 4)
  79. ->setCellValue('C10', 4)
  80. ->setCellValue('C11', 4)
  81. ->setCellValue('C12', 4);
  82. $spreadsheet->getActiveSheet()->setCellValue('C14', '=COUNT(C2:C12)')
  83. ->setCellValue('C15', '=SUM(C2:C12)')
  84. ->setCellValue('C16', '=MAX(C2:C12)')
  85. ->setCellValue('C17', '=MIN(C2:C12)')
  86. ->setCellValue('C18', '=AVERAGE(C2:C12)')
  87. ->setCellValue('C19', '=MEDIAN(C2:C12)')
  88. ->setCellValue('C20', '=MODE(C2:C12)');
  89. $spreadsheet->getActiveSheet()->setCellValue('C22', '=COUNTA(C2:C12)')
  90. ->setCellValue('C23', '=MAXA(C2:C12)')
  91. ->setCellValue('C24', '=MINA(C2:C12)');
  92. $spreadsheet->getActiveSheet()->setCellValue('C26', '=STDEV(C2:C12)')
  93. ->setCellValue('C27', '=STDEVA(C2:C12)')
  94. ->setCellValue('C28', '=STDEVP(C2:C12)')
  95. ->setCellValue('C29', '=STDEVPA(C2:C12)');
  96. $spreadsheet->getActiveSheet()->setCellValue('C31', '=DEVSQ(C2:C12)')
  97. ->setCellValue('C32', '=VAR(C2:C12)')
  98. ->setCellValue('C33', '=VARA(C2:C12)')
  99. ->setCellValue('C34', '=VARP(C2:C12)')
  100. ->setCellValue('C35', '=VARPA(C2:C12)');
  101. $spreadsheet->getActiveSheet()->setCellValue('D1', 'Range 3')
  102. ->setCellValue('D2', 2)
  103. ->setCellValue('D3', 3)
  104. ->setCellValue('D4', 4);
  105. $spreadsheet->getActiveSheet()->setCellValue('D14', '=((D2 * D3) + D4) & " should be 10"');
  106. $spreadsheet->getActiveSheet()->setCellValue('E12', 'Other functions')
  107. ->setCellValue('E14', '=PI()')
  108. ->setCellValue('E15', '=RAND()')
  109. ->setCellValue('E16', '=RANDBETWEEN(5, 10)');
  110. $spreadsheet->getActiveSheet()->setCellValue('E17', 'Count of both ranges:')
  111. ->setCellValue('F17', '=COUNT(B2:C12)');
  112. $spreadsheet->getActiveSheet()->setCellValue('E18', 'Total of both ranges:')
  113. ->setCellValue('F18', '=SUM(B2:C12)');
  114. $spreadsheet->getActiveSheet()->setCellValue('E19', 'Maximum of both ranges:')
  115. ->setCellValue('F19', '=MAX(B2:C12)');
  116. $spreadsheet->getActiveSheet()->setCellValue('E20', 'Minimum of both ranges:')
  117. ->setCellValue('F20', '=MIN(B2:C12)');
  118. $spreadsheet->getActiveSheet()->setCellValue('E21', 'Average of both ranges:')
  119. ->setCellValue('F21', '=AVERAGE(B2:C12)');
  120. $spreadsheet->getActiveSheet()->setCellValue('E22', 'Median of both ranges:')
  121. ->setCellValue('F22', '=MEDIAN(B2:C12)');
  122. $spreadsheet->getActiveSheet()->setCellValue('E23', 'Mode of both ranges:')
  123. ->setCellValue('F23', '=MODE(B2:C12)');
  124. // Calculated data
  125. $helper->log('Calculated data');
  126. for ($col = 'B'; $col != 'G'; ++$col) {
  127. for ($row = 14; $row <= 41; ++$row) {
  128. if ((($formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue()) !== null) &&
  129. ($formula[0] == '=')) {
  130. $helper->log('Value of ' . $col . $row . ' [' . $formula . ']: ' . $spreadsheet->getActiveSheet()->getCell($col . $row)->getCalculatedValue());
  131. }
  132. }
  133. }
  134. //
  135. // If we set Pre Calculated Formulas to true then PhpSpreadsheet will calculate all formulae in the
  136. // workbook before saving. This adds time and memory overhead, and can cause some problems with formulae
  137. // using functions or features (such as array formulae) that aren't yet supported by the calculation engine
  138. // If the value is false (the default) for the Xlsx Writer, then MS Excel (or the application used to
  139. // open the file) will need to recalculate values itself to guarantee that the correct results are available.
  140. //
  141. //$writer->setPreCalculateFormulas(true);
  142. // Save
  143. $helper->write($spreadsheet, __FILE__);