03_Formulas.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. require __DIR__ . '/../Header.php';
  4. // Create new Spreadsheet object
  5. $helper->log('Create new Spreadsheet object');
  6. $spreadsheet = new Spreadsheet();
  7. // Set document properties
  8. $helper->log('Set document properties');
  9. $spreadsheet->getProperties()->setCreator('Maarten Balliauw')
  10. ->setLastModifiedBy('Maarten Balliauw')
  11. ->setTitle('Office 2007 XLSX Test Document')
  12. ->setSubject('Office 2007 XLSX Test Document')
  13. ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
  14. ->setKeywords('office 2007 openxml php')
  15. ->setCategory('Test result file');
  16. // Add some data, we will use some formulas here
  17. $helper->log('Add some data');
  18. $spreadsheet->getActiveSheet()
  19. ->setCellValue('A5', 'Sum:');
  20. $spreadsheet->getActiveSheet()->setCellValue('B1', 'Range #1')
  21. ->setCellValue('B2', 3)
  22. ->setCellValue('B3', 7)
  23. ->setCellValue('B4', 13)
  24. ->setCellValue('B5', '=SUM(B2:B4)');
  25. $helper->log('Sum of Range #1 is ' . $spreadsheet->getActiveSheet()->getCell('B5')->getCalculatedValue());
  26. $spreadsheet->getActiveSheet()->setCellValue('C1', 'Range #2')
  27. ->setCellValue('C2', 5)
  28. ->setCellValue('C3', 11)
  29. ->setCellValue('C4', 17)
  30. ->setCellValue('C5', '=SUM(C2:C4)');
  31. $helper->log('Sum of Range #2 is ' . $spreadsheet->getActiveSheet()->getCell('C5')->getCalculatedValue());
  32. $spreadsheet->getActiveSheet()
  33. ->setCellValue('A7', 'Total of both ranges:');
  34. $spreadsheet->getActiveSheet()
  35. ->setCellValue('B7', '=SUM(B5:C5)');
  36. $helper->log('Sum of both Ranges is ' . $spreadsheet->getActiveSheet()->getCell('B7')->getCalculatedValue());
  37. $spreadsheet->getActiveSheet()
  38. ->setCellValue('A8', 'Minimum of both ranges:');
  39. $spreadsheet->getActiveSheet()
  40. ->setCellValue('B8', '=MIN(B2:C4)');
  41. $helper->log('Minimum value in either Range is ' . $spreadsheet->getActiveSheet()->getCell('B8')->getCalculatedValue());
  42. $spreadsheet->getActiveSheet()
  43. ->setCellValue('A9', 'Maximum of both ranges:');
  44. $spreadsheet->getActiveSheet()
  45. ->setCellValue('B9', '=MAX(B2:C4)');
  46. $helper->log('Maximum value in either Range is ' . $spreadsheet->getActiveSheet()->getCell('B9')->getCalculatedValue());
  47. $spreadsheet->getActiveSheet()
  48. ->setCellValue('A10', 'Average of both ranges:');
  49. $spreadsheet->getActiveSheet()
  50. ->setCellValue('B10', '=AVERAGE(B2:C4)');
  51. $helper->log('Average value of both Ranges is ' . $spreadsheet->getActiveSheet()->getCell('B10')->getCalculatedValue());
  52. $spreadsheet->getActiveSheet()
  53. ->getColumnDimension('A')
  54. ->setAutoSize(true);
  55. // Rename worksheet
  56. $helper->log('Rename worksheet');
  57. $spreadsheet->getActiveSheet()
  58. ->setTitle('Formulas');
  59. //
  60. // If we set Pre Calculated Formulas to true then PhpSpreadsheet will calculate all formulae in the
  61. // workbook before saving. This adds time and memory overhead, and can cause some problems with formulae
  62. // using functions or features (such as array formulae) that aren't yet supported by the calculation engine
  63. // If the value is false (the default) for the Xlsx Writer, then MS Excel (or the application used to
  64. // open the file) will need to recalculate values itself to guarantee that the correct results are available.
  65. //
  66. //$writer->setPreCalculateFormulas(true);
  67. // Save
  68. $helper->write($spreadsheet, __FILE__);