Skip to content

Update Excel function samples for Database and Date/Time functions #3047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions samples/Calculations/Database/DAVERAGE.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

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

$helper->log('Returns the average of selected database entries.');
$category = 'Database';
$functionName = 'DAVERAGE';
$description = 'Returns the average of selected database entries that match criteria';

$helper->titles($category, $functionName, $description);

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
Expand Down Expand Up @@ -36,21 +40,19 @@
$helper->log('Database');

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

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

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

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

$helper->log('Criteria');

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

$helper->log($worksheet->getCell('A13')->getValue());
$helper->log('DAVERAGE() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');
33 changes: 18 additions & 15 deletions samples/Calculations/Database/DCOUNT.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require __DIR__ . '/../../Header.php';
$helper->log('Counts the cells that contain numbers in a database.');

$category = 'Database';
$functionName = 'DCOUNT';
$description = 'Counts the cells that contain numbers in a set of database records that match criteria';

$helper->titles($category, $functionName, $description);

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
Expand All @@ -14,9 +19,9 @@
['Apple', 18, 20, 14, 105.00],
['Pear', 12, 12, 10, 96.00],
['Cherry', 13, 14, 9, 105.00],
['Apple', 14, 15, 10, 75.00],
['Pear', 9, 8, 8, 76.80],
['Apple', 8, 9, 6, 45.00],
['Apple', 14, 'N/A', 10, 75.00],
['Pear', 9, 8, 8, 77.00],
['Apple', 12, 11, 6, 45.00],
];
$criteria = [['Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height'],
['="=Apple"', '>10', null, null, null, '<16'],
Expand All @@ -26,30 +31,28 @@
$worksheet->fromArray($criteria, null, 'A1');
$worksheet->fromArray($database, null, 'A4');

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

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

$helper->log('Database');

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

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

$criteriaData = $worksheet->rangeToArray('A1:B2', null, true, true, true);
var_dump($criteriaData);
$criteriaData = $worksheet->rangeToArray('A1:F2', null, true, true, true);
$helper->displayGrid($criteriaData);

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

$helper->log('Criteria');

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

$helper->log($worksheet->getCell('A13')->getValue());
$helper->log('DCOUNT() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');
58 changes: 58 additions & 0 deletions samples/Calculations/Database/DCOUNTA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

use PhpOffice\PhpSpreadsheet\Spreadsheet;

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

$category = 'Database';
$functionName = 'DCOUNTA';
$description = 'Counts the cells in a set of database records that match criteria';

$helper->titles($category, $functionName, $description);

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Add some data
$database = [['Tree', 'Height', 'Age', 'Yield', 'Profit'],
['Apple', 18, 20, 14, 105.00],
['Pear', 12, 12, 10, 96.00],
['Cherry', 13, 14, 9, 105.00],
['Apple', 14, 'N/A', 10, 75.00],
['Pear', 9, 8, 8, 77.00],
['Apple', 12, 11, 6, 45.00],
];
$criteria = [['Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height'],
['="=Apple"', '>10', null, null, null, '<16'],
['="=Pear"', null, null, null, null, null],
];

$worksheet->fromArray($criteria, null, 'A1');
$worksheet->fromArray($database, null, 'A4');

$worksheet->setCellValue('A12', 'The Number of Apple trees between 10\' and 16\' in height');
$worksheet->setCellValue('B12', '=DCOUNTA(A4:E10,"Age",A1:F2)');

$worksheet->setCellValue('A13', 'The Number of Apple and Pear trees in the orchard');
$worksheet->setCellValue('B13', '=DCOUNTA(A4:E10,3,A1:A3)');

$helper->log('Database');

$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
$helper->displayGrid($databaseData);

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

$criteriaData = $worksheet->rangeToArray('A1:F2', null, true, true, true);
$helper->displayGrid($criteriaData);

$helper->logCalculationResult($worksheet, $functionName, 'B12', 'A12');

$helper->log('Criteria');

$criteriaData = $worksheet->rangeToArray('A1:A3', null, true, true, true);
$helper->displayGrid($criteriaData);

$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');
24 changes: 15 additions & 9 deletions samples/Calculations/Database/DGET.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

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

$helper->log('Extracts a single value from a column of a list or database that matches conditions that you specify.');
$category = 'Database';
$functionName = 'DGET';
$description = 'Extracts a single value from a column of a list or database that matches criteria that you specify';

$helper->titles($category, $functionName, $description);

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
Expand All @@ -21,7 +25,7 @@
];
$criteria = [['Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height'],
['="=Apple"', '>10', null, null, null, '<16'],
['="=Pear"', null, null, null, null, null],
['="=Pear"', '>12', null, null, null, null],
];

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

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

$helper->log('Database');

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

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

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

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

$helper->log('Criteria');

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

$helper->log($worksheet->getCell('A13')->getValue());
$helper->log('DMAX() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');
16 changes: 9 additions & 7 deletions samples/Calculations/Database/DMAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

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

$helper->log('Returns the maximum value from selected database entries.');
$category = 'Database';
$functionName = 'DMAX';
$description = 'Returns the maximum value from selected database entries';

$helper->titles($category, $functionName, $description);

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
Expand Down Expand Up @@ -36,20 +40,18 @@
$helper->log('Database');

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

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

$helper->log('ALL');

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

$helper->log('Criteria');

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

$helper->log($worksheet->getCell('A13')->getValue());
$helper->log('DMAX() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');
16 changes: 9 additions & 7 deletions samples/Calculations/Database/DMIN.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

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

$helper->log('Returns the minimum value from selected database entries.');
$category = 'Database';
$functionName = 'DMIN';
$description = 'Returns the minimum value from selected database entries';

$helper->titles($category, $functionName, $description);

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
Expand Down Expand Up @@ -36,20 +40,18 @@
$helper->log('Database');

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

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

$helper->log('ALL');

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

$helper->log('Criteria');

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

$helper->log($worksheet->getCell('A13')->getValue());
$helper->log('DMIN() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');
24 changes: 15 additions & 9 deletions samples/Calculations/Database/DPRODUCT.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

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

$helper->log('Multiplies the values in a column of a list or database that match conditions that you specify.');
$category = 'Database';
$functionName = 'DPRODUCT';
$description = 'Multiplies the values in a column of a list or database that match conditions that you specify';

$helper->titles($category, $functionName, $description);

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

$worksheet->setCellValue('A13', 'The product of the yields of all Apple trees in the orchard');
$worksheet->setCellValue('B13', '=DPRODUCT(A4:E10,"Yield",A1:A2)');

$helper->log('Database');

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

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

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

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

$helper->log('Criteria');

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

$helper->log($worksheet->getCell('A13')->getValue());
$helper->log('DMAX() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');
18 changes: 10 additions & 8 deletions samples/Calculations/Database/DSTDEV.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

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

$helper->log('Estimates the standard deviation based on a sample of selected database entries.');
$category = 'Database';
$functionName = 'DSTDEV';
$description = 'Estimates the standard deviation based on a sample of selected database entries';

$helper->titles($category, $functionName, $description);

// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
Expand Down Expand Up @@ -36,21 +40,19 @@
$helper->log('Database');

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

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

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

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

$helper->log('Criteria');

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

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