mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-18 01:43:26 +00:00
accb321197
Excel supports the following notations for cell styles: - `[$-F800]` and `[$-x-sysdate]` will format the date according to what appears to be the user's system long date format. - `[$-F400]` and `[$-x-systime]`, will format the time according to what appears to be the user's system long time format. - Builtin style 14 will format date according to what appears to be the user's system short date format. - Builtin style 22 will format date and time according to the user's preference. It appears that the date portion is formatted according to the user's system short date format, but the time portion is formatted according to a format which is neither system short time format nor system long time format, so I'm not sure how this preference is set. For F800, sysdate, F400, and systime, any other characters in the style are ignored, except that, if you have more than one of this type of block in the style, Excel will treat it as corrupt (error message on open and style changed to General). Support is added for the new codes. In addition, note that the value displayed in the cell may differ in different environments. To give the PhpSpreadsheet programmer an opportunity to emulate what the intended audience will most often see, properties `shortDateFormat` (default value is builtin 14), `longDateFormat` (default value is `dddd, mmmm d, yyyy`), `dateTimeFormat` (defaults to builtin 22), and `timeFormat` (default is `FORMAT_DATE_TIME2`), with corresponding setters and getters, are added to Style/NumberFormat. Note that, if these properties are set to some other value in PhpSpreadsheet, it will not affect the values in the cell or the style - it is merely a convenience for the programmer. It will, however, affect column width if autosize is specified for the column. If the programmer does not alter any of the new properties, the output should be unchanged from before for builtins 14 and 22. The new styles are also recognized by the `TEXT` function. In this case, the cell's calculated value may differ from user to user. Note that this is a small subset of adding locale information to styles. No attempt is made to support any of the other possibilities - locale data will continue to be passed through to the spreadsheet, but PhpSpreadsheet will discard it before attempting to generate the formatted value of a cell.
121 lines
5.4 KiB
PHP
121 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Style;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class NumberFormatSystemDateTimeTest extends TestCase
|
|
{
|
|
private string $shortDateFormat;
|
|
|
|
private string $longDateFormat;
|
|
|
|
private string $dateTimeFormat;
|
|
|
|
private string $timeFormat;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->shortDateFormat = NumberFormat::getShortDateFormat();
|
|
$this->longDateFormat = NumberFormat::getLongDateFormat();
|
|
$this->dateTimeFormat = NumberFormat::getDateTimeFormat();
|
|
$this->timeFormat = NumberFormat::getTimeFormat();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
NumberFormat::setShortDateFormat($this->shortDateFormat);
|
|
NumberFormat::setLongDateFormat($this->longDateFormat);
|
|
NumberFormat::setDateTimeFormat($this->dateTimeFormat);
|
|
NumberFormat::setTimeFormat($this->timeFormat);
|
|
}
|
|
|
|
public function testOverrides(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$formula = '=DATEVALUE("2024-02-29")+TIMEVALUE("8:12:15 AM")';
|
|
$sheet->getCell('A1')->setValue($formula);
|
|
$sheet->getCell('A2')->setValue($formula);
|
|
$sheet->getStyle('A2')->getNumberFormat()
|
|
->setBuiltinFormatCode(14);
|
|
$sheet->getCell('A3')->setValue($formula);
|
|
$sheet->getStyle('A3')->getNumberFormat()
|
|
->setBuiltinFormatCode(15);
|
|
$sheet->getCell('A4')->setValue($formula);
|
|
$sheet->getStyle('A4')->getNumberFormat()
|
|
->setBuiltinFormatCode(22);
|
|
$sheet->getCell('A5')->setValue($formula);
|
|
$sheet->getStyle('A5')->getNumberFormat()
|
|
->setFormatCode('[$-F800]');
|
|
$sheet->getCell('A6')->setValue($formula);
|
|
$sheet->getStyle('A6')->getNumberFormat()
|
|
->setFormatCode('[$-F400]');
|
|
$sheet->getCell('A7')->setValue($formula);
|
|
$sheet->getStyle('A7')->getNumberFormat()
|
|
->setFormatCode('[$-x-sysdate]');
|
|
$sheet->getCell('A8')->setValue($formula);
|
|
$sheet->getStyle('A8')->getNumberFormat()
|
|
->setFormatCode('[$-x-systime]');
|
|
$sheet->getCell('A9')->setValue($formula);
|
|
$sheet->getStyle('A9')->getNumberFormat()
|
|
->setFormatCode('hello' . NumberFormat::FORMAT_SYSDATE_F800 . 'goodbye');
|
|
NumberFormat::setShortDateFormat('yyyy/mm/dd');
|
|
NumberFormat::setDateTimeFormat('yyyy/mm/dd hh:mm AM/PM');
|
|
NumberFormat::setLongDateFormat('dddd d mmm yyyy');
|
|
NumberFormat::setTimeFormat('h:mm');
|
|
self::assertSame('2024/02/29', $sheet->getCell('A2')->getformattedValue());
|
|
self::assertSame('2024/02/29 08:12 AM', $sheet->getCell('A4')->getformattedValue());
|
|
self::assertSame('Thursday 29 Feb 2024', $sheet->getCell('A5')->getformattedValue());
|
|
self::assertSame('8:12', $sheet->getCell('A6')->getformattedValue());
|
|
self::assertSame('Thursday 29 Feb 2024', $sheet->getCell('A7')->getformattedValue());
|
|
self::assertSame('8:12', $sheet->getCell('A8')->getformattedValue());
|
|
self::assertSame('Thursday 29 Feb 2024', $sheet->getCell('A9')->getformattedValue());
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public function testDefaults(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$formula = '=DATEVALUE("2024-02-29")+TIMEVALUE("8:12:15 AM")';
|
|
$sheet->getCell('A1')->setValue($formula);
|
|
$sheet->getCell('A2')->setValue($formula);
|
|
$sheet->getStyle('A2')->getNumberFormat()
|
|
->setBuiltinFormatCode(14);
|
|
$sheet->getCell('A3')->setValue($formula);
|
|
$sheet->getStyle('A3')->getNumberFormat()
|
|
->setBuiltinFormatCode(15);
|
|
$sheet->getCell('A4')->setValue($formula);
|
|
$sheet->getStyle('A4')->getNumberFormat()
|
|
->setBuiltinFormatCode(22);
|
|
$sheet->getCell('A5')->setValue($formula);
|
|
$sheet->getStyle('A5')->getNumberFormat()
|
|
->setFormatCode('[$-F800]');
|
|
$sheet->getCell('A6')->setValue($formula);
|
|
$sheet->getStyle('A6')->getNumberFormat()
|
|
->setFormatCode('[$-F400]');
|
|
$sheet->getCell('A7')->setValue($formula);
|
|
$sheet->getStyle('A7')->getNumberFormat()
|
|
->setFormatCode('[$-x-sysdate]');
|
|
$sheet->getCell('A8')->setValue($formula);
|
|
$sheet->getStyle('A8')->getNumberFormat()
|
|
->setFormatCode('[$-x-systime]');
|
|
$sheet->getCell('A9')->setValue($formula);
|
|
$sheet->getStyle('A9')->getNumberFormat()
|
|
->setFormatCode('hello' . NumberFormat::FORMAT_SYSDATE_F800 . 'goodbye');
|
|
self::assertSame('2/29/2024', $sheet->getCell('A2')->getformattedValue());
|
|
self::assertSame('2/29/2024 8:12', $sheet->getCell('A4')->getformattedValue());
|
|
self::assertSame('Thursday, February 29, 2024', $sheet->getCell('A5')->getformattedValue());
|
|
self::assertSame('8:12:15 AM', $sheet->getCell('A6')->getformattedValue());
|
|
self::assertSame('Thursday, February 29, 2024', $sheet->getCell('A7')->getformattedValue());
|
|
self::assertSame('8:12:15 AM', $sheet->getCell('A8')->getformattedValue());
|
|
self::assertSame('Thursday, February 29, 2024', $sheet->getCell('A9')->getformattedValue());
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
}
|