18_Extendedcalculation.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. require __DIR__ . '/../Header.php';
  5. // List functions
  6. $helper->log('List implemented functions');
  7. $calc = Calculation::getInstance();
  8. print_r($calc->getImplementedFunctionNames());
  9. // Create new Spreadsheet object
  10. $helper->log('Create new Spreadsheet object');
  11. $spreadsheet = new Spreadsheet();
  12. // Add some data, we will use some formulas here
  13. $helper->log('Add some data');
  14. $spreadsheet->getActiveSheet()->setCellValue('A14', 'Count:');
  15. $spreadsheet->getActiveSheet()->setCellValue('B1', 'Range 1');
  16. $spreadsheet->getActiveSheet()->setCellValue('B2', 2);
  17. $spreadsheet->getActiveSheet()->setCellValue('B3', 8);
  18. $spreadsheet->getActiveSheet()->setCellValue('B4', 10);
  19. $spreadsheet->getActiveSheet()->setCellValue('B5', true);
  20. $spreadsheet->getActiveSheet()->setCellValue('B6', false);
  21. $spreadsheet->getActiveSheet()->setCellValue('B7', 'Text String');
  22. $spreadsheet->getActiveSheet()->setCellValue('B9', '22');
  23. $spreadsheet->getActiveSheet()->setCellValue('B10', 4);
  24. $spreadsheet->getActiveSheet()->setCellValue('B11', 6);
  25. $spreadsheet->getActiveSheet()->setCellValue('B12', 12);
  26. $spreadsheet->getActiveSheet()->setCellValue('B14', '=COUNT(B2:B12)');
  27. $spreadsheet->getActiveSheet()->setCellValue('C1', 'Range 2');
  28. $spreadsheet->getActiveSheet()->setCellValue('C2', 1);
  29. $spreadsheet->getActiveSheet()->setCellValue('C3', 2);
  30. $spreadsheet->getActiveSheet()->setCellValue('C4', 2);
  31. $spreadsheet->getActiveSheet()->setCellValue('C5', 3);
  32. $spreadsheet->getActiveSheet()->setCellValue('C6', 3);
  33. $spreadsheet->getActiveSheet()->setCellValue('C7', 3);
  34. $spreadsheet->getActiveSheet()->setCellValue('C8', '0');
  35. $spreadsheet->getActiveSheet()->setCellValue('C9', 4);
  36. $spreadsheet->getActiveSheet()->setCellValue('C10', 4);
  37. $spreadsheet->getActiveSheet()->setCellValue('C11', 4);
  38. $spreadsheet->getActiveSheet()->setCellValue('C12', 4);
  39. $spreadsheet->getActiveSheet()->setCellValue('C14', '=COUNT(C2:C12)');
  40. $spreadsheet->getActiveSheet()->setCellValue('D1', 'Range 3');
  41. $spreadsheet->getActiveSheet()->setCellValue('D2', 2);
  42. $spreadsheet->getActiveSheet()->setCellValue('D3', 3);
  43. $spreadsheet->getActiveSheet()->setCellValue('D4', 4);
  44. $spreadsheet->getActiveSheet()->setCellValue('D5', '=((D2 * D3) + D4) & " should be 10"');
  45. $spreadsheet->getActiveSheet()->setCellValue('E1', 'Other functions');
  46. $spreadsheet->getActiveSheet()->setCellValue('E2', '=PI()');
  47. $spreadsheet->getActiveSheet()->setCellValue('E3', '=RAND()');
  48. $spreadsheet->getActiveSheet()->setCellValue('E4', '=RANDBETWEEN(5, 10)');
  49. $spreadsheet->getActiveSheet()->setCellValue('E14', 'Count of both ranges:');
  50. $spreadsheet->getActiveSheet()->setCellValue('F14', '=COUNT(B2:C12)');
  51. // Calculated data
  52. $helper->log('Calculated data');
  53. $helper->log('Value of B14 [=COUNT(B2:B12)]: ' . $spreadsheet->getActiveSheet()->getCell('B14')->getCalculatedValue());
  54. $helper->logEndingNotes();