DAVERAGE.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. error_reporting(E_ALL);
  3. set_time_limit(0);
  4. date_default_timezone_set('Europe/London');
  5. ?>
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  9. <title>PHPExcel Calculation Examples</title>
  10. </head>
  11. <body>
  12. <h1>DAVERAGE</h1>
  13. <h2>Returns the average of selected database entries.</h2>
  14. <?php
  15. /** Include path **/
  16. set_include_path(get_include_path() . PATH_SEPARATOR . '../../../../Classes/');
  17. /** Include PHPExcel */
  18. include 'PHPExcel.php';
  19. // Create new PHPExcel object
  20. $objPHPExcel = new PHPExcel();
  21. $worksheet = $objPHPExcel->getActiveSheet();
  22. // Add some data
  23. $database = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit' ),
  24. array( 'Apple', 18, 20, 14, 105.00 ),
  25. array( 'Pear', 12, 12, 10, 96.00 ),
  26. array( 'Cherry', 13, 14, 9, 105.00 ),
  27. array( 'Apple', 14, 15, 10, 75.00 ),
  28. array( 'Pear', 9, 8, 8, 76.80 ),
  29. array( 'Apple', 8, 9, 6, 45.00 ),
  30. );
  31. $criteria = array( array( 'Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height' ),
  32. array( '="=Apple"', '>10', NULL, NULL, NULL, '<16' ),
  33. array( '="=Pear"', NULL, NULL, NULL, NULL, NULL )
  34. );
  35. $worksheet->fromArray( $criteria, NULL, 'A1' );
  36. $worksheet->fromArray( $database, NULL, 'A4' );
  37. $worksheet->setCellValue('A12', 'The Average yield of Apple trees over 10\' in height');
  38. $worksheet->setCellValue('B12', '=DAVERAGE(A4:E10,"Yield",A1:B2)');
  39. $worksheet->setCellValue('A13', 'The Average age of all Apple and Pear trees in the orchard');
  40. $worksheet->setCellValue('B13', '=DAVERAGE(A4:E10,3,A1:A3)');
  41. echo '<hr />';
  42. echo '<h4>Database</h4>';
  43. $databaseData = $worksheet->rangeToArray('A4:E10',null,true,true,true);
  44. var_dump($databaseData);
  45. echo '<hr />';
  46. // Test the formulae
  47. echo '<h4>Criteria</h4>';
  48. $criteriaData = $worksheet->rangeToArray('A1:B2',null,true,true,true);
  49. var_dump($criteriaData);
  50. echo $worksheet->getCell("A12")->getValue() .'<br />';
  51. echo 'DAVERAGE() Result is ' . $worksheet->getCell("B12")->getCalculatedValue() .'<br /><br />';
  52. echo '<h4>Criteria</h4>';
  53. $criteriaData = $worksheet->rangeToArray('A1:A3',null,true,true,true);
  54. var_dump($criteriaData);
  55. echo $worksheet->getCell("A13")->getValue() .'<br />';
  56. echo 'DAVERAGE() Result is ' . $worksheet->getCell("B13")->getCalculatedValue();
  57. ?>
  58. <body>
  59. </html>