DCOUNTA.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. require __DIR__ . '/../../Header.php';
  4. $category = 'Database';
  5. $functionName = 'DCOUNTA';
  6. $description = 'Counts the cells in a set of database records that match criteria';
  7. $helper->titles($category, $functionName, $description);
  8. // Create new PhpSpreadsheet object
  9. $spreadsheet = new Spreadsheet();
  10. $worksheet = $spreadsheet->getActiveSheet();
  11. // Add some data
  12. $database = [['Tree', 'Height', 'Age', 'Yield', 'Profit'],
  13. ['Apple', 18, 20, 14, 105.00],
  14. ['Pear', 12, 12, 10, 96.00],
  15. ['Cherry', 13, 14, 9, 105.00],
  16. ['Apple', 14, 'N/A', 10, 75.00],
  17. ['Pear', 9, 8, 8, 77.00],
  18. ['Apple', 12, 11, 6, 45.00],
  19. ];
  20. $criteria = [['Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height'],
  21. ['="=Apple"', '>10', null, null, null, '<16'],
  22. ['="=Pear"', null, null, null, null, null],
  23. ];
  24. $worksheet->fromArray($criteria, null, 'A1');
  25. $worksheet->fromArray($database, null, 'A4');
  26. $worksheet->setCellValue('A12', 'The Number of Apple trees between 10\' and 16\' in height');
  27. $worksheet->setCellValue('B12', '=DCOUNTA(A4:E10,"Age",A1:F2)');
  28. $worksheet->setCellValue('A13', 'The Number of Apple and Pear trees in the orchard');
  29. $worksheet->setCellValue('B13', '=DCOUNTA(A4:E10,3,A1:A3)');
  30. $helper->log('Database');
  31. $databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
  32. $helper->displayGrid($databaseData);
  33. // Test the formulae
  34. $helper->log('Criteria');
  35. $criteriaData = $worksheet->rangeToArray('A1:F2', null, true, true, true);
  36. $helper->displayGrid($criteriaData);
  37. $helper->logCalculationResult($worksheet, $functionName, 'B12', 'A12');
  38. $helper->log('Criteria');
  39. $criteriaData = $worksheet->rangeToArray('A1:A3', null, true, true, true);
  40. $helper->displayGrid($criteriaData);
  41. $helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');