init commit

This commit is contained in:
2026-03-17 09:56:00 +08:00
commit e2c8ae752d
6827 changed files with 1211784 additions and 0 deletions

View File

@@ -0,0 +1,483 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
class Alignment extends Supervisor
{
// Horizontal alignment styles
const HORIZONTAL_GENERAL = 'general';
const HORIZONTAL_LEFT = 'left';
const HORIZONTAL_RIGHT = 'right';
const HORIZONTAL_CENTER = 'center';
const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
const HORIZONTAL_JUSTIFY = 'justify';
const HORIZONTAL_FILL = 'fill';
const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only
// Vertical alignment styles
const VERTICAL_BOTTOM = 'bottom';
const VERTICAL_TOP = 'top';
const VERTICAL_CENTER = 'center';
const VERTICAL_JUSTIFY = 'justify';
const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only
// Read order
const READORDER_CONTEXT = 0;
const READORDER_LTR = 1;
const READORDER_RTL = 2;
// Special value for Text Rotation
const TEXTROTATION_STACK_EXCEL = 255;
const TEXTROTATION_STACK_PHPSPREADSHEET = -165; // 90 - 255
/**
* Horizontal alignment.
*
* @var null|string
*/
protected $horizontal = self::HORIZONTAL_GENERAL;
/**
* Vertical alignment.
*
* @var null|string
*/
protected $vertical = self::VERTICAL_BOTTOM;
/**
* Text rotation.
*
* @var null|int
*/
protected $textRotation = 0;
/**
* Wrap text.
*
* @var bool
*/
protected $wrapText = false;
/**
* Shrink to fit.
*
* @var bool
*/
protected $shrinkToFit = false;
/**
* Indent - only possible with horizontal alignment left and right.
*
* @var int
*/
protected $indent = 0;
/**
* Read order.
*
* @var int
*/
protected $readOrder = 0;
/**
* Create a new Alignment.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
if ($isConditional) {
$this->horizontal = null;
$this->vertical = null;
$this->textRotation = null;
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Alignment
*/
public function getSharedComponent()
{
return $this->parent->getSharedComponent()->getAlignment();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['alignment' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
* [
* 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
* 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
* 'textRotation' => 0,
* 'wrapText' => TRUE
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())
->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['horizontal'])) {
$this->setHorizontal($styleArray['horizontal']);
}
if (isset($styleArray['vertical'])) {
$this->setVertical($styleArray['vertical']);
}
if (isset($styleArray['textRotation'])) {
$this->setTextRotation($styleArray['textRotation']);
}
if (isset($styleArray['wrapText'])) {
$this->setWrapText($styleArray['wrapText']);
}
if (isset($styleArray['shrinkToFit'])) {
$this->setShrinkToFit($styleArray['shrinkToFit']);
}
if (isset($styleArray['indent'])) {
$this->setIndent($styleArray['indent']);
}
if (isset($styleArray['readOrder'])) {
$this->setReadOrder($styleArray['readOrder']);
}
}
return $this;
}
/**
* Get Horizontal.
*
* @return null|string
*/
public function getHorizontal()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHorizontal();
}
return $this->horizontal;
}
/**
* Set Horizontal.
*
* @param string $horizontalAlignment see self::HORIZONTAL_*
*
* @return $this
*/
public function setHorizontal(string $horizontalAlignment)
{
if ($horizontalAlignment == '') {
$horizontalAlignment = self::HORIZONTAL_GENERAL;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['horizontal' => $horizontalAlignment]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->horizontal = $horizontalAlignment;
}
return $this;
}
/**
* Get Vertical.
*
* @return null|string
*/
public function getVertical()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getVertical();
}
return $this->vertical;
}
/**
* Set Vertical.
*
* @param string $verticalAlignment see self::VERTICAL_*
*
* @return $this
*/
public function setVertical($verticalAlignment)
{
if ($verticalAlignment == '') {
$verticalAlignment = self::VERTICAL_BOTTOM;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['vertical' => $verticalAlignment]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->vertical = $verticalAlignment;
}
return $this;
}
/**
* Get TextRotation.
*
* @return null|int
*/
public function getTextRotation()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getTextRotation();
}
return $this->textRotation;
}
/**
* Set TextRotation.
*
* @param int $angleInDegrees
*
* @return $this
*/
public function setTextRotation($angleInDegrees)
{
// Excel2007 value 255 => PhpSpreadsheet value -165
if ($angleInDegrees == self::TEXTROTATION_STACK_EXCEL) {
$angleInDegrees = self::TEXTROTATION_STACK_PHPSPREADSHEET;
}
// Set rotation
if (($angleInDegrees >= -90 && $angleInDegrees <= 90) || $angleInDegrees == self::TEXTROTATION_STACK_PHPSPREADSHEET) {
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['textRotation' => $angleInDegrees]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->textRotation = $angleInDegrees;
}
} else {
throw new PhpSpreadsheetException('Text rotation should be a value between -90 and 90.');
}
return $this;
}
/**
* Get Wrap Text.
*
* @return bool
*/
public function getWrapText()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getWrapText();
}
return $this->wrapText;
}
/**
* Set Wrap Text.
*
* @param bool $wrapped
*
* @return $this
*/
public function setWrapText($wrapped)
{
if ($wrapped == '') {
$wrapped = false;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['wrapText' => $wrapped]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->wrapText = $wrapped;
}
return $this;
}
/**
* Get Shrink to fit.
*
* @return bool
*/
public function getShrinkToFit()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getShrinkToFit();
}
return $this->shrinkToFit;
}
/**
* Set Shrink to fit.
*
* @param bool $shrink
*
* @return $this
*/
public function setShrinkToFit($shrink)
{
if ($shrink == '') {
$shrink = false;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['shrinkToFit' => $shrink]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->shrinkToFit = $shrink;
}
return $this;
}
/**
* Get indent.
*
* @return int
*/
public function getIndent()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getIndent();
}
return $this->indent;
}
/**
* Set indent.
*
* @param int $indent
*
* @return $this
*/
public function setIndent($indent)
{
if ($indent > 0) {
if (
$this->getHorizontal() != self::HORIZONTAL_GENERAL &&
$this->getHorizontal() != self::HORIZONTAL_LEFT &&
$this->getHorizontal() != self::HORIZONTAL_RIGHT &&
$this->getHorizontal() != self::HORIZONTAL_DISTRIBUTED
) {
$indent = 0; // indent not supported
}
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['indent' => $indent]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->indent = $indent;
}
return $this;
}
/**
* Get read order.
*
* @return int
*/
public function getReadOrder()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getReadOrder();
}
return $this->readOrder;
}
/**
* Set read order.
*
* @param int $readOrder
*
* @return $this
*/
public function setReadOrder($readOrder)
{
if ($readOrder < 0 || $readOrder > 2) {
$readOrder = 0;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['readOrder' => $readOrder]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->readOrder = $readOrder;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->horizontal .
$this->vertical .
$this->textRotation .
($this->wrapText ? 't' : 'f') .
($this->shrinkToFit ? 't' : 'f') .
$this->indent .
$this->readOrder .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'horizontal', $this->getHorizontal());
$this->exportArray2($exportedArray, 'indent', $this->getIndent());
$this->exportArray2($exportedArray, 'readOrder', $this->getReadOrder());
$this->exportArray2($exportedArray, 'shrinkToFit', $this->getShrinkToFit());
$this->exportArray2($exportedArray, 'textRotation', $this->getTextRotation());
$this->exportArray2($exportedArray, 'vertical', $this->getVertical());
$this->exportArray2($exportedArray, 'wrapText', $this->getWrapText());
return $exportedArray;
}
}

View File

@@ -0,0 +1,234 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
class Border extends Supervisor
{
// Border style
const BORDER_NONE = 'none';
const BORDER_DASHDOT = 'dashDot';
const BORDER_DASHDOTDOT = 'dashDotDot';
const BORDER_DASHED = 'dashed';
const BORDER_DOTTED = 'dotted';
const BORDER_DOUBLE = 'double';
const BORDER_HAIR = 'hair';
const BORDER_MEDIUM = 'medium';
const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
const BORDER_MEDIUMDASHED = 'mediumDashed';
const BORDER_SLANTDASHDOT = 'slantDashDot';
const BORDER_THICK = 'thick';
const BORDER_THIN = 'thin';
/**
* Border style.
*
* @var string
*/
protected $borderStyle = self::BORDER_NONE;
/**
* Border color.
*
* @var Color
*/
protected $color;
/**
* @var null|int
*/
public $colorIndex;
/**
* Create a new Border.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
// bind parent if we are a supervisor
if ($isSupervisor) {
$this->color->bindParent($this, 'color');
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Border
*/
public function getSharedComponent()
{
/** @var Borders $sharedComponent */
$sharedComponent = $this->parent->getSharedComponent();
switch ($this->parentPropertyName) {
case 'bottom':
return $sharedComponent->getBottom();
case 'diagonal':
return $sharedComponent->getDiagonal();
case 'left':
return $sharedComponent->getLeft();
case 'right':
return $sharedComponent->getRight();
case 'top':
return $sharedComponent->getTop();
}
throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.');
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return $this->parent->getStyleArray([$this->parentPropertyName => $array]);
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
* [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['borderStyle'])) {
$this->setBorderStyle($styleArray['borderStyle']);
}
if (isset($styleArray['color'])) {
$this->getColor()->applyFromArray($styleArray['color']);
}
}
return $this;
}
/**
* Get Border style.
*
* @return string
*/
public function getBorderStyle()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getBorderStyle();
}
return $this->borderStyle;
}
/**
* Set Border style.
*
* @param bool|string $style
* When passing a boolean, FALSE equates Border::BORDER_NONE
* and TRUE to Border::BORDER_MEDIUM
*
* @return $this
*/
public function setBorderStyle($style)
{
if (empty($style)) {
$style = self::BORDER_NONE;
} elseif (is_bool($style)) {
$style = self::BORDER_MEDIUM;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['borderStyle' => $style]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->borderStyle = $style;
}
return $this;
}
/**
* Get Border Color.
*
* @return Color
*/
public function getColor()
{
return $this->color;
}
/**
* Set Border Color.
*
* @return $this
*/
public function setColor(Color $color)
{
// make sure parameter is a real color and not a supervisor
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
if ($this->isSupervisor) {
$styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->color = $color;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->borderStyle .
$this->color->getHashCode() .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'borderStyle', $this->getBorderStyle());
$this->exportArray2($exportedArray, 'color', $this->getColor());
return $exportedArray;
}
}

View File

@@ -0,0 +1,421 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
class Borders extends Supervisor
{
// Diagonal directions
const DIAGONAL_NONE = 0;
const DIAGONAL_UP = 1;
const DIAGONAL_DOWN = 2;
const DIAGONAL_BOTH = 3;
/**
* Left.
*
* @var Border
*/
protected $left;
/**
* Right.
*
* @var Border
*/
protected $right;
/**
* Top.
*
* @var Border
*/
protected $top;
/**
* Bottom.
*
* @var Border
*/
protected $bottom;
/**
* Diagonal.
*
* @var Border
*/
protected $diagonal;
/**
* DiagonalDirection.
*
* @var int
*/
protected $diagonalDirection;
/**
* All borders pseudo-border. Only applies to supervisor.
*
* @var Border
*/
protected $allBorders;
/**
* Outline pseudo-border. Only applies to supervisor.
*
* @var Border
*/
protected $outline;
/**
* Inside pseudo-border. Only applies to supervisor.
*
* @var Border
*/
protected $inside;
/**
* Vertical pseudo-border. Only applies to supervisor.
*
* @var Border
*/
protected $vertical;
/**
* Horizontal pseudo-border. Only applies to supervisor.
*
* @var Border
*/
protected $horizontal;
/**
* Create a new Borders.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
$this->left = new Border($isSupervisor);
$this->right = new Border($isSupervisor);
$this->top = new Border($isSupervisor);
$this->bottom = new Border($isSupervisor);
$this->diagonal = new Border($isSupervisor);
$this->diagonalDirection = self::DIAGONAL_NONE;
// Specially for supervisor
if ($isSupervisor) {
// Initialize pseudo-borders
$this->allBorders = new Border(true);
$this->outline = new Border(true);
$this->inside = new Border(true);
$this->vertical = new Border(true);
$this->horizontal = new Border(true);
// bind parent if we are a supervisor
$this->left->bindParent($this, 'left');
$this->right->bindParent($this, 'right');
$this->top->bindParent($this, 'top');
$this->bottom->bindParent($this, 'bottom');
$this->diagonal->bindParent($this, 'diagonal');
$this->allBorders->bindParent($this, 'allBorders');
$this->outline->bindParent($this, 'outline');
$this->inside->bindParent($this, 'inside');
$this->vertical->bindParent($this, 'vertical');
$this->horizontal->bindParent($this, 'horizontal');
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Borders
*/
public function getSharedComponent()
{
return $this->parent->getSharedComponent()->getBorders();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['borders' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
* [
* 'bottom' => [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ],
* 'top' => [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ]
* ]
* );
* </code>
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
* [
* 'allBorders' => [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ]
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['left'])) {
$this->getLeft()->applyFromArray($styleArray['left']);
}
if (isset($styleArray['right'])) {
$this->getRight()->applyFromArray($styleArray['right']);
}
if (isset($styleArray['top'])) {
$this->getTop()->applyFromArray($styleArray['top']);
}
if (isset($styleArray['bottom'])) {
$this->getBottom()->applyFromArray($styleArray['bottom']);
}
if (isset($styleArray['diagonal'])) {
$this->getDiagonal()->applyFromArray($styleArray['diagonal']);
}
if (isset($styleArray['diagonalDirection'])) {
$this->setDiagonalDirection($styleArray['diagonalDirection']);
}
if (isset($styleArray['allBorders'])) {
$this->getLeft()->applyFromArray($styleArray['allBorders']);
$this->getRight()->applyFromArray($styleArray['allBorders']);
$this->getTop()->applyFromArray($styleArray['allBorders']);
$this->getBottom()->applyFromArray($styleArray['allBorders']);
}
}
return $this;
}
/**
* Get Left.
*
* @return Border
*/
public function getLeft()
{
return $this->left;
}
/**
* Get Right.
*
* @return Border
*/
public function getRight()
{
return $this->right;
}
/**
* Get Top.
*
* @return Border
*/
public function getTop()
{
return $this->top;
}
/**
* Get Bottom.
*
* @return Border
*/
public function getBottom()
{
return $this->bottom;
}
/**
* Get Diagonal.
*
* @return Border
*/
public function getDiagonal()
{
return $this->diagonal;
}
/**
* Get AllBorders (pseudo-border). Only applies to supervisor.
*
* @return Border
*/
public function getAllBorders()
{
if (!$this->isSupervisor) {
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
}
return $this->allBorders;
}
/**
* Get Outline (pseudo-border). Only applies to supervisor.
*
* @return Border
*/
public function getOutline()
{
if (!$this->isSupervisor) {
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
}
return $this->outline;
}
/**
* Get Inside (pseudo-border). Only applies to supervisor.
*
* @return Border
*/
public function getInside()
{
if (!$this->isSupervisor) {
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
}
return $this->inside;
}
/**
* Get Vertical (pseudo-border). Only applies to supervisor.
*
* @return Border
*/
public function getVertical()
{
if (!$this->isSupervisor) {
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
}
return $this->vertical;
}
/**
* Get Horizontal (pseudo-border). Only applies to supervisor.
*
* @return Border
*/
public function getHorizontal()
{
if (!$this->isSupervisor) {
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
}
return $this->horizontal;
}
/**
* Get DiagonalDirection.
*
* @return int
*/
public function getDiagonalDirection()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getDiagonalDirection();
}
return $this->diagonalDirection;
}
/**
* Set DiagonalDirection.
*
* @param int $direction see self::DIAGONAL_*
*
* @return $this
*/
public function setDiagonalDirection($direction)
{
if ($direction == '') {
$direction = self::DIAGONAL_NONE;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['diagonalDirection' => $direction]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->diagonalDirection = $direction;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashcode();
}
return md5(
$this->getLeft()->getHashCode() .
$this->getRight()->getHashCode() .
$this->getTop()->getHashCode() .
$this->getBottom()->getHashCode() .
$this->getDiagonal()->getHashCode() .
$this->getDiagonalDirection() .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'bottom', $this->getBottom());
$this->exportArray2($exportedArray, 'diagonal', $this->getDiagonal());
$this->exportArray2($exportedArray, 'diagonalDirection', $this->getDiagonalDirection());
$this->exportArray2($exportedArray, 'left', $this->getLeft());
$this->exportArray2($exportedArray, 'right', $this->getRight());
$this->exportArray2($exportedArray, 'top', $this->getTop());
return $exportedArray;
}
}

