提交的内容

This commit is contained in:
2025-05-12 15:45:02 +08:00
parent 629c4750da
commit b48c692775
3043 changed files with 34732 additions and 60810 deletions

View File

@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Reader\Xls\Color\BIFF8;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
@ -67,14 +68,19 @@ class Formatter
// 3 sections: [POSITIVE/TEXT] [NEGATIVE] [ZERO]
// 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
$sectionCount = count($sections);
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . ')\\]/mui';
// Colour could be a named colour, or a numeric index entry in the colour-palette
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . '|color\\s*(\\d+))\\]/mui';
$cond_regex = '/\\[(>|>=|<|<=|=|<>)([+-]?\\d+([.]\\d+)?)\\]/';
$colors = ['', '', '', '', ''];
$conditionOperations = ['', '', '', '', ''];
$conditionComparisonValues = [0, 0, 0, 0, 0];
for ($idx = 0; $idx < $sectionCount; ++$idx) {
if (preg_match($color_regex, $sections[$idx], $matches)) {
$colors[$idx] = $matches[0];
if (isset($matches[2])) {
$colors[$idx] = '#' . BIFF8::lookup((int) $matches[2] + 7)['rgb'];
} else {
$colors[$idx] = $matches[0];
}
$sections[$idx] = (string) preg_replace($color_regex, '', $sections[$idx]);
}
if (preg_match($cond_regex, $sections[$idx], $matches)) {
@ -170,10 +176,11 @@ class Formatter
$format = (string) preg_replace('/_.?/ui', ' ', $format);
// Let's begin inspecting the format and converting the value to a formatted string
// Check for date/time characters (not inside quotes)
if (
(preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy](?=(?:[^"]|"[^"]*")*$)/miu', $format)) &&
(preg_match('/0(?![^\[]*\])/miu', $format) === 0)
// Check for date/time characters (not inside quotes)
(preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy](?=(?:[^"]|"[^"]*")*$)/miu', $format))
// A date/time with a decimal time shouldn't have a digit placeholder before the decimal point
&& (preg_match('/[0\?#]\.(?![^\[]*\])/miu', $format) === 0)
) {
// datetime format
$value = DateFormatter::format($value, $format);
@ -194,8 +201,6 @@ class Formatter
$value = $writerInstance->$function($value, $colors);
}
$value = str_replace(chr(0x00), '.', $value);
return $value;
return str_replace(chr(0x00), '.', $value);
}
}

View File

@ -89,13 +89,13 @@ class Accounting extends Currency
(
$this->currencySymbolPosition === self::LEADING_SYMBOL &&
$this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
) ? ' ' : '',
) ? "\u{a0}" : '',
$this->thousandsSeparator ? '#,##' : null,
$this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null,
(
$this->currencySymbolPosition === self::TRAILING_SYMBOL &&
$this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
) ? ' ' : '',
) ? "\u{a0}" : '',
$this->currencySymbolPosition === self::TRAILING_SYMBOL ? $this->formatCurrencyCode() : null
);
}

View File

