mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-17 15:04:31 +00:00
ecbd702628
Phpstan hasn't identified any errors in Samples in a long time. But, as it turns out, one particular error is suppressed: ``` Variable $helper might not be defined. ``` This error would be generated by almost all samples, and the errors that it suppresses will become problematic if we move to Level 10. We are not yet committed to doing that, but it is pretty easy to write a script to change the samples so that error no longer happens, and there isn't really any reason to delay doing so. The results of that script constitute this PR.
61 lines
1.6 KiB
PHP
61 lines
1.6 KiB
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\NamedRange;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->setActiveSheetIndex(0);
|
|
|
|
// Set up some basic data for a timesheet
|
|
$worksheet
|
|
->setCellValue('A1', 'Charge Rate/hour:')
|
|
->setCellValue('B1', '7.50')
|
|
->setCellValue('A3', 'Date')
|
|
->setCellValue('B3', 'Hours')
|
|
->setCellValue('C3', 'Charge');
|
|
|
|
// Define named range using an absolute cell reference
|
|
$spreadsheet->addNamedRange(new NamedRange('CHARGE_RATE', $worksheet, '=$B$1'));
|
|
|
|
$workHours = [
|
|
'2020-0-06' => 7.5,
|
|
'2020-0-07' => 7.25,
|
|
'2020-0-08' => 6.5,
|
|
'2020-0-09' => 7.0,
|
|
'2020-0-10' => 5.5,
|
|
];
|
|
|
|
// Populate the Timesheet
|
|
$startRow = 4;
|
|
$row = $startRow;
|
|
foreach ($workHours as $date => $hours) {
|
|
$worksheet
|
|
->setCellValue("A{$row}", $date)
|
|
->setCellValue("B{$row}", $hours)
|
|
->setCellValue("C{$row}", "=B{$row}*CHARGE_RATE");
|
|
++$row;
|
|
}
|
|
$endRow = $row - 1;
|
|
|
|
++$row;
|
|
$worksheet
|
|
->setCellValue("B{$row}", "=SUM(B{$startRow}:B{$endRow})")
|
|
->setCellValue("C{$row}", "=SUM(C{$startRow}:C{$endRow})");
|
|
|
|
/** @var float */
|
|
$calc1 = $worksheet->getCell("B{$row}")->getCalculatedValue();
|
|
/** @var float */
|
|
$value = $worksheet->getCell('B1')->getValue();
|
|
/** @var float */
|
|
$calc2 = $worksheet->getCell("C{$row}")->getCalculatedValue();
|
|
$helper->log(sprintf(
|
|
'Worked %.2f hours at a rate of %.2f - Charge to the client is %.2f',
|
|
$calc1,
|
|
$value,
|
|
$calc2
|
|
));
|
|
|
|
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
|