View File

@@ -0,0 +1,413 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
class Color extends Supervisor
{
const NAMED_COLORS = [
'Black',
'White',
'Red',
'Green',
'Blue',
'Yellow',
'Magenta',
'Cyan',
];
// Colors
const COLOR_BLACK = 'FF000000';
const COLOR_WHITE = 'FFFFFFFF';
const COLOR_RED = 'FFFF0000';
const COLOR_DARKRED = 'FF800000';
const COLOR_BLUE = 'FF0000FF';
const COLOR_DARKBLUE = 'FF000080';
const COLOR_GREEN = 'FF00FF00';
const COLOR_DARKGREEN = 'FF008000';
const COLOR_YELLOW = 'FFFFFF00';
const COLOR_DARKYELLOW = 'FF808000';
const VALIDATE_ARGB_SIZE = 8;
const VALIDATE_RGB_SIZE = 6;
const VALIDATE_COLOR_VALUE = '/^[A-F0-9]{%d}$/i';
/**
* Indexed colors array.
*
* @var array
*/
protected static $indexedColors;
/**
* ARGB - Alpha RGB.
*
* @var null|string
*/
protected $argb;
/**
* Create a new Color.
*
* @param string $colorValue ARGB value for the colour, or named colour
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($colorValue = self::COLOR_BLACK, $isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
if (!$isConditional) {
$this->argb = $this->validateColor($colorValue, self::VALIDATE_ARGB_SIZE) ? $colorValue : self::COLOR_BLACK;
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Color
*/
public function getSharedComponent()
{
/** @var Border|Fill $sharedComponent */
$sharedComponent = $this->parent->getSharedComponent();
if ($this->parentPropertyName === 'endColor') {
return $sharedComponent->getEndColor();
}
if ($this->parentPropertyName === 'startColor') {
return $sharedComponent->getStartColor();
}
return $sharedComponent->getColor();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return $this->parent->getStyleArray([$this->parentPropertyName => $array]);
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray(['rgb' => '808080']);
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['rgb'])) {
$this->setRGB($styleArray['rgb']);
}
if (isset($styleArray['argb'])) {
$this->setARGB($styleArray['argb']);
}
}
return $this;
}
private function validateColor(string $colorValue, int $size): bool
{
return in_array(ucfirst(strtolower($colorValue)), self::NAMED_COLORS) ||
preg_match(sprintf(self::VALIDATE_COLOR_VALUE, $size), $colorValue);
}
/**
* Get ARGB.
*/
public function getARGB(): ?string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getARGB();
}
return $this->argb;
}
/**
* Set ARGB.
*
* @param string $colorValue ARGB value, or a named color
*
* @return $this
*/
public function setARGB(?string $colorValue = self::COLOR_BLACK)
{
if ($colorValue === '' || $colorValue === null) {
$colorValue = self::COLOR_BLACK;
} elseif (!$this->validateColor($colorValue, self::VALIDATE_ARGB_SIZE)) {
return $this;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['argb' => $colorValue]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->argb = $colorValue;
}
return $this;
}
/**
* Get RGB.
*/
public function getRGB(): string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getRGB();
}
return substr($this->argb ?? '', 2);
}
/**
* Set RGB.
*
* @param string $colorValue RGB value, or a named color
*
* @return $this
*/
public function setRGB(?string $colorValue = self::COLOR_BLACK)
{
if ($colorValue === '' || $colorValue === null) {
$colorValue = '000000';
} elseif (!$this->validateColor($colorValue, self::VALIDATE_RGB_SIZE)) {
return $this;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['argb' => 'FF' . $colorValue]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->argb = 'FF' . $colorValue;
}
return $this;
}
/**
* Get a specified colour component of an RGB value.
*
* @param string $rgbValue The colour as an RGB value (e.g. FF00CCCC or CCDDEE
* @param int $offset Position within the RGB value to extract
* @param bool $hex Flag indicating whether the component should be returned as a hex or a
* decimal value
*
* @return int|string The extracted colour component
*/
private static function getColourComponent($rgbValue, $offset, $hex = true)
{
$colour = substr($rgbValue, $offset, 2);
return ($hex) ? $colour : hexdec($colour);
}
/**
* Get the red colour component of an RGB value.
*
* @param string $rgbValue The colour as an RGB value (e.g. FF00CCCC or CCDDEE
* @param bool $hex Flag indicating whether the component should be returned as a hex or a
* decimal value
*
* @return int|string The red colour component
*/
public static function getRed($rgbValue, $hex = true)
{
return self::getColourComponent($rgbValue, strlen($rgbValue) - 6, $hex);
}
/**
* Get the green colour component of an RGB value.
*
* @param string $rgbValue The colour as an RGB value (e.g. FF00CCCC or CCDDEE
* @param bool $hex Flag indicating whether the component should be returned as a hex or a
* decimal value
*
* @return int|string The green colour component
*/
public static function getGreen($rgbValue, $hex = true)
{
return self::getColourComponent($rgbValue, strlen($rgbValue) - 4, $hex);
}
/**
* Get the blue colour component of an RGB value.
*
* @param string $rgbValue The colour as an RGB value (e.g. FF00CCCC or CCDDEE
* @param bool $hex Flag indicating whether the component should be returned as a hex or a
* decimal value
*
* @return int|string The blue colour component
*/
public static function getBlue($rgbValue, $hex = true)
{
return self::getColourComponent($rgbValue, strlen($rgbValue) - 2, $hex);
}
/**
* Adjust the brightness of a color.
*
* @param string $hexColourValue The colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
* @param float $adjustPercentage The percentage by which to adjust the colour as a float from -1 to 1
*
* @return string The adjusted colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
*/
public static function changeBrightness($hexColourValue, $adjustPercentage)
{
$rgba = (strlen($hexColourValue) === 8);
$adjustPercentage = max(-1.0, min(1.0, $adjustPercentage));
/** @var int $red */
$red = self::getRed($hexColourValue, false);
/** @var int $green */
$green = self::getGreen($hexColourValue, false);
/** @var int $blue */
$blue = self::getBlue($hexColourValue, false);
if ($adjustPercentage > 0) {
$red += (255 - $red) * $adjustPercentage;
$green += (255 - $green) * $adjustPercentage;
$blue += (255 - $blue) * $adjustPercentage;
} else {
$red += $red * $adjustPercentage;
$green += $green * $adjustPercentage;
$blue += $blue * $adjustPercentage;
}
$rgb = strtoupper(
str_pad(dechex((int) $red), 2, '0', 0) .
str_pad(dechex((int) $green), 2, '0', 0) .
str_pad(dechex((int) $blue), 2, '0', 0)
);
return (($rgba) ? 'FF' : '') . $rgb;
}
/**
* Get indexed color.
*
* @param int $colorIndex Index entry point into the colour array
* @param bool $background Flag to indicate whether default background or foreground colour
* should be returned if the indexed colour doesn't exist
*
* @return Color
*/
public static function indexedColor($colorIndex, $background = false): self
{
// Clean parameter
$colorIndex = (int) $colorIndex;
// Indexed colors
if (self::$indexedColors === null) {
self::$indexedColors = [
1 => 'FF000000', // System Colour #1 - Black
2 => 'FFFFFFFF', // System Colour #2 - White
3 => 'FFFF0000', // System Colour #3 - Red
4 => 'FF00FF00', // System Colour #4 - Green
5 => 'FF0000FF', // System Colour #5 - Blue
6 => 'FFFFFF00', // System Colour #6 - Yellow
7 => 'FFFF00FF', // System Colour #7- Magenta
8 => 'FF00FFFF', // System Colour #8- Cyan
9 => 'FF800000', // Standard Colour #9
10 => 'FF008000', // Standard Colour #10
11 => 'FF000080', // Standard Colour #11
12 => 'FF808000', // Standard Colour #12
13 => 'FF800080', // Standard Colour #13
14 => 'FF008080', // Standard Colour #14
15 => 'FFC0C0C0', // Standard Colour #15
16 => 'FF808080', // Standard Colour #16
17 => 'FF9999FF', // Chart Fill Colour #17
18 => 'FF993366', // Chart Fill Colour #18
19 => 'FFFFFFCC', // Chart Fill Colour #19
20 => 'FFCCFFFF', // Chart Fill Colour #20
21 => 'FF660066', // Chart Fill Colour #21
22 => 'FFFF8080', // Chart Fill Colour #22
23 => 'FF0066CC', // Chart Fill Colour #23
24 => 'FFCCCCFF', // Chart Fill Colour #24
25 => 'FF000080', // Chart Line Colour #25
26 => 'FFFF00FF', // Chart Line Colour #26
27 => 'FFFFFF00', // Chart Line Colour #27
28 => 'FF00FFFF', // Chart Line Colour #28
29 => 'FF800080', // Chart Line Colour #29
30 => 'FF800000', // Chart Line Colour #30
31 => 'FF008080', // Chart Line Colour #31
32 => 'FF0000FF', // Chart Line Colour #32
33 => 'FF00CCFF', // Standard Colour #33
34 => 'FFCCFFFF', // Standard Colour #34
35 => 'FFCCFFCC', // Standard Colour #35
36 => 'FFFFFF99', // Standard Colour #36
37 => 'FF99CCFF', // Standard Colour #37
38 => 'FFFF99CC', // Standard Colour #38
39 => 'FFCC99FF', // Standard Colour #39
40 => 'FFFFCC99', // Standard Colour #40
41 => 'FF3366FF', // Standard Colour #41
42 => 'FF33CCCC', // Standard Colour #42
43 => 'FF99CC00', // Standard Colour #43
44 => 'FFFFCC00', // Standard Colour #44
45 => 'FFFF9900', // Standard Colour #45
46 => 'FFFF6600', // Standard Colour #46
47 => 'FF666699', // Standard Colour #47
48 => 'FF969696', // Standard Colour #48
49 => 'FF003366', // Standard Colour #49
50 => 'FF339966', // Standard Colour #50
51 => 'FF003300', // Standard Colour #51
52 => 'FF333300', // Standard Colour #52
53 => 'FF993300', // Standard Colour #53
54 => 'FF993366', // Standard Colour #54
55 => 'FF333399', // Standard Colour #55
56 => 'FF333333', // Standard Colour #56
];
}
if (isset(self::$indexedColors[$colorIndex])) {
return new self(self::$indexedColors[$colorIndex]);
}
return ($background) ? new self(self::COLOR_WHITE) : new self(self::COLOR_BLACK);
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode(): string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->argb .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'argb', $this->getARGB());
return $exportedArray;
}
}

View File

