Added alignment to styles

This commit is contained in:
Freek van der Veer 2016-06-06 16:22:06 +02:00
parent 3b03a9cc24
commit eda5e3bd11
4 changed files with 59 additions and 3 deletions

0
src/Spout/Writer/AbstractWriter.php Normal file → Executable file
View File

37
src/Spout/Writer/Style/Style.php Normal file → Executable file
View File

@ -56,6 +56,15 @@ class Style
/** @var bool Whether specific font properties should be applied */
protected $shouldApplyFont = false;
protected $verticalAlignment = 'center';
protected $horizontalAlignment = 'center';
protected $hasVerticalAlignment = false;
protected $hasHorizontalAlignment = false;
protected $shouldApplyVerticalAlignment = false;
protected $shouldApplyHorizontalAlignment = false;
/** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */
protected $shouldWrapText = false;
/** @var bool Whether the wrap text property was set */
@ -278,6 +287,34 @@ class Style
return $this->numberFormat;
}
public function setVerticalAlignment($alignment) {
$this->verticalAlignment = $alignment;
$this->hasVerticalAlignment = true;
$this->shouldApplyVerticalAlignment = true;
}
public function setHorizontalAlignment($alignment) {
$this->horizontalAlignment = $alignment;
$this->hasHorizontalAlignment = true;
$this->shouldApplyHorizontalAlignment = true;
}
public function getVerticalAlignment() {
return $this->verticalAlignment;
}
public function getHorizontalAlignment() {
return $this->horizontalAlignment;
}
public function shouldApplyVerticalAlignment() {
return $this->shouldApplyVerticalAlignment;
}
public function shouldApplyHorizontalAlignment() {
return $this->shouldApplyHorizontalAlignment;
}
/**
* Serializes the style for future comparison with other styles.

10
src/Spout/Writer/Style/StyleBuilder.php Normal file → Executable file
View File

@ -127,6 +127,16 @@ class StyleBuilder
return $this;
}
public function setVerticalAlignment($alignment) {
$this->style->setVerticalAlignment($alignment);
return $this;
}
public function setHorizontalAlignment($alignment) {
$this->style->setHorizontalAlignment($alignment);
return $this;
}
/**
* Returns the configured style. The style is cached and can be reused.
*

15
src/Spout/Writer/XLSX/Helper/StyleHelper.php Normal file → Executable file
View File

@ -175,10 +175,19 @@ EOD;
$content .= ' applyFont="1"';
}
if ($style->shouldWrapText()) {
if ($style->shouldWrapText() || $style->shouldApplyVerticalAlignment() || $style->shouldApplyHorizontalAlignment()) {
$content .= ' applyAlignment="1">';
$content .= '<alignment wrapText="1"/>';
$content .= '</xf>';
$content .= '<alignment ';
if ($style->shouldWrapText()) {
$content .= 'wrapText="1" ';
}
if ($style->shouldApplyVerticalAlignment()) {
$content .= 'vertical="' . $style->getVerticalAlignment() . '" ';
}
if ($style->shouldApplyHorizontalAlignment()) {
$content .= 'horizontal="' . $style->getHorizontalAlignment() . '" ';
}
$content .= '/></xf>';
} else {
$content .= '/>';
}