Skip to content

Commit 420a63b

Browse files
authored
Merge pull request PHPOffice#3047 from PHPOffice/Calculation-Examples
Update Excel function samples for Database and Date/Time functions
2 parents adbda63 + 350f38c commit 420a63b

34 files changed

+1200
-107
lines changed

samples/Calculations/Database/DAVERAGE.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
require __DIR__ . '/../../Header.php';
66

7-
$helper->log('Returns the average of selected database entries.');
7+
$category = 'Database';
8+
$functionName = 'DAVERAGE';
9+
$description = 'Returns the average of selected database entries that match criteria';
10+
11+
$helper->titles($category, $functionName, $description);
812

913
// Create new PhpSpreadsheet object
1014
$spreadsheet = new Spreadsheet();
@@ -36,21 +40,19 @@
3640
$helper->log('Database');
3741

3842
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
39-
var_dump($databaseData);
43+
$helper->displayGrid($databaseData);
4044

4145
// Test the formulae
4246
$helper->log('Criteria');
4347

4448
$criteriaData = $worksheet->rangeToArray('A1:B2', null, true, true, true);
45-
var_dump($criteriaData);
49+
$helper->displayGrid($criteriaData);
4650

47-
$helper->log($worksheet->getCell('A12')->getValue());
48-
$helper->log('DAVERAGE() Result is ' . $worksheet->getCell('B12')->getCalculatedValue());
51+
$helper->logCalculationResult($worksheet, $functionName, 'B12', 'A12');
4952

5053
$helper->log('Criteria');
5154

5255
$criteriaData = $worksheet->rangeToArray('A1:A3', null, true, true, true);
53-
var_dump($criteriaData);
56+
$helper->displayGrid($criteriaData);
5457

55-
$helper->log($worksheet->getCell('A13')->getValue());
56-
$helper->log('DAVERAGE() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
58+
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');

samples/Calculations/Database/DCOUNT.php

+18-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
use PhpOffice\PhpSpreadsheet\Spreadsheet;
44

55
require __DIR__ . '/../../Header.php';
6-
$helper->log('Counts the cells that contain numbers in a database.');
6+
7+
$category = 'Database';
8+
$functionName = 'DCOUNT';
9+
$description = 'Counts the cells that contain numbers in a set of database records that match criteria';
10+
11+
$helper->titles($category, $functionName, $description);
712

813
// Create new PhpSpreadsheet object
914
$spreadsheet = new Spreadsheet();
@@ -14,9 +19,9 @@
1419
['Apple', 18, 20, 14, 105.00],
1520
['Pear', 12, 12, 10, 96.00],
1621
['Cherry', 13, 14, 9, 105.00],
17-
['Apple', 14, 15, 10, 75.00],
18-
['Pear', 9, 8, 8, 76.80],
19-
['Apple', 8, 9, 6, 45.00],
22+
['Apple', 14, 'N/A', 10, 75.00],
23+
['Pear', 9, 8, 8, 77.00],
24+
['Apple', 12, 11, 6, 45.00],
2025
];
2126
$criteria = [['Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height'],
2227
['="=Apple"', '>10', null, null, null, '<16'],
@@ -26,30 +31,28 @@
2631
$worksheet->fromArray($criteria, null, 'A1');
2732
$worksheet->fromArray($database, null, 'A4');
2833

29-
$worksheet->setCellValue('A12', 'The Number of Apple trees over 10\' in height');
30-
$worksheet->setCellValue('B12', '=DCOUNT(A4:E10,"Yield",A1:B2)');
34+
$worksheet->setCellValue('A12', 'The Number of Apple trees between 10\' and 16\' in height whose age is known');
35+
$worksheet->setCellValue('B12', '=DCOUNT(A4:E10,"Age",A1:F2)');
3136

32-
$worksheet->setCellValue('A13', 'The Number of Apple and Pear trees in the orchard');
37+
$worksheet->setCellValue('A13', 'The Number of Apple and Pear trees in the orchard with a numeric value in column 3 ("Age")');
3338
$worksheet->setCellValue('B13', '=DCOUNT(A4:E10,3,A1:A3)');
3439

3540
$helper->log('Database');
3641

3742
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
38-
var_dump($databaseData);
43+
$helper->displayGrid($databaseData);
3944

4045
// Test the formulae
4146
$helper->log('Criteria');
4247

43-
$criteriaData = $worksheet->rangeToArray('A1:B2', null, true, true, true);
44-
var_dump($criteriaData);
48+
$criteriaData = $worksheet->rangeToArray('A1:F2', null, true, true, true);
49+
$helper->displayGrid($criteriaData);
4550

46-
$helper->log($worksheet->getCell('A12')->getValue());
47-
$helper->log('DCOUNT() Result is ' . $worksheet->getCell('B12')->getCalculatedValue());
51+
$helper->logCalculationResult($worksheet, $functionName, 'B12', 'A12');
4852

4953
$helper->log('Criteria');
5054

5155
$criteriaData = $worksheet->rangeToArray('A1:A3', null, true, true, true);
52-
var_dump($criteriaData);
56+
$helper->displayGrid($criteriaData);
5357

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

samples/Calculations/Database/DGET.php

+15-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
require __DIR__ . '/../../Header.php';
66

7-
$helper->log('Extracts a single value from a column of a list or database that matches conditions that you specify.');
7+
$category = 'Database';
8+
$functionName = 'DGET';
9+
$description = 'Extracts a single value from a column of a list or database that matches criteria that you specify';
10+
11+
$helper->titles($category, $functionName, $description);
812

913
// Create new PhpSpreadsheet object
1014
$spreadsheet = new Spreadsheet();
@@ -21,7 +25,7 @@
2125
];
2226
$criteria = [['Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height'],
2327
['="=Apple"', '>10', null, null, null, '<16'],
24-
['="=Pear"', null, null, null, null, null],
28+
['="=Pear"', '>12', null, null, null, null],
2529
];
2630