@@ -0,0 +1,323 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\IComparable;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalDataBar;
class Conditional implements IComparable
{
// Condition types
const CONDITION_NONE = 'none';
const CONDITION_CELLIS = 'cellIs';
const CONDITION_CONTAINSTEXT = 'containsText';
const CONDITION_EXPRESSION = 'expression';
const CONDITION_CONTAINSBLANKS = 'containsBlanks';
const CONDITION_NOTCONTAINSBLANKS = 'notContainsBlanks';
const CONDITION_DATABAR = 'dataBar';
const CONDITION_NOTCONTAINSTEXT = 'notContainsText';
private const CONDITION_TYPES = [
self::CONDITION_CELLIS,
self::CONDITION_CONTAINSBLANKS,
self::CONDITION_CONTAINSTEXT,
self::CONDITION_DATABAR,
self::CONDITION_EXPRESSION,
self::CONDITION_NONE,
self::CONDITION_NOTCONTAINSBLANKS,
self::CONDITION_NOTCONTAINSTEXT,
];
// Operator types
const OPERATOR_NONE = '';
const OPERATOR_BEGINSWITH = 'beginsWith';
const OPERATOR_ENDSWITH = 'endsWith';
const OPERATOR_EQUAL = 'equal';
const OPERATOR_GREATERTHAN = 'greaterThan';
const OPERATOR_GREATERTHANOREQUAL = 'greaterThanOrEqual';
const OPERATOR_LESSTHAN = 'lessThan';
const OPERATOR_LESSTHANOREQUAL = 'lessThanOrEqual';
const OPERATOR_NOTEQUAL = 'notEqual';
const OPERATOR_CONTAINSTEXT = 'containsText';
const OPERATOR_NOTCONTAINS = 'notContains';
const OPERATOR_BETWEEN = 'between';
const OPERATOR_NOTBETWEEN = 'notBetween';
/**
* Condition type.
*
* @var string
*/
private $conditionType = self::CONDITION_NONE;
/**
* Operator type.
*
* @var string
*/
private $operatorType = self::OPERATOR_NONE;
/**
* Text.
*
* @var string
*/
private $text;
/**
* Stop on this condition, if it matches.
*
* @var bool
*/
private $stopIfTrue = false;
/**
* Condition.
*
* @var string[]
*/
private $condition = [];
/**
* @var ConditionalDataBar
*/
private $dataBar;
/**
* Style.
*
* @var Style
*/
private $style;
/**
* Create a new Conditional.
*/
public function __construct()
{
// Initialise values
$this->style = new Style(false, true);
}
/**
* Get Condition type.
*
* @return string
*/
public function getConditionType()
{
return $this->conditionType;
}
/**
* Set Condition type.
*
* @param string $type Condition type, see self::CONDITION_*
*
* @return $this
*/
public function setConditionType($type)
{
$this->conditionType = $type;
return $this;
}
/**
* Get Operator type.
*
* @return string
*/
public function getOperatorType()
{
return $this->operatorType;
}
/**
* Set Operator type.
*
* @param string $type Conditional operator type, see self::OPERATOR_*
*
* @return $this
*/
public function setOperatorType($type)
{
$this->operatorType = $type;
return $this;
}
/**
* Get text.
*
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* Set text.
*
* @param string $text
*
* @return $this
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* Get StopIfTrue.
*
* @return bool
*/
public function getStopIfTrue()
{
return $this->stopIfTrue;
}
/**
* Set StopIfTrue.
*
* @param bool $stopIfTrue
*
* @return $this
*/
public function setStopIfTrue($stopIfTrue)
{
$this->stopIfTrue = $stopIfTrue;
return $this;
}
/**
* Get Conditions.
*
* @return string[]
*/
public function getConditions()
{
return $this->condition;
}
/**
* Set Conditions.
*
* @param bool|float|int|string|string[] $conditions Condition
*
* @return $this
*/
public function setConditions($conditions)
{
if (!is_array($conditions)) {
$conditions = [$conditions];
}
$this->condition = $conditions;
return $this;
}
/**
* Add Condition.
*
* @param string $condition Condition
*
* @return $this
*/
public function addCondition($condition)
{
$this->condition[] = $condition;
return $this;
}
/**
* Get Style.
*
* @return Style
*/
public function getStyle()
{
return $this->style;
}
/**
* Set Style.
*
* @param Style $style
*
* @return $this
*/
public function setStyle(?Style $style = null)
{
$this->style = $style;
return $this;
}
/**
* get DataBar.
*
* @return null|ConditionalDataBar
*/
public function getDataBar()
{
return $this->dataBar;
}
/**
* set DataBar.
*
* @return $this
*/
public function setDataBar(ConditionalDataBar $dataBar)
{
$this->dataBar = $dataBar;
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
return md5(
$this->conditionType .
$this->operatorType .
implode(';', $this->condition) .
$this->style->getHashCode() .
__CLASS__
);
}
/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$vars = get_object_vars($this);
foreach ($vars as $key => $value) {
if (is_object($value)) {
$this->$key = clone $value;
} else {
$this->$key = $value;
}
}
}
/**
* Verify if param is valid condition type.
*/
public static function isValidConditionType(string $type): bool
{
return in_array($type, self::CONDITION_TYPES);
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
class ConditionalDataBar
{
/** <dataBar> attribute */
/** @var null|bool */
private $showValue;
/** <dataBar> children */
/** @var ConditionalFormatValueObject */
private $minimumConditionalFormatValueObject;
/** @var ConditionalFormatValueObject */
private $maximumConditionalFormatValueObject;
/** @var string */
private $color;
/** <extLst> */
/** @var ConditionalFormattingRuleExtension */
private $conditionalFormattingRuleExt;
/**
* @return null|bool
*/
public function getShowValue()
{
return $this->showValue;
}
/**
* @param bool $showValue
*/
public function setShowValue($showValue)
{
$this->showValue = $showValue;
return $this;
}
/**
* @return ConditionalFormatValueObject
*/
public function getMinimumConditionalFormatValueObject()
{
return $this->minimumConditionalFormatValueObject;
}
public function setMinimumConditionalFormatValueObject(ConditionalFormatValueObject $minimumConditionalFormatValueObject)
{
$this->minimumConditionalFormatValueObject = $minimumConditionalFormatValueObject;
return $this;
}
/**
* @return ConditionalFormatValueObject
*/
public function getMaximumConditionalFormatValueObject()
{
return $this->maximumConditionalFormatValueObject;
}
public function setMaximumConditionalFormatValueObject(ConditionalFormatValueObject $maximumConditionalFormatValueObject)
{
$this->maximumConditionalFormatValueObject = $maximumConditionalFormatValueObject;
return $this;
}
public function getColor(): string
{
return $this->color;
}
public function setColor(string $color): self
{
$this->color = $color;
return $this;
}
/**
* @return ConditionalFormattingRuleExtension
*/
public function getConditionalFormattingRuleExt()
{
return $this->conditionalFormattingRuleExt;
}
public function setConditionalFormattingRuleExt(ConditionalFormattingRuleExtension $conditionalFormattingRuleExt)
{
$this->conditionalFormattingRuleExt = $conditionalFormattingRuleExt;
return $this;
}
}

View File

@@ -0,0 +1,290 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
class ConditionalDataBarExtension
{
/** <dataBar> attributes */
/** @var int */
private $minLength;
/** @var int */
private $maxLength;
/** @var null|bool */
private $border;
/** @var null|bool */
private $gradient;
/** @var string */
private $direction;
/** @var null|bool */
private $negativeBarBorderColorSameAsPositive;
/** @var string */
private $axisPosition;
// <dataBar> children
/** @var ConditionalFormatValueObject */
private $maximumConditionalFormatValueObject;
/** @var ConditionalFormatValueObject */
private $minimumConditionalFormatValueObject;
/** @var string */
private $borderColor;
/** @var string */
private $negativeFillColor;
/** @var string */
private $negativeBorderColor;
/** @var array */
private $axisColor = [
'rgb' => null,
'theme' => null,
'tint' => null,
];
public function getXmlAttributes()
{
$ret = [];
foreach (['minLength', 'maxLength', 'direction', 'axisPosition'] as $attrKey) {
if (null !== $this->{$attrKey}) {
$ret[$attrKey] = $this->{$attrKey};
}
}
foreach (['border', 'gradient', 'negativeBarBorderColorSameAsPositive'] as $attrKey) {
if (null !== $this->{$attrKey}) {
$ret[$attrKey] = $this->{$attrKey} ? '1' : '0';
}
}
return $ret;
}
public function getXmlElements()
{
$ret = [];
$elms = ['borderColor', 'negativeFillColor', 'negativeBorderColor'];
foreach ($elms as $elmKey) {
if (null !== $this->{$elmKey}) {
$ret[$elmKey] = ['rgb' => $this->{$elmKey}];
}
}
foreach (array_filter($this->axisColor) as $attrKey => $axisColorAttr) {
if (!isset($ret['axisColor'])) {
$ret['axisColor'] = [];
}
$ret['axisColor'][$attrKey] = $axisColorAttr;
}
return $ret;
}
/**
* @return int
*/
public function getMinLength()
{
return $this->minLength;
}
public function setMinLength(int $minLength): self
{
$this->minLength = $minLength;
return $this;
}
/**
* @return int
*/
public function getMaxLength()
{
return $this->maxLength;
}
public function setMaxLength(int $maxLength): self
{
$this->maxLength = $maxLength;
return $this;
}
/**
* @return null|bool
*/
public function getBorder()
{
return $this->border;
}
public function setBorder(bool $border): self
{
$this->border = $border;
return $this;
}
/**
* @return null|bool
*/
public function getGradient()
{
return $this->gradient;
}
public function setGradient(bool $gradient): self
{
$this->gradient = $gradient;
return $this;
}
/**
* @return string
*/
public function getDirection()
{
return $this->direction;
}
public function setDirection(string $direction): self
{
$this->direction = $direction;
return $this;
}
/**
* @return null|bool
*/
public function getNegativeBarBorderColorSameAsPositive()
{
return $this->negativeBarBorderColorSameAsPositive;
}
public function setNegativeBarBorderColorSameAsPositive(bool $negativeBarBorderColorSameAsPositive): self
{
$this->negativeBarBorderColorSameAsPositive = $negativeBarBorderColorSameAsPositive;
return $this;
}
/**
* @return string
*/
public function getAxisPosition()
{
return $this->axisPosition;
}
public function setAxisPosition(string $axisPosition): self
{
$this->axisPosition = $axisPosition;
return $this;
}
/**
* @return ConditionalFormatValueObject
*/
public function getMaximumConditionalFormatValueObject()
{
return $this->maximumConditionalFormatValueObject;
}
public function setMaximumConditionalFormatValueObject(ConditionalFormatValueObject $maximumConditionalFormatValueObject)
{
$this->maximumConditionalFormatValueObject = $maximumConditionalFormatValueObject;
return $this;
}
/**
* @return ConditionalFormatValueObject
*/
public function getMinimumConditionalFormatValueObject()
{
return $this->minimumConditionalFormatValueObject;
}
public function setMinimumConditionalFormatValueObject(ConditionalFormatValueObject $minimumConditionalFormatValueObject)
{
$this->minimumConditionalFormatValueObject = $minimumConditionalFormatValueObject;
return $this;
}
/**
* @return string
*/
public function getBorderColor()
{
return $this->borderColor;
}
public function setBorderColor(string $borderColor): self
{
$this->borderColor = $borderColor;
return $this;
}
/**
* @return string
*/
public function getNegativeFillColor()
{
return $this->negativeFillColor;
}
public function setNegativeFillColor(string $negativeFillColor): self
{
$this->negativeFillColor = $negativeFillColor;
return $this;
}
/**
* @return string
*/
public function getNegativeBorderColor()
{
return $this->negativeBorderColor;
}
public function setNegativeBorderColor(string $negativeBorderColor): self
{
$this->negativeBorderColor = $negativeBorderColor;
return $this;
}
public function getAxisColor(): array
{
return $this->axisColor;
}
/**
* @param mixed $rgb
* @param null|mixed $theme
* @param null|mixed $tint
*/
public function setAxisColor($rgb, $theme = null, $tint = null): self
{
$this->axisColor = [
'rgb' => $rgb,
'theme' => $theme,
'tint' => $tint,
];
return $this;
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
class ConditionalFormatValueObject
{
private $type;
private $value;
private $cellFormula;
/**
* ConditionalFormatValueObject constructor.
*
* @param null|mixed $cellFormula
*/
public function __construct($type, $value = null, $cellFormula = null)
{
$this->type = $type;
$this->value = $value;
$this->cellFormula = $cellFormula;
}
/**
* @return mixed
*/
public function getType()
{
return $this->type;
}
/**
* @param mixed $type
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* @param mixed $value
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* @return mixed
*/
public function getCellFormula()
{
return $this->cellFormula;
}
/**
* @param mixed $cellFormula
*/
public function setCellFormula($cellFormula)
{
$this->cellFormula = $cellFormula;
return $this;
}
}

View File

@@ -0,0 +1,208 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use SimpleXMLElement;
class ConditionalFormattingRuleExtension
{
const CONDITION_EXTENSION_DATABAR = 'dataBar';
/** <conditionalFormatting> attributes */
private $id;
/** @var string Conditional Formatting Rule */
private $cfRule;
/** <conditionalFormatting> children */
/** @var ConditionalDataBarExtension */
private $dataBar;
/** @var string Sequence of References */
private $sqref;
/**
* ConditionalFormattingRuleExtension constructor.
*/
public function __construct($id = null, string $cfRule = self::CONDITION_EXTENSION_DATABAR)
{
if (null === $id) {
$this->id = '{' . $this->generateUuid() . '}';
} else {
$this->id = $id;
}
$this->cfRule = $cfRule;
}
private function generateUuid()
{
$chars = str_split('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx');
foreach ($chars as $i => $char) {
if ($char === 'x') {
$chars[$i] = dechex(random_int(0, 15));
} elseif ($char === 'y') {
$chars[$i] = dechex(random_int(8, 11));
}
}
return implode('', $chars);
}
public static function parseExtLstXml($extLstXml)
{
$conditionalFormattingRuleExtensions = [];
$conditionalFormattingRuleExtensionXml = null;
if ($extLstXml instanceof SimpleXMLElement) {
foreach ((count($extLstXml) > 0 ? $extLstXml : [$extLstXml]) as $extLst) {
//this uri is conditionalFormattings
//https://docs.microsoft.com/en-us/openspecs/office_standards/ms-xlsx/07d607af-5618-4ca2-b683-6a78dc0d9627
if (isset($extLst->ext['uri']) && (string) $extLst->ext['uri'] === '{78C0D931-6437-407d-A8EE-F0AAD7539E65}') {
$conditionalFormattingRuleExtensionXml = $extLst->ext;
}
}
if ($conditionalFormattingRuleExtensionXml) {
$ns = $conditionalFormattingRuleExtensionXml->getNamespaces(true);
$extFormattingsXml = $conditionalFormattingRuleExtensionXml->children($ns['x14']);
foreach ($extFormattingsXml->children($ns['x14']) as $extFormattingXml) {
$extCfRuleXml = $extFormattingXml->cfRule;
$attributes = $extCfRuleXml->attributes();
if (!$attributes || ((string) $attributes->type) !== Conditional::CONDITION_DATABAR) {
continue;
}
$extFormattingRuleObj = new self((string) $attributes->id);
$extFormattingRuleObj->setSqref((string) $extFormattingXml->children($ns['xm'])->sqref);
$conditionalFormattingRuleExtensions[$extFormattingRuleObj->getId()] = $extFormattingRuleObj;
$extDataBarObj = new ConditionalDataBarExtension();
$extFormattingRuleObj->setDataBarExt($extDataBarObj);
$dataBarXml = $extCfRuleXml->dataBar;
self::parseExtDataBarAttributesFromXml($extDataBarObj, $dataBarXml);
self::parseExtDataBarElementChildrenFromXml($extDataBarObj, $dataBarXml, $ns);
}
}
}
return $conditionalFormattingRuleExtensions;
}
private static function parseExtDataBarAttributesFromXml(
ConditionalDataBarExtension $extDataBarObj,
SimpleXMLElement $dataBarXml
): void {
$dataBarAttribute = $dataBarXml->attributes();
if ($dataBarAttribute->minLength) {
$extDataBarObj->setMinLength((int) $dataBarAttribute->minLength);
}
if ($dataBarAttribute->maxLength) {
$extDataBarObj->setMaxLength((int) $dataBarAttribute->maxLength);
}
if ($dataBarAttribute->border) {
$extDataBarObj->setBorder((bool) (string) $dataBarAttribute->border);
}
if ($dataBarAttribute->gradient) {
$extDataBarObj->setGradient((bool) (string) $dataBarAttribute->gradient);
}
if ($dataBarAttribute->direction) {
$extDataBarObj->setDirection((string) $dataBarAttribute->direction);
}
if ($dataBarAttribute->negativeBarBorderColorSameAsPositive) {
$extDataBarObj->setNegativeBarBorderColorSameAsPositive((bool) (string) $dataBarAttribute->negativeBarBorderColorSameAsPositive);
}
if ($dataBarAttribute->axisPosition) {
$extDataBarObj->setAxisPosition((string) $dataBarAttribute->axisPosition);
}
}
private static function parseExtDataBarElementChildrenFromXml(ConditionalDataBarExtension $extDataBarObj, SimpleXMLElement $dataBarXml, $ns): void
{
if ($dataBarXml->borderColor) {
$extDataBarObj->setBorderColor((string) $dataBarXml->borderColor->attributes()['rgb']);
}
if ($dataBarXml->negativeFillColor) {
$extDataBarObj->setNegativeFillColor((string) $dataBarXml->negativeFillColor->attributes()['rgb']);
}
if ($dataBarXml->negativeBorderColor) {
$extDataBarObj->setNegativeBorderColor((string) $dataBarXml->negativeBorderColor->attributes()['rgb']);
}
if ($dataBarXml->axisColor) {
$axisColorAttr = $dataBarXml->axisColor->attributes();
$extDataBarObj->setAxisColor((string) $axisColorAttr['rgb'], (string) $axisColorAttr['theme'], (string) $axisColorAttr['tint']);
}
$cfvoIndex = 0;
foreach ($dataBarXml->cfvo as $cfvo) {
$f = (string) $cfvo->children($ns['xm'])->f;
$attributes = $cfvo->attributes();
if (!($attributes)) {
continue;
}
if ($cfvoIndex === 0) {
$extDataBarObj->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $attributes['type'], null, (empty($f) ? null : $f)));
}
if ($cfvoIndex === 1) {
$extDataBarObj->setMaximumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $attributes['type'], null, (empty($f) ? null : $f)));
}
++$cfvoIndex;
}
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id): self
{
$this->id = $id;
return $this;
}
public function getCfRule(): string
{
return $this->cfRule;
}
public function setCfRule(string $cfRule): self
{
$this->cfRule = $cfRule;
return $this;
}
public function getDataBarExt(): ConditionalDataBarExtension
{
return $this->dataBar;
}
public function setDataBarExt(ConditionalDataBarExtension $dataBar): self
{
$this->dataBar = $dataBar;
return $this;
}
public function getSqref(): string
{
return $this->sqref;
}
public function setSqref(string $sqref): self
{
$this->sqref = $sqref;
return $this;
}
}

