mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-17 18:01:42 +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.
44 lines
1.7 KiB
PHP
44 lines
1.7 KiB
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
|
|
$category = 'Date/Time';
|
|
$functionName = 'DATEVALUE';
|
|
$description = 'Converts a date in the form of text to an Excel serial number';
|
|
|
|
$helper->titles($category, $functionName, $description);
|
|
|
|
// Create new PhpSpreadsheet object
|
|
$spreadsheet = new Spreadsheet();
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
|
|
// Add some data
|
|
$testDates = ['26 March 2012', '29 Feb 2012', 'April 1, 2012', '25/12/2012',
|
|
'2012-Oct-31', '5th November', 'January 1st', 'April 2012',
|
|
'17-03', '03-17', '03-2012', '29 Feb 2011', '03-05-07',
|
|
'03-MAY-07', '03-13-07', '13-03-07', '03/13/07', '13/03/07',
|
|
];
|
|
$testDateCount = count($testDates);
|
|
|
|
for ($row = 1; $row <= $testDateCount; ++$row) {
|
|
$worksheet->setCellValue('A' . $row, $testDates[$row - 1]);
|
|
$worksheet->setCellValue('B' . $row, '=DATEVALUE(A' . $row . ')');
|
|
$worksheet->setCellValue('C' . $row, '=B' . $row);
|
|
}
|
|
|
|
$worksheet->getStyle('C1:C' . $testDateCount)
|
|
->getNumberFormat()
|
|
->setFormatCode('yyyy-mm-dd');
|
|
|
|
// Test the formulae
|
|
$helper->log('<strong>Warning: </strong>The PhpSpreadsheet DATEVALUE() function accepts a wider range of date formats than MS Excel DATEFORMAT() function.');
|
|
for ($row = 1; $row <= $testDateCount; ++$row) {
|
|
$helper->log("(A{$row}) Date String: " . $worksheet->getCell('A' . $row)->getFormattedValue());
|
|
$helper->log('Formula: ' . $worksheet->getCell('B' . $row)->getValueString());
|
|
$helper->log('Excel DateStamp: ' . $worksheet->getCell('B' . $row)->getCalculatedValueString());
|
|
$helper->log('Formatted DateStamp: ' . $worksheet->getCell('C' . $row)->getFormattedValue());
|
|
$helper->log('');
|
|
}
|