2731
$worksheet->fromArray($criteria, null, 'A1');
@@ -30,23 +34,25 @@
3034
$worksheet->setCellValue('A12', 'The height of the Apple tree between 10\' and 16\' tall');
3135
$worksheet->setCellValue('B12', '=DGET(A4:E10,"Height",A1:F2)');
3236

37+
$worksheet->setCellValue('A13', 'The height of the Apple tree (will return an Excel error, because there is more than one apple tree)');
38+
$worksheet->setCellValue('B13', '=DGET(A4:E10,"Height",A1:A2)');
39+
3340
$helper->log('Database');
3441

3542
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
36-
var_dump($databaseData);
43+
$helper->displayGrid($databaseData);
3744

3845
// Test the formulae
3946
$helper->log('Criteria');
4047

41-
$helper->log('ALL');
48+
$criteriaData = $worksheet->rangeToArray('A1:F2', null, true, true, true);
49+
$helper->displayGrid($criteriaData);
4250

43-
$helper->log($worksheet->getCell('A12')->getValue());
44-
$helper->log('DMAX() Result is ' . $worksheet->getCell('B12')->getCalculatedValue());
51+
$helper->logCalculationResult($worksheet, $functionName, 'B12', 'A12');
4552

4653
$helper->log('Criteria');
4754

4855
$criteriaData = $worksheet->rangeToArray('A1:A2', null, true, true, true);
49-
var_dump($criteriaData);
56+
$helper->displayGrid($criteriaData);
5057