View File

@@ -0,0 +1,325 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
class Fill extends Supervisor
{
// Fill types
const FILL_NONE = 'none';
const FILL_SOLID = 'solid';
const FILL_GRADIENT_LINEAR = 'linear';
const FILL_GRADIENT_PATH = 'path';
const FILL_PATTERN_DARKDOWN = 'darkDown';
const FILL_PATTERN_DARKGRAY = 'darkGray';
const FILL_PATTERN_DARKGRID = 'darkGrid';
const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal';
const FILL_PATTERN_DARKTRELLIS = 'darkTrellis';
const FILL_PATTERN_DARKUP = 'darkUp';
const FILL_PATTERN_DARKVERTICAL = 'darkVertical';
const FILL_PATTERN_GRAY0625 = 'gray0625';
const FILL_PATTERN_GRAY125 = 'gray125';
const FILL_PATTERN_LIGHTDOWN = 'lightDown';
const FILL_PATTERN_LIGHTGRAY = 'lightGray';
const FILL_PATTERN_LIGHTGRID = 'lightGrid';
const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal';
const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis';
const FILL_PATTERN_LIGHTUP = 'lightUp';
const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical';
const FILL_PATTERN_MEDIUMGRAY = 'mediumGray';
/**
* @var null|int
*/
public $startcolorIndex;
/**
* @var null|int
*/
public $endcolorIndex;
/**
* Fill type.
*
* @var null|string
*/
protected $fillType = self::FILL_NONE;
/**
* Rotation.
*
* @var float
*/
protected $rotation = 0;
/**
* Start color.
*
* @var Color
*/
protected $startColor;
/**
* End color.
*
* @var Color
*/
protected $endColor;
/**
* Create a new Fill.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
if ($isConditional) {
$this->fillType = null;
}
$this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional);
$this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
// bind parent if we are a supervisor
if ($isSupervisor) {
$this->startColor->bindParent($this, 'startColor');
$this->endColor->bindParent($this, 'endColor');
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Fill
*/
public function getSharedComponent()
{
return $this->parent->getSharedComponent()->getFill();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['fill' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
* [
* 'fillType' => Fill::FILL_GRADIENT_LINEAR,
* 'rotation' => 0,
* 'startColor' => [
* 'rgb' => '000000'
* ],
* 'endColor' => [
* 'argb' => 'FFFFFFFF'
* ]
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['fillType'])) {
$this->setFillType($styleArray['fillType']);
}
if (isset($styleArray['rotation'])) {
$this->setRotation($styleArray['rotation']);
}
if (isset($styleArray['startColor'])) {
$this->getStartColor()->applyFromArray($styleArray['startColor']);
}
if (isset($styleArray['endColor'])) {
$this->getEndColor()->applyFromArray($styleArray['endColor']);
}
if (isset($styleArray['color'])) {
$this->getStartColor()->applyFromArray($styleArray['color']);
$this->getEndColor()->applyFromArray($styleArray['color']);
}
}
return $this;
}
/**
* Get Fill Type.
*
* @return null|string
*/
public function getFillType()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getFillType();
}
return $this->fillType;
}
/**
* Set Fill Type.
*
* @param string $fillType Fill type, see self::FILL_*
*
* @return $this
*/
public function setFillType($fillType)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['fillType' => $fillType]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->fillType = $fillType;
}
return $this;
}
/**
* Get Rotation.
*
* @return float
*/
public function getRotation()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getRotation();
}
return $this->rotation;
}
/**
* Set Rotation.
*
* @param float $angleInDegrees
*
* @return $this
*/
public function setRotation($angleInDegrees)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['rotation' => $angleInDegrees]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->rotation = $angleInDegrees;
}
return $this;
}
/**
* Get Start Color.
*
* @return Color
*/
public function getStartColor()
{
return $this->startColor;
}
/**
* Set Start Color.
*
* @return $this
*/
public function setStartColor(Color $color)
{
// make sure parameter is a real color and not a supervisor
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
if ($this->isSupervisor) {
$styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->startColor = $color;
}
return $this;
}
/**
* Get End Color.
*
* @return Color
*/
public function getEndColor()
{
return $this->endColor;
}
/**
* Set End Color.
*
* @return $this
*/
public function setEndColor(Color $color)
{
// make sure parameter is a real color and not a supervisor
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
if ($this->isSupervisor) {
$styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->endColor = $color;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
// Note that we don't care about colours for fill type NONE, but could have duplicate NONEs with
// different hashes if we don't explicitly prevent this
return md5(
$this->getFillType() .
$this->getRotation() .
($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '') .
($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '') .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'endColor', $this->getEndColor());
$this->exportArray2($exportedArray, 'fillType', $this->getFillType());
$this->exportArray2($exportedArray, 'rotation', $this->getRotation());
$this->exportArray2($exportedArray, 'startColor', $this->getStartColor());
return $exportedArray;
}
}

View File