@ -0,0 +1,125 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
class Date extends DateTimeWizard
{
/**
* Year (4 digits), e.g. 2023.
*/
public const YEAR_FULL = 'yyyy';
/**
* Year (last 2 digits), e.g. 23.
*/
public const YEAR_SHORT = 'yy';
public const MONTH_FIRST_LETTER = 'mmmmm';
/**
* Month name, long form, e.g. January.
*/
public const MONTH_NAME_FULL = 'mmmm';
/**
* Month name, short form, e.g. Jan.
*/
public const MONTH_NAME_SHORT = 'mmm';
/**
* Month number with a leading zero if required, e.g. 01.
*/
public const MONTH_NUMBER_LONG = 'mm';
/**
* Month number without a leading zero, e.g. 1.
*/
public const MONTH_NUMBER_SHORT = 'm';
/**
* Day of the week, full form, e.g. Tuesday.
*/
public const WEEKDAY_NAME_LONG = 'dddd';
/**
* Day of the week, short form, e.g. Tue.
*/
public const WEEKDAY_NAME_SHORT = 'ddd';
/**
* Day number with a leading zero, e.g. 03.
*/
public const DAY_NUMBER_LONG = 'dd';
/**
* Day number without a leading zero, e.g. 3.
*/
public const DAY_NUMBER_SHORT = 'd';
protected const DATE_BLOCKS = [
self::YEAR_FULL,
self::YEAR_SHORT,
self::MONTH_FIRST_LETTER,
self::MONTH_NAME_FULL,
self::MONTH_NAME_SHORT,
self::MONTH_NUMBER_LONG,
self::MONTH_NUMBER_SHORT,
self::WEEKDAY_NAME_LONG,
self::WEEKDAY_NAME_SHORT,
self::DAY_NUMBER_LONG,
self::DAY_NUMBER_SHORT,
];
public const SEPARATOR_DASH = '-';
public const SEPARATOR_DOT = '.';
public const SEPARATOR_SLASH = '/';
public const SEPARATOR_SPACE_NONBREAKING = "\u{a0}";
public const SEPARATOR_SPACE = ' ';
protected const DATE_DEFAULT = [
self::YEAR_FULL,
self::MONTH_NUMBER_LONG,
self::DAY_NUMBER_LONG,
];
/**
* @var string[]
*/
protected array $separators;
/**
* @var string[]
*/
protected array $formatBlocks;
/**
* @param null|string|string[] $separators
* If you want to use the same separator for all format blocks, then it can be passed as a string literal;
* if you wish to use different separators, then they should be passed as an array.
* If you want to use only a single format block, then pass a null as the separator argument
*/
public function __construct($separators = self::SEPARATOR_DASH, string ...$formatBlocks)
{
$separators ??= self::SEPARATOR_DASH;
$formatBlocks = (count($formatBlocks) === 0) ? self::DATE_DEFAULT : $formatBlocks;
$this->separators = $this->padSeparatorArray(
is_array($separators) ? $separators : [$separators],
count($formatBlocks) - 1
);
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
}
private function mapFormatBlocks(string $value): string
{
// Any date masking codes are returned as lower case values
if (in_array(mb_strtolower($value), self::DATE_BLOCKS, true)) {
return mb_strtolower($value);
}
// Wrap any string literals in quotes, so that they're clearly defined as string literals
return $this->wrapLiteral($value);
}
public function format(): string
{
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
class DateTime extends DateTimeWizard
{
/**
* @var string[]
*/
protected array $separators;
/**
* @var array<DateTimeWizard|string>
*/
protected array $formatBlocks;
/**
* @param null|string|string[] $separators
* If you want to use only a single format block, then pass a null as the separator argument
* @param DateTimeWizard|string ...$formatBlocks
*/
public function __construct($separators, ...$formatBlocks)
{
$this->separators = $this->padSeparatorArray(
is_array($separators) ? $separators : [$separators],
count($formatBlocks) - 1
);
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
}
/**
* @param DateTimeWizard|string $value
*/
private function mapFormatBlocks($value): string
{
// Any date masking codes are returned as lower case values
if (is_object($value)) {
// We can't explicitly test for Stringable until PHP >= 8.0
return $value;
}
// Wrap any string literals in quotes, so that they're clearly defined as string literals
return $this->wrapLiteral($value);
}
public function format(): string
{
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
abstract class DateTimeWizard implements Wizard
{
protected const NO_ESCAPING_NEEDED = "$+-/():!^&'~{}<>= ";
protected function padSeparatorArray(array $separators, int $count): array
{
$lastSeparator = array_pop($separators);
return $separators + array_fill(0, $count, $lastSeparator);
}
protected function escapeSingleCharacter(string $value): string
{
if (strpos(self::NO_ESCAPING_NEEDED, $value) !== false) {
return $value;
}
return "\\{$value}";
}
protected function wrapLiteral(string $value): string
{
if (mb_strlen($value, 'UTF-8') === 1) {
return $this->escapeSingleCharacter($value);
}
// Wrap any other string literals in quotes, so that they're clearly defined as string literals
return '"' . str_replace('"', '""', $value) . '"';
}
protected function intersperse(string $formatBlock, ?string $separator): string
{
return "{$formatBlock}{$separator}";
}
public function __toString(): string
{
return $this->format();
}
}

View File

@ -0,0 +1,153 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
class Duration extends DateTimeWizard
{
public const DAYS_DURATION = 'd';
/**
* Hours as a duration (can exceed 24), e.g. 29.
*/
public const HOURS_DURATION = '[h]';
/**
* Hours without a leading zero, e.g. 9.
*/
public const HOURS_SHORT = 'h';
/**
* Hours with a leading zero, e.g. 09.
*/
public const HOURS_LONG = 'hh';
/**
* Minutes as a duration (can exceed 60), e.g. 109.
*/
public const MINUTES_DURATION = '[m]';
/**
* Minutes without a leading zero, e.g. 5.
*/
public const MINUTES_SHORT = 'm';
/**
* Minutes with a leading zero, e.g. 05.
*/
public const MINUTES_LONG = 'mm';
/**
* Seconds as a duration (can exceed 60), e.g. 129.
*/
public const SECONDS_DURATION = '[s]';
/**
* Seconds without a leading zero, e.g. 2.
*/
public const SECONDS_SHORT = 's';
/**
* Seconds with a leading zero, e.g. 02.
*/
public const SECONDS_LONG = 'ss';
protected const DURATION_BLOCKS = [
self::DAYS_DURATION,
self::HOURS_DURATION,
self::HOURS_LONG,
self::HOURS_SHORT,
self::MINUTES_DURATION,
self::MINUTES_LONG,
self::MINUTES_SHORT,
self::SECONDS_DURATION,
self::SECONDS_LONG,
self::SECONDS_SHORT,
];
protected const DURATION_MASKS = [
self::DAYS_DURATION => self::DAYS_DURATION,
self::HOURS_DURATION => self::HOURS_SHORT,
self::MINUTES_DURATION => self::MINUTES_LONG,
self::SECONDS_DURATION => self::SECONDS_LONG,
];
protected const DURATION_DEFAULTS = [
self::HOURS_LONG => self::HOURS_DURATION,
self::HOURS_SHORT => self::HOURS_DURATION,
self::MINUTES_LONG => self::MINUTES_DURATION,
self::MINUTES_SHORT => self::MINUTES_DURATION,
self::SECONDS_LONG => self::SECONDS_DURATION,
self::SECONDS_SHORT => self::SECONDS_DURATION,
];
public const SEPARATOR_COLON = ':';
public const SEPARATOR_SPACE_NONBREAKING = "\u{a0}";
public const SEPARATOR_SPACE = ' ';
public const DURATION_DEFAULT = [
self::HOURS_DURATION,
self::MINUTES_LONG,
self::SECONDS_LONG,
];
/**
* @var string[]
*/
protected array $separators;
/**
* @var string[]
*/
protected array $formatBlocks;
protected bool $durationIsSet = false;
/**
* @param null|string|string[] $separators
* If you want to use the same separator for all format blocks, then it can be passed as a string literal;
* if you wish to use different separators, then they should be passed as an array.
* If you want to use only a single format block, then pass a null as the separator argument
*/
public function __construct($separators = self::SEPARATOR_COLON, string ...$formatBlocks)
{
$separators ??= self::SEPARATOR_COLON;
$formatBlocks = (count($formatBlocks) === 0) ? self::DURATION_DEFAULT : $formatBlocks;
$this->separators = $this->padSeparatorArray(
is_array($separators) ? $separators : [$separators],
count($formatBlocks) - 1
);
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
if ($this->durationIsSet === false) {
// We need at least one duration mask, so if none has been set we change the first mask element
// to a duration.
$this->formatBlocks[0] = self::DURATION_DEFAULTS[mb_strtolower($this->formatBlocks[0])];
}
}
private function mapFormatBlocks(string $value): string
{
// Any duration masking codes are returned as lower case values
if (in_array(mb_strtolower($value), self::DURATION_BLOCKS, true)) {
if (array_key_exists(mb_strtolower($value), self::DURATION_MASKS)) {
if ($this->durationIsSet) {
// We should only have a single duration mask, the first defined in the mask set,
// so convert any additional duration masks to standard time masks.
$value = self::DURATION_MASKS[mb_strtolower($value)];
}
$this->durationIsSet = true;
}
return mb_strtolower($value);
}
// Wrap any string literals in quotes, so that they're clearly defined as string literals
return $this->wrapLiteral($value);
}
public function format(): string
{
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
}
}

View File

@ -0,0 +1,105 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
class Time extends DateTimeWizard
{
/**
* Hours without a leading zero, e.g. 9.
*/
public const HOURS_SHORT = 'h';
/**
* Hours with a leading zero, e.g. 09.
*/
public const HOURS_LONG = 'hh';
/**
* Minutes without a leading zero, e.g. 5.
*/
public const MINUTES_SHORT = 'm';
/**
* Minutes with a leading zero, e.g. 05.
*/
public const MINUTES_LONG = 'mm';
/**
* Seconds without a leading zero, e.g. 2.
*/
public const SECONDS_SHORT = 's';
/**
* Seconds with a leading zero, e.g. 02.
*/
public const SECONDS_LONG = 'ss';
public const MORNING_AFTERNOON = 'AM/PM';
protected const TIME_BLOCKS = [
self::HOURS_LONG,
self::HOURS_SHORT,
self::MINUTES_LONG,
self::MINUTES_SHORT,
self::SECONDS_LONG,
self::SECONDS_SHORT,
self::MORNING_AFTERNOON,
];
public const SEPARATOR_COLON = ':';
public const SEPARATOR_SPACE_NONBREAKING = "\u{a0}";
public const SEPARATOR_SPACE = ' ';
protected const TIME_DEFAULT = [
self::HOURS_LONG,
self::MINUTES_LONG,
self::SECONDS_LONG,
];
/**
* @var string[]
*/
protected array $separators;
/**
* @var string[]
*/
protected array $formatBlocks;
/**
* @param null|string|string[] $separators
* If you want to use the same separator for all format blocks, then it can be passed as a string literal;
* if you wish to use different separators, then they should be passed as an array.
* If you want to use only a single format block, then pass a null as the separator argument
*/
public function __construct($separators = self::SEPARATOR_COLON, string ...$formatBlocks)
{
$separators ??= self::SEPARATOR_COLON;
$formatBlocks = (count($formatBlocks) === 0) ? self::TIME_DEFAULT : $formatBlocks;
$this->separators = $this->padSeparatorArray(
is_array($separators) ? $separators : [$separators],
count($formatBlocks) - 1
);
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
}
private function mapFormatBlocks(string $value): string
{
// Any date masking codes are returned as lower case values
// except for AM/PM, which is set to uppercase
if (in_array(mb_strtolower($value), self::TIME_BLOCKS, true)) {
return mb_strtolower($value);
} elseif (mb_strtoupper($value) === self::MORNING_AFTERNOON) {
return mb_strtoupper($value);
}
// Wrap any string literals in quotes, so that they're clearly defined as string literals
return $this->wrapLiteral($value);
}
public function format(): string
{
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
}
}