Update Excel function samples for Database functions

This commit is contained in:
MarkBaker 2022-09-03 12:58:23 +02:00
parent 4f8aa806bc
commit ad2d3df2f7
14 changed files with 401 additions and 87 deletions

View File

@ -4,7 +4,11 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
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();
@ -36,21 +40,19 @@ $worksheet->setCellValue('B13', '=DAVERAGE(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);
$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');

View File

@ -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();
@ -14,9 +19,9 @@ $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, 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'],
@ -26,30 +31,28 @@ $criteria = [['Tree', 'Height', 'Age', 'Yield', 'Profit', 'Height'],
$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');

View File

@ -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');

View File

@ -4,7 +4,11 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
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();
@ -21,7 +25,7 @@ $database = [['Tree', 'Height', 'Age', 'Yield', 'Profit'],
];
$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');
@ -30,23 +34,25 @@ $worksheet->fromArray($database, null, 'A4');
$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');

View File

@ -4,7 +4,11 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
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();
@ -36,20 +40,18 @@ $worksheet->setCellValue('B13', '=DMAX(A4:E10,3,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');
$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');

View File

@ -4,7 +4,11 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
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();
@ -36,20 +40,18 @@ $worksheet->setCellValue('B13', '=DMIN(A4:E10,3,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');
$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');

View File

@ -4,7 +4,11 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
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();
@ -16,7 +20,7 @@ $database = [['Tree', 'Height', 'Age', 'Yield', 'Profit'],
['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'],
@ -30,23 +34,25 @@ $worksheet->fromArray($database, null, 'A4');
$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');

View File

@ -4,7 +4,11 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
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();
@ -36,21 +40,19 @@ $worksheet->setCellValue('B13', '=DSTDEV(A4:E10,2,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: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');

View File

@ -3,7 +3,12 @@
use PhpOffice\PhpSpreadsheet\Spreadsheet;
require __DIR__ . '/../../Header.php';
$helper->log('Calculates the standard deviation based on the entire population of selected database entries.');
$category = 'Database';
$functionName = 'DSTDEVP';
$description = 'Calculates the standard deviation based on the entire population of selected database entries';
$helper->titles($category, $functionName, $description);
// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
@ -35,21 +40,19 @@ $worksheet->setCellValue('B13', '=DSTDEVP(A4:E10,2,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:A3', null, true, true, true);
var_dump($criteriaData);
$helper->displayGrid($criteriaData);
$helper->log($worksheet->getCell('A12')->getValue());
$helper->log('DSTDEVP() 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('DSTDEVP() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');

View File

@ -0,0 +1,58 @@
<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
require __DIR__ . '/../../Header.php';
$category = 'Database';
$functionName = 'DSUM';
$description = 'Returns the sum of selected database entries';
$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, 15, 10, 75.00],
['Pear', 9, 8, 8, 76.80],
['Apple', 8, 9, 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 total profit from apple trees');
$worksheet->setCellValue('B12', '=DSUM(A4:E10,"Profit",A1:A2)');
$worksheet->setCellValue('A13', 'Total profit from apple trees with a height between 10 and 16 feet, and all pear trees');
$worksheet->setCellValue('B13', '=DSUM(A4:E10,"Profit",A1:F3)');
$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:A2', null, true, true, true);
$helper->displayGrid($criteriaData);
$helper->logCalculationResult($worksheet, $functionName, 'B12', 'A12');
$helper->log('Criteria');
$criteriaData = $worksheet->rangeToArray('A1:F3', null, true, true, true);
$helper->displayGrid($criteriaData);
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');

View File

@ -3,7 +3,12 @@
use PhpOffice\PhpSpreadsheet\Spreadsheet;
require __DIR__ . '/../../Header.php';
$helper->log('Estimates variance based on a sample from selected database entries.');
$category = 'Database';
$functionName = 'DVAR';
$description = 'Estimates variance based on a sample from selected database entries';
$helper->titles($category, $functionName, $description);
// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
@ -35,21 +40,19 @@ $worksheet->setCellValue('B13', '=DVAR(A4:E10,2,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:A3', null, true, true, true);
var_dump($criteriaData);
$helper->displayGrid($criteriaData);
$helper->log($worksheet->getCell('A12')->getValue());
$helper->log('DVAR() 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('DVAR() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');

View File

@ -4,7 +4,11 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
require __DIR__ . '/../../Header.php';
$helper->log('Calculates variance based on the entire population of selected database entries,');
$category = 'Database';
$functionName = 'DVARP';
$description = 'Calculates variance based on the entire population of selected database entries';
$helper->titles($category, $functionName, $description);
// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
@ -36,21 +40,19 @@ $worksheet->setCellValue('B13', '=DVARP(A4:E10,2,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:A3', null, true, true, true);
var_dump($criteriaData);
$helper->displayGrid($criteriaData);
$helper->log($worksheet->getCell('A12')->getValue());
$helper->log('DVARP() 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('DVARP() Result is ' . $worksheet->getCell('B13')->getCalculatedValue());
$helper->logCalculationResult($worksheet, $functionName, 'B13', 'A13');

View File

@ -4,6 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Helper;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
@ -182,6 +183,33 @@ class Sample
echo date('H:i:s ') . $message . $eol;
}
public function titles(string $category, string $functionName, ?string $description = null): void
{
$this->log(sprintf('%s Functions:', $category));
$description === null
? $this->log(sprintf('%s()', rtrim($functionName, '()')))
: $this->log(sprintf('%s() - %s.', rtrim($functionName, '()'), rtrim($description, '.')));
}
public function displayGrid(array $matrix): void
{
$renderer = new TextGrid($matrix, $this->isCli());
echo $renderer->render();
}
public function logCalculationResult(
Worksheet $worksheet,
string $functionName,
string $formulaCell,
?string $descriptionCell = null
): void {
if ($descriptionCell !== null) {
$this->log($worksheet->getCell($descriptionCell)->getValue());
}
$this->log($worksheet->getCell($formulaCell)->getValue());
$this->log(sprintf('%s() Result is ', $functionName) . $worksheet->getCell($formulaCell)->getCalculatedValue());
}
/**
* Log ending notes.
*/

View File

@ -0,0 +1,139 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Helper;
class TextGrid
{
/**
* @var bool
*/
private $isCli = true;
/**
* @var array
*/
protected $matrix;
/**
* @var array
*/
protected $rows;
/**
* @var array
*/
protected $columns;
/**
* @var string
*/
private $gridDisplay;
public function __construct(array $matrix, bool $isCli = true)
{
$this->rows = array_keys($matrix);
$this->columns = array_keys($matrix[$this->rows[0]]);
$matrix = array_values($matrix);
array_walk(
$matrix,
function (&$row): void {
$row = array_values($row);
}
);
$this->matrix = $matrix;
$this->isCli = $isCli;
}
public function render(): string
{
$this->gridDisplay = $this->isCli ? '' : '<code>';
$maxRow = max($this->rows);
$maxRowLength = strlen((string) $maxRow) + 1;
$columnWidths = $this->getColumnWidths($this->matrix);
$this->renderColumnHeader($maxRowLength, $columnWidths);
$this->renderRows($maxRowLength, $columnWidths);
$this->renderFooter($maxRowLength, $columnWidths);
$this->gridDisplay .= $this->isCli ? '' : '</code>';
return $this->gridDisplay;
}
private function renderRows(int $maxRowLength, array $columnWidths): void
{
foreach ($this->matrix as $row => $rowData) {
$this->gridDisplay .= '|' . str_pad((string) $this->rows[$row], $maxRowLength, ' ', STR_PAD_LEFT) . ' ';
$this->renderCells($rowData, $columnWidths);
$this->gridDisplay .= '|' . PHP_EOL;
}
}
private function renderCells(array $rowData, array $columnWidths): void
{
foreach ($rowData as $column => $cell) {
$cell = ($this->isCli) ? (string) $cell : htmlentities((string) $cell);
$this->gridDisplay .= '| ';
$this->gridDisplay .= str_pad($cell, $columnWidths[$column] + 1, ' ');
}
}
private function renderColumnHeader(int $maxRowLength, array $columnWidths): void
{
$this->gridDisplay .= str_repeat(' ', $maxRowLength + 2);
foreach ($this->columns as $column => $reference) {
$this->gridDisplay .= '+-' . str_repeat('-', $columnWidths[$column] + 1);
}
$this->gridDisplay .= '+' . PHP_EOL;
$this->gridDisplay .= str_repeat(' ', $maxRowLength + 2);
foreach ($this->columns as $column => $reference) {
$this->gridDisplay .= '| ' . str_pad((string) $reference, $columnWidths[$column] + 1, ' ');
}
$this->gridDisplay .= '|' . PHP_EOL;
$this->renderFooter($maxRowLength, $columnWidths);
}
private function renderFooter(int $maxRowLength, array $columnWidths): void
{
$this->gridDisplay .= '+' . str_repeat('-', $maxRowLength + 1);
foreach ($this->columns as $column => $reference) {
$this->gridDisplay .= '+-';
$this->gridDisplay .= str_pad((string) '', $columnWidths[$column] + 1, '-');
}
$this->gridDisplay .= '+' . PHP_EOL;
}
private function getColumnWidths(array $matrix): array
{
$columnCount = count($this->matrix, COUNT_RECURSIVE) / count($this->matrix);
$columnWidths = [];
for ($column = 0; $column < $columnCount; ++$column) {
$columnWidths[] = $this->getColumnWidth(array_column($this->matrix, $column));
}
return $columnWidths;
}
private function getColumnWidth(array $columnData): int
{
$columnWidth = 0;
$columnData = array_values($columnData);
foreach ($columnData as $columnValue) {
if (is_string($columnValue)) {
$columnWidth = max($columnWidth, strlen($columnValue));
} elseif (is_bool($columnValue)) {
$columnWidth = max($columnWidth, strlen($columnValue ? 'TRUE' : 'FALSE'));
}
$columnWidth = max($columnWidth, strlen((string) $columnWidth));
}
return $columnWidth;
}
}