@@ -0,0 +1,565 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
class Font extends Supervisor
{
// Underline types
const UNDERLINE_NONE = 'none';
const UNDERLINE_DOUBLE = 'double';
const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting';
const UNDERLINE_SINGLE = 'single';
const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting';
/**
* Font Name.
*
* @var null|string
*/
protected $name = 'Calibri';
/**
* Font Size.
*
* @var null|float
*/
protected $size = 11;
/**
* Bold.
*
* @var null|bool
*/
protected $bold = false;
/**
* Italic.
*
* @var null|bool
*/
protected $italic = false;
/**
* Superscript.
*
* @var null|bool
*/
protected $superscript = false;
/**
* Subscript.
*
* @var null|bool
*/
protected $subscript = false;
/**
* Underline.
*
* @var null|string
*/
protected $underline = self::UNDERLINE_NONE;
/**
* Strikethrough.
*
* @var null|bool
*/
protected $strikethrough = false;
/**
* Foreground color.
*
* @var Color
*/
protected $color;
/**
* @var null|int
*/
public $colorIndex;
/**
* Create a new Font.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
if ($isConditional) {
$this->name = null;
$this->size = null;
$this->bold = null;
$this->italic = null;
$this->superscript = null;
$this->subscript = null;
$this->underline = null;
$this->strikethrough = null;
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
} else {
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
}
// bind parent if we are a supervisor
if ($isSupervisor) {
$this->color->bindParent($this, 'color');
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Font
*/
public function getSharedComponent()
{
return $this->parent->getSharedComponent()->getFont();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['font' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
* [
* 'name' => 'Arial',
* 'bold' => TRUE,
* 'italic' => FALSE,
* 'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE,
* 'strikethrough' => FALSE,
* 'color' => [
* 'rgb' => '808080'
* ]
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['name'])) {
$this->setName($styleArray['name']);
}
if (isset($styleArray['bold'])) {
$this->setBold($styleArray['bold']);
}
if (isset($styleArray['italic'])) {
$this->setItalic($styleArray['italic']);
}
if (isset($styleArray['superscript'])) {
$this->setSuperscript($styleArray['superscript']);
}
if (isset($styleArray['subscript'])) {
$this->setSubscript($styleArray['subscript']);
}
if (isset($styleArray['underline'])) {
$this->setUnderline($styleArray['underline']);
}
if (isset($styleArray['strikethrough'])) {
$this->setStrikethrough($styleArray['strikethrough']);
}
if (isset($styleArray['color'])) {
$this->getColor()->applyFromArray($styleArray['color']);
}
if (isset($styleArray['size'])) {
$this->setSize($styleArray['size']);
}
}
return $this;
}
/**
* Get Name.
*
* @return null|string
*/
public function getName()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getName();
}
return $this->name;
}
/**
* Set Name.
*
* @param string $fontname
*
* @return $this
*/
public function setName($fontname)
{
if ($fontname == '') {
$fontname = 'Calibri';
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['name' => $fontname]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->name = $fontname;
}
return $this;
}
/**
* Get Size.
*
* @return null|float
*/
public function getSize()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getSize();
}
return $this->size;
}
/**
* Set Size.
*
* @param mixed $sizeInPoints A float representing the value of a positive measurement in points (1/72 of an inch)
*
* @return $this
*/
public function setSize($sizeInPoints)
{
if (is_string($sizeInPoints) || is_int($sizeInPoints)) {
$sizeInPoints = (float) $sizeInPoints; // $pValue = 0 if given string is not numeric
}
// Size must be a positive floating point number
// ECMA-376-1:2016, part 1, chapter 18.4.11 sz (Font Size), p. 1536
if (!is_float($sizeInPoints) || !($sizeInPoints > 0)) {
$sizeInPoints = 10.0;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['size' => $sizeInPoints]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->size = $sizeInPoints;
}
return $this;
}
/**
* Get Bold.
*
* @return null|bool
*/
public function getBold()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getBold();
}
return $this->bold;
}
/**
* Set Bold.
*
* @param bool $bold
*
* @return $this
*/
public function setBold($bold)
{
if ($bold == '') {
$bold = false;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['bold' => $bold]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->bold = $bold;
}
return $this;
}
/**
* Get Italic.
*
* @return null|bool
*/
public function getItalic()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getItalic();
}
return $this->italic;
}
/**
* Set Italic.
*
* @param bool $italic
*
* @return $this
*/
public function setItalic($italic)
{
if ($italic == '') {
$italic = false;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['italic' => $italic]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->italic = $italic;
}
return $this;
}
/**
* Get Superscript.
*
* @return null|bool
*/
public function getSuperscript()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getSuperscript();
}
return $this->superscript;
}
/**
* Set Superscript.
*
* @return $this
*/
public function setSuperscript(bool $superscript)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['superscript' => $superscript]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->superscript = $superscript;
if ($this->superscript) {
$this->subscript = false;
}
}
return $this;
}
/**
* Get Subscript.
*
* @return null|bool
*/
public function getSubscript()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getSubscript();
}
return $this->subscript;
}
/**
* Set Subscript.
*
* @return $this
*/
public function setSubscript(bool $subscript)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['subscript' => $subscript]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->subscript = $subscript;
if ($this->subscript) {
$this->superscript = false;
}
}
return $this;
}
/**
* Get Underline.
*
* @return null|string
*/
public function getUnderline()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getUnderline();
}
return $this->underline;
}
/**
* Set Underline.
*
* @param bool|string $underlineStyle \PhpOffice\PhpSpreadsheet\Style\Font underline type
* If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE,
* false equates to UNDERLINE_NONE
*
* @return $this
*/
public function setUnderline($underlineStyle)
{
if (is_bool($underlineStyle)) {
$underlineStyle = ($underlineStyle) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE;
} elseif ($underlineStyle == '') {
$underlineStyle = self::UNDERLINE_NONE;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['underline' => $underlineStyle]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->underline = $underlineStyle;
}
return $this;
}
/**
* Get Strikethrough.
*
* @return null|bool
*/
public function getStrikethrough()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getStrikethrough();
}
return $this->strikethrough;
}
/**
* Set Strikethrough.
*
* @param bool $strikethru
*
* @return $this
*/
public function setStrikethrough($strikethru)
{
if ($strikethru == '') {
$strikethru = false;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['strikethrough' => $strikethru]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->strikethrough = $strikethru;
}
return $this;
}
/**
* Get Color.
*
* @return Color
*/
public function getColor()
{
return $this->color;
}
/**
* Set Color.
*
* @return $this
*/
public function setColor(Color $color)
{
// make sure parameter is a real color and not a supervisor
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
if ($this->isSupervisor) {
$styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->color = $color;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->name .
$this->size .
($this->bold ? 't' : 'f') .
($this->italic ? 't' : 'f') .
($this->superscript ? 't' : 'f') .
($this->subscript ? 't' : 'f') .
$this->underline .
($this->strikethrough ? 't' : 'f') .
$this->color->getHashCode() .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'bold', $this->getBold());
$this->exportArray2($exportedArray, 'color', $this->getColor());
$this->exportArray2($exportedArray, 'italic', $this->getItalic());
$this->exportArray2($exportedArray, 'name', $this->getName());
$this->exportArray2($exportedArray, 'size', $this->getSize());
$this->exportArray2($exportedArray, 'strikethrough', $this->getStrikethrough());
$this->exportArray2($exportedArray, 'subscript', $this->getSubscript());
$this->exportArray2($exportedArray, 'superscript', $this->getSuperscript());
$this->exportArray2($exportedArray, 'underline', $this->getUnderline());
return $exportedArray;
}
}

View File

@@ -0,0 +1,409 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
class NumberFormat extends Supervisor
{
// Pre-defined formats
const FORMAT_GENERAL = 'General';
const FORMAT_TEXT = '@';
const FORMAT_NUMBER = '0';
const FORMAT_NUMBER_00 = '0.00';
const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00';
const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-';
const FORMAT_PERCENTAGE = '0%';
const FORMAT_PERCENTAGE_00 = '0.00%';
const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd';
const FORMAT_DATE_YYYYMMDD = 'yyyy-mm-dd';
const FORMAT_DATE_DDMMYYYY = 'dd/mm/yyyy';
const FORMAT_DATE_DMYSLASH = 'd/m/yy';
const FORMAT_DATE_DMYMINUS = 'd-m-yy';
const FORMAT_DATE_DMMINUS = 'd-m';
const FORMAT_DATE_MYMINUS = 'm-yy';
const FORMAT_DATE_XLSX14 = 'mm-dd-yy';
const FORMAT_DATE_XLSX15 = 'd-mmm-yy';
const FORMAT_DATE_XLSX16 = 'd-mmm';
const FORMAT_DATE_XLSX17 = 'mmm-yy';
const FORMAT_DATE_XLSX22 = 'm/d/yy h:mm';
const FORMAT_DATE_DATETIME = 'd/m/yy h:mm';
const FORMAT_DATE_TIME1 = 'h:mm AM/PM';
const FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM';
const FORMAT_DATE_TIME3 = 'h:mm';
const FORMAT_DATE_TIME4 = 'h:mm:ss';
const FORMAT_DATE_TIME5 = 'mm:ss';
const FORMAT_DATE_TIME6 = 'h:mm:ss';
const FORMAT_DATE_TIME7 = 'i:s.S';
const FORMAT_DATE_TIME8 = 'h:mm:ss;@';
const FORMAT_DATE_YYYYMMDDSLASH = 'yyyy/mm/dd;@';
const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-';
const FORMAT_CURRENCY_USD = '$#,##0_-';
const FORMAT_CURRENCY_EUR_SIMPLE = '#,##0.00_-"€"';
const FORMAT_CURRENCY_EUR = '#,##0_-"€"';
const FORMAT_ACCOUNTING_USD = '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)';
const FORMAT_ACCOUNTING_EUR = '_("€"* #,##0.00_);_("€"* \(#,##0.00\);_("€"* "-"??_);_(@_)';
/**
* Excel built-in number formats.
*
* @var array
*/
protected static $builtInFormats;
/**
* Excel built-in number formats (flipped, for faster lookups).
*
* @var array
*/
protected static $flippedBuiltInFormats;
/**
* Format Code.
*
* @var null|string
*/
protected $formatCode = self::FORMAT_GENERAL;
/**
* Built-in format Code.
*
* @var false|int
*/
protected $builtInFormatCode = 0;
/**
* Create a new NumberFormat.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
if ($isConditional) {
$this->formatCode = null;
$this->builtInFormatCode = false;
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return NumberFormat
*/
public function getSharedComponent()
{
return $this->parent->getSharedComponent()->getNumberFormat();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['numberFormat' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray(
* [
* 'formatCode' => NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['formatCode'])) {
$this->setFormatCode($styleArray['formatCode']);
}
}
return $this;
}
/**
* Get Format Code.
*
* @return null|string
*/
public function getFormatCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getFormatCode();
}
if ($this->builtInFormatCode !== false) {
return self::builtInFormatCode($this->builtInFormatCode);
}
return $this->formatCode;
}
/**
* Set Format Code.
*
* @param string $formatCode see self::FORMAT_*
*
* @return $this
*/
public function setFormatCode($formatCode)
{
if ($formatCode == '') {
$formatCode = self::FORMAT_GENERAL;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['formatCode' => $formatCode]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->formatCode = $formatCode;
$this->builtInFormatCode = self::builtInFormatCodeIndex($formatCode);
}
return $this;
}
/**
* Get Built-In Format Code.
*
* @return false|int
*/
public function getBuiltInFormatCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getBuiltInFormatCode();
}
return $this->builtInFormatCode;
}
/**
* Set Built-In Format Code.
*
* @param int $formatCodeIndex
*
* @return $this
*/
public function setBuiltInFormatCode($formatCodeIndex)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['formatCode' => self::builtInFormatCode($formatCodeIndex)]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->builtInFormatCode = $formatCodeIndex;
$this->formatCode = self::builtInFormatCode($formatCodeIndex);
}
return $this;
}
/**
* Fill built-in format codes.
*/
private static function fillBuiltInFormatCodes(): void
{
// [MS-OI29500: Microsoft Office Implementation Information for ISO/IEC-29500 Standard Compliance]
// 18.8.30. numFmt (Number Format)
//
// The ECMA standard defines built-in format IDs
// 14: "mm-dd-yy"
// 22: "m/d/yy h:mm"
// 37: "#,##0 ;(#,##0)"
// 38: "#,##0 ;[Red](#,##0)"
// 39: "#,##0.00;(#,##0.00)"
// 40: "#,##0.00;[Red](#,##0.00)"
// 47: "mmss.0"
// KOR fmt 55: "yyyy-mm-dd"
// Excel defines built-in format IDs
// 14: "m/d/yyyy"
// 22: "m/d/yyyy h:mm"
// 37: "#,##0_);(#,##0)"
// 38: "#,##0_);[Red](#,##0)"
// 39: "#,##0.00_);(#,##0.00)"
// 40: "#,##0.00_);[Red](#,##0.00)"
// 47: "mm:ss.0"
// KOR fmt 55: "yyyy/mm/dd"
// Built-in format codes
if (self::$builtInFormats === null) {
self::$builtInFormats = [];
// General
self::$builtInFormats[0] = self::FORMAT_GENERAL;
self::$builtInFormats[1] = '0';
self::$builtInFormats[2] = '0.00';
self::$builtInFormats[3] = '#,##0';
self::$builtInFormats[4] = '#,##0.00';
self::$builtInFormats[9] = '0%';
self::$builtInFormats[10] = '0.00%';
self::$builtInFormats[11] = '0.00E+00';
self::$builtInFormats[12] = '# ?/?';
self::$builtInFormats[13] = '# ??/??';
self::$builtInFormats[14] = 'm/d/yyyy'; // Despite ECMA 'mm-dd-yy';
self::$builtInFormats[15] = 'd-mmm-yy';
self::$builtInFormats[16] = 'd-mmm';
self::$builtInFormats[17] = 'mmm-yy';
self::$builtInFormats[18] = 'h:mm AM/PM';
self::$builtInFormats[19] = 'h:mm:ss AM/PM';
self::$builtInFormats[20] = 'h:mm';
self::$builtInFormats[21] = 'h:mm:ss';
self::$builtInFormats[22] = 'm/d/yyyy h:mm'; // Despite ECMA 'm/d/yy h:mm';
self::$builtInFormats[37] = '#,##0_);(#,##0)'; // Despite ECMA '#,##0 ;(#,##0)';
self::$builtInFormats[38] = '#,##0_);[Red](#,##0)'; // Despite ECMA '#,##0 ;[Red](#,##0)';
self::$builtInFormats[39] = '#,##0.00_);(#,##0.00)'; // Despite ECMA '#,##0.00;(#,##0.00)';
self::$builtInFormats[40] = '#,##0.00_);[Red](#,##0.00)'; // Despite ECMA '#,##0.00;[Red](#,##0.00)';
self::$builtInFormats[44] = '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)';
self::$builtInFormats[45] = 'mm:ss';
self::$builtInFormats[46] = '[h]:mm:ss';
self::$builtInFormats[47] = 'mm:ss.0'; // Despite ECMA 'mmss.0';
self::$builtInFormats[48] = '##0.0E+0';
self::$builtInFormats[49] = '@';
// CHT
self::$builtInFormats[27] = '[$-404]e/m/d';
self::$builtInFormats[30] = 'm/d/yy';
self::$builtInFormats[36] = '[$-404]e/m/d';
self::$builtInFormats[50] = '[$-404]e/m/d';
self::$builtInFormats[57] = '[$-404]e/m/d';
// THA
self::$builtInFormats[59] = 't0';
self::$builtInFormats[60] = 't0.00';
self::$builtInFormats[61] = 't#,##0';
self::$builtInFormats[62] = 't#,##0.00';
self::$builtInFormats[67] = 't0%';
self::$builtInFormats[68] = 't0.00%';
self::$builtInFormats[69] = 't# ?/?';
self::$builtInFormats[70] = 't# ??/??';
// JPN
self::$builtInFormats[28] = '[$-411]ggge"年"m"月"d"日"';
self::$builtInFormats[29] = '[$-411]ggge"年"m"月"d"日"';
self::$builtInFormats[31] = 'yyyy"年"m"月"d"日"';
self::$builtInFormats[32] = 'h"時"mm"分"';
self::$builtInFormats[33] = 'h"時"mm"分"ss"秒"';
self::$builtInFormats[34] = 'yyyy"年"m"月"';
self::$builtInFormats[35] = 'm"月"d"日"';
self::$builtInFormats[51] = '[$-411]ggge"年"m"月"d"日"';
self::$builtInFormats[52] = 'yyyy"年"m"月"';
self::$builtInFormats[53] = 'm"月"d"日"';
self::$builtInFormats[54] = '[$-411]ggge"年"m"月"d"日"';
self::$builtInFormats[55] = 'yyyy"年"m"月"';
self::$builtInFormats[56] = 'm"月"d"日"';
self::$builtInFormats[58] = '[$-411]ggge"年"m"月"d"日"';
// Flip array (for faster lookups)
self::$flippedBuiltInFormats = array_flip(self::$builtInFormats);
}
}
/**
* Get built-in format code.
*
* @param int $index
*
* @return string
*/
public static function builtInFormatCode($index)
{
// Clean parameter
$index = (int) $index;
// Ensure built-in format codes are available
self::fillBuiltInFormatCodes();
// Lookup format code
if (isset(self::$builtInFormats[$index])) {
return self::$builtInFormats[$index];
}
return '';
}
/**
* Get built-in format code index.
*
* @param string $formatCodeIndex
*
* @return bool|int
*/
public static function builtInFormatCodeIndex($formatCodeIndex)
{
// Ensure built-in format codes are available
self::fillBuiltInFormatCodes();
// Lookup format code
if (array_key_exists($formatCodeIndex, self::$flippedBuiltInFormats)) {
return self::$flippedBuiltInFormats[$formatCodeIndex];
}
return false;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->formatCode .
$this->builtInFormatCode .
__CLASS__
);
}
/**
* Convert a value in a pre-defined format to a PHP string.
*
* @param mixed $value Value to format
* @param string $format Format code, see = self::FORMAT_*
* @param array $callBack Callback function for additional formatting of string
*
* @return string Formatted string
*/
public static function toFormattedString($value, $format, $callBack = null)
{
return NumberFormat\Formatter::toFormattedString($value, $format, $callBack);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'formatCode', $this->getFormatCode());
return $exportedArray;
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
abstract class BaseFormatter
{
protected static function stripQuotes(string $format): string
{
// Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
return str_replace(['"', '*'], '', $format);
}
}