51-
$helper->log($worksheet->getCell('A13')->getValue());
52-
$helper->log('DMAX() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
58+
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');

samples/Calculations/Database/DMAX.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
require __DIR__ . '/../../Header.php';
66

7-
$helper->log('Returns the maximum value from selected database entries.');
7+
$category = 'Database';
8+
$functionName = 'DMAX';
9+
$description = 'Returns the maximum value from selected database entries';
10+
11+
$helper->titles($category, $functionName, $description);
812

913
// Create new PhpSpreadsheet object
1014
$spreadsheet = new Spreadsheet();
@@ -36,20 +40,18 @@
3640
$helper->log('Database');
3741

3842
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
39-
var_dump($databaseData);
43+
$helper->displayGrid($databaseData);
4044

4145
// Test the formulae
4246
$helper->log('Criteria');
4347

4448
$helper->log('ALL');
4549

46-
$helper->log($worksheet->getCell('A12')->getValue());
47-
$helper->log('DMAX() Result is ' . $worksheet->getCell('B12')->getCalculatedValue());
50+
$helper->logCalculationResult($worksheet, $functionName, 'B12', 'A12');
4851

4952
$helper->log('Criteria');
5053

5154
$criteriaData = $worksheet->rangeToArray('A1:A2', null, true, true, true);
52-
var_dump($criteriaData);
55+
$helper->displayGrid($criteriaData);
5356

54-
$helper->log($worksheet->getCell('A13')->getValue());
55-
$helper->log('DMAX() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
57+
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');

samples/Calculations/Database/DMIN.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
require __DIR__ . '/../../Header.php';
66

7-
$helper->log('Returns the minimum value from selected database entries.');
7+
$category = 'Database';
8+
$functionName = 'DMIN';
9+
$description = 'Returns the minimum value from selected database entries';
10+
11+
$helper->titles($category, $functionName, $description);
812

913
// Create new PhpSpreadsheet object
1014
$spreadsheet = new Spreadsheet();
@@ -36,20 +40,18 @@
3640
$helper->log('Database');
3741

3842
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
39-
var_dump($databaseData);
43+
$helper->displayGrid($databaseData);
4044

4145
// Test the formulae
4246
$helper->log('Criteria');
4347

4448
$helper->log('ALL');
4549

46-
$helper->log($worksheet->getCell('A12')->getValue());
47-
$helper->log('DMIN() Result is ' . $worksheet->getCell('B12')->getCalculatedValue());
50+
$helper->logCalculationResult($worksheet, $functionName, 'B12', 'A12');
4851

4952
$helper->log('Criteria');
5053

5154
$criteriaData = $worksheet->rangeToArray('A1:A2', null, true, true, true);
52-
var_dump($criteriaData);
55+
$helper->displayGrid($criteriaData);
5356

54-
$helper->log($worksheet->getCell('A13')->getValue());
55-
$helper->log('DMIN() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
57+
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');

samples/Calculations/Database/DPRODUCT.php

+15-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
require __DIR__ . '/../../Header.php';
66

7-
$helper->log('Multiplies the values in a column of a list or database that match conditions that you specify.');
7+
$category = 'Database';
8+
$functionName = 'DPRODUCT';
9+
$description = 'Multiplies the values in a column of a list or database that match conditions that you specify';
10+
11+
$helper->titles($category, $functionName, $description);
812

913
// Create new PhpSpreadsheet object
1014
$spreadsheet = new Spreadsheet();
@@ -16,7 +20,7 @@
1620
['Pear', 12, 12, 10, 96.00],
1721
['Cherry', 13, 14, 9, 105.00],
1822
['Apple', 14, 15, 10, 75.00],
19-
['Pear', 9, 8, 8, 76.80],
23+
['Pear', 9, 8, 8, 77.00],
2024
['Apple', 8, 9, 6, 45.00],
2125
];
2226
$criteria = [['Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height'],
@@ -30,23 +34,25 @@
3034
$worksheet->setCellValue('A12', 'The product of the yields of all Apple trees over 10\' in the orchard');
3135
$worksheet->setCellValue('B12', '=DPRODUCT(A4:E10,"Yield",A1:B2)');
3236

37+
$worksheet->setCellValue('A13', 'The product of the yields of all Apple trees in the orchard');
38+
$worksheet->setCellValue('B13', '=DPRODUCT(A4:E10,"Yield",A1:A2)');
39+
3340
$helper->log('Database');
3441

3542
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
36-
var_dump($databaseData);
43+
$helper->displayGrid($databaseData);
3744

3845
// Test the formulae
3946
$helper->log('Criteria');
4047

41-
$helper->log('ALL');
48+
$criteriaData = $worksheet->rangeToArray('A1:B2', null, true, true, true);
49+
$helper->displayGrid($criteriaData);
4250

43-
$helper->log($worksheet->getCell('A12')->getValue());
44-
$helper->log('DMAX() Result is ' . $worksheet->getCell('B12')->getCalculatedValue());
51+
$helper->logCalculationResult($worksheet, $functionName, 'B12', 'A12');
4552

4653
$helper->log('Criteria');
4754

4855
$criteriaData = $worksheet->rangeToArray('A1:A2', null, true, true, true);
49-
var_dump($criteriaData);
56+
$helper->displayGrid($criteriaData);
5057

51-
$helper->log($worksheet->getCell('A13')->getValue());
52-
$helper->log('DMAX() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
58+
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');

samples/Calculations/Database/DSTDEV.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
require __DIR__ . '/../../Header.php';
66

7-
$helper->log('Estimates the standard deviation based on a sample of selected database entries.');
7+
$category = 'Database';
8+
$functionName = 'DSTDEV';
9+
$description = 'Estimates the standard deviation based on a sample of selected database entries';
10+
11+
$helper->titles($category, $functionName, $description);
812

913
// Create new PhpSpreadsheet object
1014
$spreadsheet = new Spreadsheet();
@@ -36,21 +40,19 @@
3640
$helper->log('Database');
3741

3842
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
39-
var_dump($databaseData);
43+
$helper->displayGrid($databaseData);
4044

4145
// Test the formulae
4246
$helper->log('Criteria');
4347

4448
$criteriaData = $worksheet->rangeToArray('A1:A3', null, true, true, true);
45-
var_dump($criteriaData);
49+
$helper->displayGrid($criteriaData);
4650

47-
$helper->log($worksheet->getCell('A12')->getValue());
48-
$helper->log('DSTDEV() Result is ' . $worksheet->getCell('B12')->getCalculatedValue());
51+
$helper->logCalculationResult($worksheet, $functionName, 'B12', 'A12');
4952

5053
$helper->log('Criteria');
5154

5255
$criteriaData = $worksheet->rangeToArray('A1:A3', null, true, true, true);
53-
var_dump($criteriaData);
56+
$helper->displayGrid($criteriaData);
5457

55-
$helper->log($worksheet->getCell('A13')->getValue());
56-
$helper->log('DSTDEV() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
58+
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');

0 commit comments

Comments
 (0)