DGET.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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>DGET</h1>
  13. <h2>Extracts a single value from a column of a list or database that matches conditions that you specify.</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 height of the Apple tree between 10\' and 16\' tall');
  38. $worksheet->setCellValue('B12', '=DGET(A4:E10,"Height",A1:F2)');
  39. echo '<hr />';
  40. echo '<h4>Database</h4>';
  41. $databaseData = $worksheet->rangeToArray('A4:E10',null,true,true,true);
  42. var_dump($databaseData);
  43. echo '<hr />';
  44. // Test the formulae
  45. echo '<h4>Criteria</h4>';
  46. echo 'ALL' . '<br /><br />';
  47. echo $worksheet->getCell("A12")->getValue() .'<br />';
  48. echo 'DMAX() Result is ' . $worksheet->getCell("B12")->getCalculatedValue() .'<br /><br />';
  49. echo '<h4>Criteria</h4>';
  50. $criteriaData = $worksheet->rangeToArray('A1:A2',null,true,true,true);
  51. var_dump($criteriaData);
  52. echo $worksheet->getCell("A13")->getValue() .'<br />';
  53. echo 'DMAX() Result is ' . $worksheet->getCell("B13")->getCalculatedValue();
  54. ?>
  55. <body>
  56. </html>