View File

@@ -0,0 +1,129 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Shared\Date;
class DateFormatter
{
/**
* Search/replace values to convert Excel date/time format masks to PHP format masks.
*
* @var array
*/
private static $dateFormatReplacements = [
// first remove escapes related to non-format characters
'\\' => '',
// 12-hour suffix
'am/pm' => 'A',
// 4-digit year
'e' => 'Y',
'yyyy' => 'Y',
// 2-digit year
'yy' => 'y',
// first letter of month - no php equivalent
'mmmmm' => 'M',
// full month name
'mmmm' => 'F',
// short month name
'mmm' => 'M',
// mm is minutes if time, but can also be month w/leading zero
// so we try to identify times be the inclusion of a : separator in the mask
// It isn't perfect, but the best way I know how
':mm' => ':i',
'mm:' => 'i:',
// month leading zero
'mm' => 'm',
// month no leading zero
'm' => 'n',
// full day of week name
'dddd' => 'l',
// short day of week name
'ddd' => 'D',
// days leading zero
'dd' => 'd',
// days no leading zero
'd' => 'j',
// seconds
'ss' => 's',
// fractional seconds - no php equivalent
'.s' => '',
];
/**
* Search/replace values to convert Excel date/time format masks hours to PHP format masks (24 hr clock).
*
* @var array
*/
private static $dateFormatReplacements24 = [
'hh' => 'H',
'h' => 'G',
];
/**
* Search/replace values to convert Excel date/time format masks hours to PHP format masks (12 hr clock).
*
* @var array
*/
private static $dateFormatReplacements12 = [
'hh' => 'h',
'h' => 'g',
];
public static function format($value, string $format): string
{
// strip off first part containing e.g. [$-F800] or [$USD-409]
// general syntax: [$<Currency string>-<language info>]
// language info is in hexadecimal
// strip off chinese part like [DBNum1][$-804]
$format = preg_replace('/^(\[DBNum\d\])*(\[\$[^\]]*\])/i', '', $format);
// OpenOffice.org uses upper-case number formats, e.g. 'YYYY', convert to lower-case;
// but we don't want to change any quoted strings
$format = preg_replace_callback('/(?:^|")([^"]*)(?:$|")/', ['self', 'setLowercaseCallback'], $format);
// Only process the non-quoted blocks for date format characters
$blocks = explode('"', $format);
foreach ($blocks as $key => &$block) {
if ($key % 2 == 0) {
$block = strtr($block, self::$dateFormatReplacements);
if (!strpos($block, 'A')) {
// 24-hour time format
// when [h]:mm format, the [h] should replace to the hours of the value * 24
if (false !== strpos($block, '[h]')) {
$hours = (int) ($value * 24);
$block = str_replace('[h]', $hours, $block);
continue;
}
$block = strtr($block, self::$dateFormatReplacements24);
} else {
// 12-hour time format
$block = strtr($block, self::$dateFormatReplacements12);
}
}
}
$format = implode('"', $blocks);
// escape any quoted characters so that DateTime format() will render them correctly
$format = preg_replace_callback('/"(.*)"/U', ['self', 'escapeQuotesCallback'], $format);
$dateObj = Date::excelToDateTimeObject($value);
// If the colon preceding minute had been quoted, as happens in
// Excel 2003 XML formats, m will not have been changed to i above.
// Change it now.
$format = \preg_replace('/\\\\:m/', ':i', $format);
return $dateObj->format($format);
}
private static function setLowercaseCallback($matches): string
{
return mb_strtolower($matches[0]);
}
private static function escapeQuotesCallback($matches): string
{
return '\\' . implode('\\', str_split($matches[1]));
}
}

View File

@@ -0,0 +1,162 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class Formatter
{
private static function splitFormatCompare($value, $cond, $val, $dfcond, $dfval)
{
if (!$cond) {
$cond = $dfcond;
$val = $dfval;
}
switch ($cond) {
case '>':
return $value > $val;
case '<':
return $value < $val;
case '<=':
return $value <= $val;
case '<>':
return $value != $val;
case '=':
return $value == $val;
}
return $value >= $val;
}
private static function splitFormat($sections, $value)
{
// Extract the relevant section depending on whether number is positive, negative, or zero?
// Text not supported yet.
// Here is how the sections apply to various values in Excel:
// 1 section: [POSITIVE/NEGATIVE/ZERO/TEXT]
// 2 sections: [POSITIVE/ZERO/TEXT] [NEGATIVE]
// 3 sections: [POSITIVE/TEXT] [NEGATIVE] [ZERO]
// 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
$cnt = count($sections);
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . ')\\]/mui';
$cond_regex = '/\\[(>|>=|<|<=|=|<>)([+-]?\\d+([.]\\d+)?)\\]/';
$colors = ['', '', '', '', ''];
$condops = ['', '', '', '', ''];
$condvals = [0, 0, 0, 0, 0];
for ($idx = 0; $idx < $cnt; ++$idx) {
if (preg_match($color_regex, $sections[$idx], $matches)) {
$colors[$idx] = $matches[0];
$sections[$idx] = preg_replace($color_regex, '', $sections[$idx]);
}
if (preg_match($cond_regex, $sections[$idx], $matches)) {
$condops[$idx] = $matches[1];
$condvals[$idx] = $matches[2];
$sections[$idx] = preg_replace($cond_regex, '', $sections[$idx]);
}
}
$color = $colors[0];
$format = $sections[0];
$absval = $value;
switch ($cnt) {
case 2:
$absval = abs($value);
if (!self::splitFormatCompare($value, $condops[0], $condvals[0], '>=', 0)) {
$color = $colors[1];
$format = $sections[1];
}
break;
case 3:
case 4:
$absval = abs($value);
if (!self::splitFormatCompare($value, $condops[0], $condvals[0], '>', 0)) {
if (self::splitFormatCompare($value, $condops[1], $condvals[1], '<', 0)) {
$color = $colors[1];
$format = $sections[1];
} else {
$color = $colors[2];
$format = $sections[2];
}
}
break;
}
return [$color, $format, $absval];
}
/**
* Convert a value in a pre-defined format to a PHP string.
*
* @param mixed $value Value to format
* @param string $format Format code, see = NumberFormat::FORMAT_*
* @param array $callBack Callback function for additional formatting of string
*
* @return string Formatted string
*/
public static function toFormattedString($value, $format, $callBack = null)
{
// For now we do not treat strings although section 4 of a format code affects strings
if (!is_numeric($value)) {
return $value;
}
// For 'General' format code, we just pass the value although this is not entirely the way Excel does it,
// it seems to round numbers to a total of 10 digits.
if (($format === NumberFormat::FORMAT_GENERAL) || ($format === NumberFormat::FORMAT_TEXT)) {
return $value;
}
$format = preg_replace_callback(
'/(["])(?:(?=(\\\\?))\\2.)*?\\1/u',
function ($matches) {
return str_replace('.', chr(0x00), $matches[0]);
},
$format
);
// Convert any other escaped characters to quoted strings, e.g. (\T to "T")
$format = preg_replace('/(\\\(((.)(?!((AM\/PM)|(A\/P))))|([^ ])))(?=(?:[^"]|"[^"]*")*$)/ui', '"${2}"', $format);
// Get the sections, there can be up to four sections, separated with a semi-colon (but only if not a quoted literal)
$sections = preg_split('/(;)(?=(?:[^"]|"[^"]*")*$)/u', $format);
[$colors, $format, $value] = self::splitFormat($sections, $value);
// In Excel formats, "_" is used to add spacing,
// The following character indicates the size of the spacing, which we can't do in HTML, so we just use a standard space
$format = 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, $matches)) {
// datetime format
$value = DateFormatter::format($value, $format);
} else {
if (substr($format, 0, 1) === '"' && substr($format, -1, 1) === '"' && substr_count($format, '"') === 2) {
$value = substr($format, 1, -1);
} elseif (preg_match('/[0#, ]%/', $format)) {
// % number format
$value = PercentageFormatter::format($value, $format);
} else {
$value = NumberFormatter::format($value, $format);
}
}
// Additional formatting provided by callback function
if ($callBack !== null) {
[$writerInstance, $function] = $callBack;
$value = $writerInstance->$function($value, $colors);
}
$value = str_replace(chr(0x00), '.', $value);
return $value;
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
class FractionFormatter extends BaseFormatter
{
/**
* @param mixed $value
*/
public static function format($value, string $format): string
{
$format = self::stripQuotes($format);
$value = (float) $value;
$absValue = abs($value);
$sign = ($value < 0.0) ? '-' : '';
$integerPart = floor($absValue);
$decimalPart = self::getDecimal((string) $absValue);
if ($decimalPart === '0') {
return "{$sign}{$integerPart}";
}
$decimalLength = strlen($decimalPart);
$decimalDivisor = 10 ** $decimalLength;
$GCD = MathTrig\Gcd::evaluate($decimalPart, $decimalDivisor);
$adjustedDecimalPart = $decimalPart / $GCD;
$adjustedDecimalDivisor = $decimalDivisor / $GCD;
if ((strpos($format, '0') !== false)) {
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
} elseif ((strpos($format, '#') !== false)) {
if ($integerPart == 0) {
return "{$sign}{$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
}
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
} elseif ((substr($format, 0, 3) == '? ?')) {
if ($integerPart == 0) {
$integerPart = '';
}
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
}
$adjustedDecimalPart += $integerPart * $adjustedDecimalDivisor;
return "{$sign}{$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
}
private static function getDecimal(string $value): string
{
$decimalPart = '0';
if (preg_match('/^\\d*[.](\\d*[1-9])0*$/', $value, $matches) === 1) {
$decimalPart = $matches[1];
}
return $decimalPart;
}
}

View File

@@ -0,0 +1,210 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class NumberFormatter
{
private const NUMBER_REGEX = '/(0+)(\\.?)(0*)/';
private static function mergeComplexNumberFormatMasks(array $numbers, array $masks): array
{
$decimalCount = strlen($numbers[1]);
$postDecimalMasks = [];
do {
$tempMask = array_pop($masks);
if ($tempMask !== null) {
$postDecimalMasks[] = $tempMask;
$decimalCount -= strlen($tempMask);
}
} while ($tempMask !== null && $decimalCount > 0);
return [
implode('.', $masks),
implode('.', array_reverse($postDecimalMasks)),
];
}
/**
* @param mixed $number
*/
private static function processComplexNumberFormatMask($number, string $mask): string
{
$result = $number;
$maskingBlockCount = preg_match_all('/0+/', $mask, $maskingBlocks, PREG_OFFSET_CAPTURE);
if ($maskingBlockCount > 1) {
$maskingBlocks = array_reverse($maskingBlocks[0]);
$offset = 0;
foreach ($maskingBlocks as $block) {
$size = strlen($block[0]);
$divisor = 10 ** $size;
$offset = $block[1];
$blockValue = sprintf("%0{$size}d", fmod($number, $divisor));
$number = floor($number / $divisor);
$mask = substr_replace($mask, $blockValue, $offset, $size);
}
if ($number > 0) {
$mask = substr_replace($mask, $number, $offset, 0);
}
$result = $mask;
}
return self::makeString($result);
}
/**
* @param mixed $number
*/
private static function complexNumberFormatMask($number, string $mask, bool $splitOnPoint = true): string
{
$sign = ($number < 0.0) ? '-' : '';
$number = (string) abs($number);
if ($splitOnPoint && strpos($mask, '.') !== false && strpos($number, '.') !== false) {
$numbers = explode('.', $number);
$masks = explode('.', $mask);
if (count($masks) > 2) {
$masks = self::mergeComplexNumberFormatMasks($numbers, $masks);
}
$integerPart = self::complexNumberFormatMask($numbers[0], $masks[0], false);
$decimalPart = strrev(self::complexNumberFormatMask(strrev($numbers[1]), strrev($masks[1]), false));
return "{$sign}{$integerPart}.{$decimalPart}";
}
$result = self::processComplexNumberFormatMask($number, $mask);
return "{$sign}{$result}";
}
/**
* @param mixed $value
*/
private static function formatStraightNumericValue($value, string $format, array $matches, bool $useThousands): string
{
$left = $matches[1];
$dec = $matches[2];
$right = $matches[3];
// minimun width of formatted number (including dot)
$minWidth = strlen($left) + strlen($dec) + strlen($right);
if ($useThousands) {
$value = number_format(
$value,
strlen($right),
StringHelper::getDecimalSeparator(),
StringHelper::getThousandsSeparator()
);
return self::pregReplace(self::NUMBER_REGEX, $value, $format);
}
if (preg_match('/[0#]E[+-]0/i', $format)) {
// Scientific format
return sprintf('%5.2E', $value);
} elseif (preg_match('/0([^\d\.]+)0/', $format) || substr_count($format, '.') > 1) {
if ($value == (int) $value && substr_count($format, '.') === 1) {
$value *= 10 ** strlen(explode('.', $format)[1]);
}
return self::complexNumberFormatMask($value, $format);
}
$sprintf_pattern = "%0$minWidth." . strlen($right) . 'f';
$value = sprintf($sprintf_pattern, $value);
return self::pregReplace(self::NUMBER_REGEX, $value, $format);
}
/**
* @param mixed $value
*/
public static function format($value, string $format): string
{
// The "_" in this string has already been stripped out,
// so this test is never true. Furthermore, testing
// on Excel shows this format uses Euro symbol, not "EUR".
//if ($format === NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE) {
// return 'EUR ' . sprintf('%1.2f', $value);
//}
// Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
$format = self::makeString(str_replace(['"', '*'], '', $format));
// Find out if we need thousands separator
// This is indicated by a comma enclosed by a digit placeholder:
// #,# or 0,0
$useThousands = (bool) preg_match('/(#,#|0,0)/', $format);
if ($useThousands) {
$format = self::pregReplace('/0,0/', '00', $format);
$format = self::pregReplace('/#,#/', '##', $format);
}
// Scale thousands, millions,...
// This is indicated by a number of commas after a digit placeholder:
// #, or 0.0,,
$scale = 1; // same as no scale
$matches = [];
if (preg_match('/(#|0)(,+)/', $format, $matches)) {
$scale = 1000 ** strlen($matches[2]);
// strip the commas
$format = self::pregReplace('/0,+/', '0', $format);
$format = self::pregReplace('/#,+/', '#', $format);
}
if (preg_match('/#?.*\?\/\?/', $format, $m)) {
$value = FractionFormatter::format($value, $format);
} else {
// Handle the number itself
// scale number
$value = $value / $scale;
// Strip #
$format = self::pregReplace('/\\#/', '0', $format);
// Remove locale code [$-###]
$format = self::pregReplace('/\[\$\-.*\]/', '', $format);
$n = '/\\[[^\\]]+\\]/';
$m = self::pregReplace($n, '', $format);
if (preg_match(self::NUMBER_REGEX, $m, $matches)) {
// There are placeholders for digits, so inject digits from the value into the mask
$value = self::formatStraightNumericValue($value, $format, $matches, $useThousands);
} elseif ($format !== NumberFormat::FORMAT_GENERAL) {
// Yes, I know that this is basically just a hack;
// if there's no placeholders for digits, just return the format mask "as is"
$value = self::makeString(str_replace('?', '', $format));
}
}
if (preg_match('/\[\$(.*)\]/u', $format, $m)) {
// Currency or Accounting
$currencyCode = $m[1];
[$currencyCode] = explode('-', $currencyCode);
if ($currencyCode == '') {
$currencyCode = StringHelper::getCurrencyCode();
}
$value = self::pregReplace('/\[\$([^\]]*)\]/u', $currencyCode, (string) $value);
}
return (string) $value;
}
/**
* @param mixed $value
*/
private static function makeString($value): string
{
return is_array($value) ? '' : (string) $value;
}
private static function pregReplace(string $pattern, string $replacement, string $subject): string
{
return self::makeString(preg_replace($pattern, $replacement, $subject));
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class PercentageFormatter extends BaseFormatter
{
public static function format($value, string $format): string
{
if ($format === NumberFormat::FORMAT_PERCENTAGE) {
return round((100 * $value), 0) . '%';
}
$value *= 100;
$format = self::stripQuotes($format);
[, $vDecimals] = explode('.', ((string) $value) . '.');
$vDecimalCount = strlen(rtrim($vDecimals, '0'));
$format = str_replace('%', '%%', $format);
$wholePartSize = strlen((string) floor($value));
$decimalPartSize = $placeHolders = 0;
// Number of decimals
if (preg_match('/\.([?0]+)/u', $format, $matches)) {
$decimalPartSize = strlen($matches[1]);
$vMinDecimalCount = strlen(rtrim($matches[1], '?'));
$decimalPartSize = min(max($vMinDecimalCount, $vDecimalCount), $decimalPartSize);
$placeHolders = str_repeat(' ', strlen($matches[1]) - $decimalPartSize);
}
// Number of digits to display before the decimal
if (preg_match('/([#0,]+)\./u', $format, $matches)) {
$wholePartSize = max($wholePartSize, strlen($matches[1]));
}
$wholePartSize += $decimalPartSize;
$replacement = "{$wholePartSize}.{$decimalPartSize}";
$mask = preg_replace('/[#0,]+\.?[?#0,]*/ui', "%{$replacement}f{$placeHolders}", $format);
return sprintf($mask, $value);
}
}

View File

@@ -0,0 +1,195 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
class Protection extends Supervisor
{
/** Protection styles */
const PROTECTION_INHERIT = 'inherit';
const PROTECTION_PROTECTED = 'protected';
const PROTECTION_UNPROTECTED = 'unprotected';
/**
* Locked.
*
* @var string
*/
protected $locked;
/**
* Hidden.
*
* @var string
*/
protected $hidden;
/**
* Create a new Protection.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
if (!$isConditional) {
$this->locked = self::PROTECTION_INHERIT;
$this->hidden = self::PROTECTION_INHERIT;
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Protection
*/
public function getSharedComponent()
{
return $this->parent->getSharedComponent()->getProtection();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['protection' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray(
* [
* 'locked' => TRUE,
* 'hidden' => FALSE
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['locked'])) {
$this->setLocked($styleArray['locked']);
}
if (isset($styleArray['hidden'])) {
$this->setHidden($styleArray['hidden']);
}
}
return $this;
}
/**
* Get locked.
*
* @return string
*/
public function getLocked()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getLocked();
}
return $this->locked;
}
/**
* Set locked.
*
* @param string $lockType see self::PROTECTION_*
*
* @return $this
*/
public function setLocked($lockType)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['locked' => $lockType]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->locked = $lockType;
}
return $this;
}
/**
* Get hidden.
*
* @return string
*/
public function getHidden()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHidden();
}
return $this->hidden;
}
/**
* Set hidden.
*
* @param string $hiddenType see self::PROTECTION_*
*
* @return $this
*/
public function setHidden($hiddenType)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['hidden' => $hiddenType]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->hidden = $hiddenType;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->locked .
$this->hidden .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'locked', $this->getLocked());
$this->exportArray2($exportedArray, 'hidden', $this->getHidden());
return $exportedArray;
}
}

View File

@@ -0,0 +1,737 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Style extends Supervisor
{
/**
* Font.
*
* @var Font
*/
protected $font;
/**
* Fill.
*
* @var Fill
*/
protected $fill;
/**
* Borders.
*
* @var Borders
*/
protected $borders;
/**
* Alignment.
*
* @var Alignment
*/
protected $alignment;
/**
* Number Format.
*
* @var NumberFormat
*/
protected $numberFormat;
/**
* Protection.
*
* @var Protection
*/
protected $protection;
/**
* Index of style in collection. Only used for real style.
*
* @var int
*/
protected $index;
/**
* Use Quote Prefix when displaying in cell editor. Only used for real style.
*
* @var bool
*/
protected $quotePrefix = false;
/**
* Internal cache for styles
* Used when applying style on range of cells (column or row) and cleared when
* all cells in range is styled.
*
* PhpSpreadsheet will always minimize the amount of styles used. So cells with
* same styles will reference the same Style instance. To check if two styles
* are similar Style::getHashCode() is used. This call is expensive. To minimize
* the need to call this method we can cache the internal PHP object id of the
* Style in the range. Style::getHashCode() will then only be called when we
* encounter a unique style.
*
* @see Style::applyFromArray()
* @see Style::getHashCode()
*
* @phpstan-var null|array{styleByHash: array<string, Style>, hashByObjId: array<int, string>}
*
* @var array<string, array>
*/
private static $cachedStyles;
/**
* Create a new Style.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
parent::__construct($isSupervisor);
// Initialise values
$this->font = new Font($isSupervisor, $isConditional);
$this->fill = new Fill($isSupervisor, $isConditional);
$this->borders = new Borders($isSupervisor);
$this->alignment = new Alignment($isSupervisor, $isConditional);
$this->numberFormat = new NumberFormat($isSupervisor, $isConditional);
$this->protection = new Protection($isSupervisor, $isConditional);
// bind parent if we are a supervisor
if ($isSupervisor) {
$this->font->bindParent($this);
$this->fill->bindParent($this);
$this->borders->bindParent($this);
$this->alignment->bindParent($this);
$this->numberFormat->bindParent($this);
$this->protection->bindParent($this);
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*/
public function getSharedComponent(): self
{
$activeSheet = $this->getActiveSheet();
$selectedCell = $this->getActiveCell(); // e.g. 'A1'
if ($activeSheet->cellExists($selectedCell)) {
$xfIndex = $activeSheet->getCell($selectedCell)->getXfIndex();
} else {
$xfIndex = 0;
}
return $this->parent->getCellXfByIndex($xfIndex);
}
/**
* Get parent. Only used for style supervisor.
*
* @return Spreadsheet
*/
public function getParent()
{
return $this->parent;
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['quotePrefix' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->applyFromArray(
* [
* 'font' => [
* 'name' => 'Arial',
* 'bold' => true,
* 'italic' => false,
* 'underline' => Font::UNDERLINE_DOUBLE,
* 'strikethrough' => false,
* 'color' => [
* 'rgb' => '808080'
* ]
* ],
* 'borders' => [
* 'bottom' => [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ],
* 'top' => [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ]
* ],
* 'alignment' => [
* 'horizontal' => Alignment::HORIZONTAL_CENTER,
* 'vertical' => Alignment::VERTICAL_CENTER,
* 'wrapText' => true,
* ],
* 'quotePrefix' => true
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
* @param bool $advancedBorders advanced mode for setting borders
*
* @return $this
*/
public function applyFromArray(array $styleArray, $advancedBorders = true)
{
if ($this->isSupervisor) {
$pRange = $this->getSelectedCells();
// Uppercase coordinate
$pRange = strtoupper($pRange);
// Is it a cell range or a single cell?
if (strpos($pRange, ':') === false) {
$rangeA = $pRange;
$rangeB = $pRange;
} else {
[$rangeA, $rangeB] = explode(':', $pRange);
}
// Calculate range outer borders
$rangeStart = Coordinate::coordinateFromString($rangeA);
$rangeEnd = Coordinate::coordinateFromString($rangeB);
$rangeStartIndexes = Coordinate::indexesFromString($rangeA);
$rangeEndIndexes = Coordinate::indexesFromString($rangeB);
$columnStart = $rangeStart[0];
$columnEnd = $rangeEnd[0];
// Make sure we can loop upwards on rows and columns
if ($rangeStartIndexes[0] > $rangeEndIndexes[0] && $rangeStartIndexes[1] > $rangeEndIndexes[1]) {
$tmp = $rangeStartIndexes;
$rangeStartIndexes = $rangeEndIndexes;
$rangeEndIndexes = $tmp;
}
// ADVANCED MODE:
if ($advancedBorders && isset($styleArray['borders'])) {
// 'allBorders' is a shorthand property for 'outline' and 'inside' and
// it applies to components that have not been set explicitly
if (isset($styleArray['borders']['allBorders'])) {
foreach (['outline', 'inside'] as $component) {
if (!isset($styleArray['borders'][$component])) {
$styleArray['borders'][$component] = $styleArray['borders']['allBorders'];
}
}
unset($styleArray['borders']['allBorders']); // not needed any more
}
// 'outline' is a shorthand property for 'top', 'right', 'bottom', 'left'
// it applies to components that have not been set explicitly
if (isset($styleArray['borders']['outline'])) {
foreach (['top', 'right', 'bottom', 'left'] as $component) {
if (!isset($styleArray['borders'][$component])) {
$styleArray['borders'][$component] = $styleArray['borders']['outline'];
}
}
unset($styleArray['borders']['outline']); // not needed any more
}
// 'inside' is a shorthand property for 'vertical' and 'horizontal'
// it applies to components that have not been set explicitly
if (isset($styleArray['borders']['inside'])) {
foreach (['vertical', 'horizontal'] as $component) {
if (!isset($styleArray['borders'][$component])) {
$styleArray['borders'][$component] = $styleArray['borders']['inside'];
}
}
unset($styleArray['borders']['inside']); // not needed any more
}
// width and height characteristics of selection, 1, 2, or 3 (for 3 or more)
$xMax = min($rangeEndIndexes[0] - $rangeStartIndexes[0] + 1, 3);
$yMax = min($rangeEndIndexes[1] - $rangeStartIndexes[1] + 1, 3);
// loop through up to 3 x 3 = 9 regions
for ($x = 1; $x <= $xMax; ++$x) {
// start column index for region
$colStart = ($x == 3) ?
Coordinate::stringFromColumnIndex($rangeEndIndexes[0])
: Coordinate::stringFromColumnIndex($rangeStartIndexes[0] + $x - 1);
// end column index for region
$colEnd = ($x == 1) ?
Coordinate::stringFromColumnIndex($rangeStartIndexes[0])
: Coordinate::stringFromColumnIndex($rangeEndIndexes[0] - $xMax + $x);
for ($y = 1; $y <= $yMax; ++$y) {
// which edges are touching the region
$edges = [];
if ($x == 1) {
// are we at left edge
$edges[] = 'left';
}
if ($x == $xMax) {
// are we at right edge
$edges[] = 'right';
}
if ($y == 1) {
// are we at top edge?
$edges[] = 'top';
}
if ($y == $yMax) {
// are we at bottom edge?
$edges[] = 'bottom';
}
// start row index for region
$rowStart = ($y == 3) ?
$rangeEndIndexes[1] : $rangeStartIndexes[1] + $y - 1;
// end row index for region
$rowEnd = ($y == 1) ?
$rangeStartIndexes[1] : $rangeEndIndexes[1] - $yMax + $y;
// build range for region
$range = $colStart . $rowStart . ':' . $colEnd . $rowEnd;
// retrieve relevant style array for region
$regionStyles = $styleArray;
unset($regionStyles['borders']['inside']);
// what are the inner edges of the region when looking at the selection
$innerEdges = array_diff(['top', 'right', 'bottom', 'left'], $edges);
// inner edges that are not touching the region should take the 'inside' border properties if they have been set
foreach ($innerEdges as $innerEdge) {
switch ($innerEdge) {
case 'top':
case 'bottom':
// should pick up 'horizontal' border property if set
if (isset($styleArray['borders']['horizontal'])) {
$regionStyles['borders'][$innerEdge] = $styleArray['borders']['horizontal'];
} else {
unset($regionStyles['borders'][$innerEdge]);
}
break;
case 'left':
case 'right':
// should pick up 'vertical' border property if set
if (isset($styleArray['borders']['vertical'])) {
$regionStyles['borders'][$innerEdge] = $styleArray['borders']['vertical'];
} else {
unset($regionStyles['borders'][$innerEdge]);
}
break;
}
}
// apply region style to region by calling applyFromArray() in simple mode
$this->getActiveSheet()->getStyle($range)->applyFromArray($regionStyles, false);
}
}
// restore initial cell selection range
$this->getActiveSheet()->getStyle($pRange);
return $this;
}
// SIMPLE MODE:
// Selection type, inspect
if (preg_match('/^[A-Z]+1:[A-Z]+1048576$/', $pRange)) {
$selectionType = 'COLUMN';
// Enable caching of styles
self::$cachedStyles = ['hashByObjId' => [], 'styleByHash' => []];
} elseif (preg_match('/^A\d+:XFD\d+$/', $pRange)) {
$selectionType = 'ROW';
// Enable caching of styles
self::$cachedStyles = ['hashByObjId' => [], 'styleByHash' => []];
} else {
$selectionType = 'CELL';
}
// First loop through columns, rows, or cells to find out which styles are affected by this operation
$oldXfIndexes = $this->getOldXfIndexes($selectionType, $rangeStartIndexes, $rangeEndIndexes, $columnStart, $columnEnd, $styleArray);
// clone each of the affected styles, apply the style array, and add the new styles to the workbook
$workbook = $this->getActiveSheet()->getParent();
$newXfIndexes = [];
foreach ($oldXfIndexes as $oldXfIndex => $dummy) {
$style = $workbook->getCellXfByIndex($oldXfIndex);
// $cachedStyles is set when applying style for a range of cells, either column or row
if (self::$cachedStyles === null) {
// Clone the old style and apply style-array
$newStyle = clone $style;
$newStyle->applyFromArray($styleArray);
// Look for existing style we can use instead (reduce memory usage)
$existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode());
} else {
// Style cache is stored by Style::getHashCode(). But calling this method is
// expensive. So we cache the php obj id -> hash.
$objId = spl_object_id($style);
// Look for the original HashCode
$styleHash = self::$cachedStyles['hashByObjId'][$objId] ?? null;
if ($styleHash === null) {
// This object_id is not cached, store the hashcode in case encounter again
$styleHash = self::$cachedStyles['hashByObjId'][$objId] = $style->getHashCode();
}
// Find existing style by hash.
$existingStyle = self::$cachedStyles['styleByHash'][$styleHash] ?? null;
if (!$existingStyle) {
// The old style combined with the new style array is not cached, so we create it now
$newStyle = clone $style;
$newStyle->applyFromArray($styleArray);
// Look for similar style in workbook to reduce memory usage
$existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode());
// Cache the new style by original hashcode
self::$cachedStyles['styleByHash'][$styleHash] = $existingStyle instanceof self ? $existingStyle : $newStyle;
}
}
if ($existingStyle) {
// there is already such cell Xf in our collection
$newXfIndexes[$oldXfIndex] = $existingStyle->getIndex();
} else {
if (!isset($newStyle)) {
// Handle bug in PHPStan, see https://github.com/phpstan/phpstan/issues/5805
// $newStyle should always be defined.
// This block might not be needed in the future
$newStyle = clone $style;
$newStyle->applyFromArray($styleArray);
}
// we don't have such a cell Xf, need to add
$workbook->addCellXf($newStyle);
$newXfIndexes[$oldXfIndex] = $newStyle->getIndex();
}
}
// Loop through columns, rows, or cells again and update the XF index
switch ($selectionType) {
case 'COLUMN':
for ($col = $rangeStartIndexes[0]; $col <= $rangeEndIndexes[0]; ++$col) {
$columnDimension = $this->getActiveSheet()->getColumnDimensionByColumn($col);
$oldXfIndex = $columnDimension->getXfIndex();
$columnDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
}
// Disable caching of styles
self::$cachedStyles = null;
break;
case 'ROW':
for ($row = $rangeStartIndexes[1]; $row <= $rangeEndIndexes[1]; ++$row) {
$rowDimension = $this->getActiveSheet()->getRowDimension($row);
// row without explicit style should be formatted based on default style
$oldXfIndex = $rowDimension->getXfIndex() ?? 0;
$rowDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
}
// Disable caching of styles
self::$cachedStyles = null;
break;
case 'CELL':
for ($col = $rangeStartIndexes[0]; $col <= $rangeEndIndexes[0]; ++$col) {
for ($row = $rangeStartIndexes[1]; $row <= $rangeEndIndexes[1]; ++$row) {
$cell = $this->getActiveSheet()->getCellByColumnAndRow($col, $row);
$oldXfIndex = $cell->getXfIndex();
$cell->setXfIndex($newXfIndexes[$oldXfIndex]);
}
}
break;
}
} else {
// not a supervisor, just apply the style array directly on style object
if (isset($styleArray['fill'])) {
$this->getFill()->applyFromArray($styleArray['fill']);
}
if (isset($styleArray['font'])) {
$this->getFont()->applyFromArray($styleArray['font']);
}
if (isset($styleArray['borders'])) {
$this->getBorders()->applyFromArray($styleArray['borders']);
}
if (isset($styleArray['alignment'])) {
$this->getAlignment()->applyFromArray($styleArray['alignment']);
}
if (isset($styleArray['numberFormat'])) {
$this->getNumberFormat()->applyFromArray($styleArray['numberFormat']);
}
if (isset($styleArray['protection'])) {
$this->getProtection()->applyFromArray($styleArray['protection']);
}
if (isset($styleArray['quotePrefix'])) {
$this->quotePrefix = $styleArray['quotePrefix'];
}
}
return $this;
}
private function getOldXfIndexes(string $selectionType, array $rangeStart, array $rangeEnd, string $columnStart, string $columnEnd, array $styleArray): array
{
$oldXfIndexes = [];
switch ($selectionType) {
case 'COLUMN':
for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
$oldXfIndexes[$this->getActiveSheet()->getColumnDimensionByColumn($col)->getXfIndex()] = true;
}
foreach ($this->getActiveSheet()->getColumnIterator($columnStart, $columnEnd) as $columnIterator) {
$cellIterator = $columnIterator->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $columnCell) {
if ($columnCell !== null) {
$columnCell->getStyle()->applyFromArray($styleArray);
}
}
}
break;
case 'ROW':
for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
if ($this->getActiveSheet()->getRowDimension($row)->getXfIndex() === null) {
$oldXfIndexes[0] = true; // row without explicit style should be formatted based on default style
} else {
$oldXfIndexes[$this->getActiveSheet()->getRowDimension($row)->getXfIndex()] = true;
}
}
foreach ($this->getActiveSheet()->getRowIterator((int) $rangeStart[1], (int) $rangeEnd[1]) as $rowIterator) {
$cellIterator = $rowIterator->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $rowCell) {
if ($rowCell !== null) {
$rowCell->getStyle()->applyFromArray($styleArray);
}
}
}
break;
case 'CELL':
for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
$oldXfIndexes[$this->getActiveSheet()->getCellByColumnAndRow($col, $row)->getXfIndex()] = true;
}
}
break;
}
return $oldXfIndexes;
}
/**
* Get Fill.
*
* @return Fill
*/
public function getFill()
{
return $this->fill;
}
/**
* Get Font.
*
* @return Font
*/
public function getFont()
{
return $this->font;
}
/**
* Set font.
*
* @return $this
*/
public function setFont(Font $font)
{
$this->font = $font;
return $this;
}
/**
* Get Borders.
*
* @return Borders
*/
public function getBorders()
{
return $this->borders;
}
/**
* Get Alignment.
*
* @return Alignment
*/
public function getAlignment()
{
return $this->alignment;
}
/**
* Get Number Format.
*
* @return NumberFormat
*/
public function getNumberFormat()
{
return $this->numberFormat;
}
/**
* Get Conditional Styles. Only used on supervisor.
*
* @return Conditional[]
*/
public function getConditionalStyles()
{
return $this->getActiveSheet()->getConditionalStyles($this->getActiveCell());
}
/**
* Set Conditional Styles. Only used on supervisor.
*
* @param Conditional[] $conditionalStyleArray Array of conditional styles
*
* @return $this
*/
public function setConditionalStyles(array $conditionalStyleArray)
{
$this->getActiveSheet()->setConditionalStyles($this->getSelectedCells(), $conditionalStyleArray);
return $this;
}
/**
* Get Protection.
*
* @return Protection
*/
public function getProtection()
{
return $this->protection;
}
/**
* Get quote prefix.
*
* @return bool
*/
public function getQuotePrefix()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getQuotePrefix();
}
return $this->quotePrefix;
}
/**
* Set quote prefix.
*
* @param bool $quotePrefix
*
* @return $this
*/
public function setQuotePrefix($quotePrefix)
{
if ($quotePrefix == '') {
$quotePrefix = false;
}
if ($this->isSupervisor) {
$styleArray = ['quotePrefix' => $quotePrefix];
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->quotePrefix = (bool) $quotePrefix;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
return md5(
$this->fill->getHashCode() .
$this->font->getHashCode() .
$this->borders->getHashCode() .
$this->alignment->getHashCode() .
$this->numberFormat->getHashCode() .
$this->protection->getHashCode() .
($this->quotePrefix ? 't' : 'f') .
__CLASS__
);
}
/**
* Get own index in style collection.
*
* @return int
*/
public function getIndex()
{
return $this->index;
}
/**
* Set own index in style collection.
*
* @param int $index
*/
public function setIndex($index): void
{
$this->index = $index;
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'alignment', $this->getAlignment());
$this->exportArray2($exportedArray, 'borders', $this->getBorders());
$this->exportArray2($exportedArray, 'fill', $this->getFill());
$this->exportArray2($exportedArray, 'font', $this->getFont());
$this->exportArray2($exportedArray, 'numberFormat', $this->getNumberFormat());
$this->exportArray2($exportedArray, 'protection', $this->getProtection());
$this->exportArray2($exportedArray, 'quotePrefx', $this->getQuotePrefix());
return $exportedArray;
}
}

View File

@@ -0,0 +1,158 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\IComparable;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
abstract class Supervisor implements IComparable
{
/**
* Supervisor?
*
* @var bool
*/
protected $isSupervisor;
/**
* Parent. Only used for supervisor.
*
* @var Spreadsheet|Style
*/
protected $parent;
/**
* Parent property name.
*
* @var null|string
*/
protected $parentPropertyName;
/**
* Create a new Supervisor.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false)
{
// Supervisor?
$this->isSupervisor = $isSupervisor;
}
/**
* Bind parent. Only used for supervisor.
*
* @param Spreadsheet|Style $parent
* @param null|string $parentPropertyName
*
* @return $this
*/
public function bindParent($parent, $parentPropertyName = null)
{
$this->parent = $parent;
$this->parentPropertyName = $parentPropertyName;
return $this;
}
/**
* Is this a supervisor or a cell style component?
*
* @return bool
*/
public function getIsSupervisor()
{
return $this->isSupervisor;
}
/**
* Get the currently active sheet. Only used for supervisor.
*
* @return Worksheet
*/
public function getActiveSheet()
{
return $this->parent->getActiveSheet();
}
/**
* Get the currently active cell coordinate in currently active sheet.
* Only used for supervisor.
*
* @return string E.g. 'A1'
*/
public function getSelectedCells()
{
return $this->getActiveSheet()->getSelectedCells();
}
/**
* Get the currently active cell coordinate in currently active sheet.
* Only used for supervisor.
*
* @return string E.g. 'A1'
*/
public function getActiveCell()
{
return $this->getActiveSheet()->getActiveCell();
}
/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$vars = get_object_vars($this);
foreach ($vars as $key => $value) {
if ((is_object($value)) && ($key != 'parent')) {
$this->$key = clone $value;
} else {
$this->$key = $value;
}
}
}
/**
* Export style as array.
*
* Available to anything which extends this class:
* Alignment, Border, Borders, Color, Fill, Font,
* NumberFormat, Protection, and Style.
*/
final public function exportArray(): array
{
return $this->exportArray1();
}
/**
* Abstract method to be implemented in anything which
* extends this class.
*
* This method invokes exportArray2 with the names and values
* of all properties to be included in output array,
* returning that array to exportArray, then to caller.
*/
abstract protected function exportArray1(): array;
/**
* Populate array from exportArray1.
* This method is available to anything which extends this class.
* The parameter index is the key to be added to the array.
* The parameter objOrValue is either a primitive type,
* which is the value added to the array,
* or a Style object to be recursively added via exportArray.
*
* @param mixed $objOrValue
*/
final protected function exportArray2(array &$exportedArray, string $index, $objOrValue): void
{
if ($objOrValue instanceof self) {
$exportedArray[$index] = $objOrValue->exportArray();
} else {
$exportedArray[$index] = $objOrValue;
}
}
}