Skip to content

Commit bd0ecc8

Browse files
committed
Issue #12 Implement styles sheet property
1 parent 1ab2bb4 commit bd0ecc8

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

README.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ Property | Description
6969
`types` (optional) | An array of types for specific columns as supported by PHPOffice, e.g. `\PHPExcel_Cell_DataType::TYPE_STRING`, indexed either by column name (e.g. `H`) or 0-based column index.
7070
`formats` (optional) | An array of format strings for specific columns as supported by Excel, e.g. `#,##0.00`, indexed either by column name (e.g. `H`) or 0-based column index.
7171
`formatters` (optional) | An array of value formatters for specific columns. Each must be a valid PHP callable whith the signature `function formatter($value, $row, $data)` where `$value` is the cell value to format, `$row` is the 0-based row index and `$data` is the current row data from the `data` configuration. The callbacks must be indexed either by column name (e.g. `H`) or by the 0-based column index.
72-
`callbacks` (optional) | An array of callbacks for specific columns that should be called after rendering a cell, e.g. to apply some styling. Each must be a valid PHP callable with the signature `function callback($cell, $col, $row)` where `$cell` is the current `PHPExcel_Cell` object and `$col` and `$row` are the 0-based column and row indices respectively.
72+
`styles` (optional) | An array of style configuration indexed by cell coordinates or a range.
73+
`callbacks` (optional) | An array of callbacks indexed by column that should be called after rendering a cell, e.g. to apply further complex styling. Each must be a valid PHP callable with the signature `function callback($cell, $col, $row)` where `$cell` is the current `PHPExcel_Cell` object and `$col` and `$row` are the 0-based column and row indices respectively.
7374

7475
### ActiveExcelSheet
7576

@@ -193,9 +194,9 @@ $file->send('demo.xlsx');
193194

194195
### Styling
195196

196-
As you have access to the `PHPExcel` object you can modify the excel file as you like.
197-
For details please consult the
198-
[PHPExcel documentation](https://github.com/PHPOffice/PHPExcel/tree/develop/Documentation/markdown/Overview).
197+
Since version 2.3.0 you can style single cells and cell ranges via the `styles`
198+
property of a sheet. For details on the accepted styling format please consult the
199+
[PHPExcel documentation](https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/08-Recipes.md#styles).
199200

200201
```php
201202
<?php
@@ -205,15 +206,39 @@ $file = \Yii::createObject([
205206
'Users' => [
206207
'class' => 'codemix\excelexport\ActiveExcelSheet',
207208
'query' => User::find(),
209+
'styles' => [
210+
'A1:Z1000' => [
211+
'font' => [
212+
'bold' => true,
213+
'color' => ['rgb' => 'FF0000'],
214+
'size' => 15,
215+
'name' => 'Verdana'
216+
]
217+
'alignment' => [
218+
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
219+
],
220+
],
221+
],
208222
]
209223
]
210224
]);
211-
$phpExcel = $file->getWorkbook();
212-
$phpExcel->getSheet(1)->getStyle('B1')
213-
->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
214225
```
215226

216-
Alternatively you can use the callback feature from our `ExcelSheet`:
227+
As you have access to the `PHPExcel` object you can also "manually" modify the excel file as you like.
228+
229+
230+
```php
231+
<?php
232+
$file
233+
->getWorkbook();
234+
->getSheet(1)
235+
->getStyle('B1')
236+
->getFont()
237+
->getColor()
238+
->setARGB(\PHPExcel_Style_Color::COLOR_RED);
239+
```
240+
241+
Alternatively you can also use the callback feature from our `ExcelSheet`:
217242

218243
```php
219244
<?php
@@ -254,4 +279,5 @@ $file = \Yii::createObject([
254279
],
255280
],
256281
]);
282+
```
257283

src/ExcelSheet.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ExcelSheet extends Object
1414
protected $_types;
1515
protected $_formats;
1616
protected $_formatters;
17+
protected $_styles = [];
1718
protected $_callbacks;
1819
protected $_row = 1;
1920

@@ -61,7 +62,7 @@ public function getTitles()
6162
}
6263

6364
/**
64-
* @param string[]|null|false $value the column titles indexed by 0-based
65+
* @param string[]|null|false $value the column titles indexed by 1-based
6566
* column index. If empty or `false`, no titles will be generated.
6667
*/
6768
public function setTitles($value)
@@ -127,6 +128,24 @@ public function setFormatters($value)
127128
$this->_formatters = $value;
128129
}
129130

131+
/**
132+
* @return array style configuration arrays indexed by cell coordinate or
133+
* cell range, e.g. `A1:Z1000`.
134+
*/
135+
public function getStyles()
136+
{
137+
return $this->_styles;
138+
}
139+
140+
/**
141+
* @param array $value style configuration arrays indexed by cell
142+
* coordinate or cell range, e.g. `A1:Z1000`.
143+
*/
144+
public function setStyles($value)
145+
{
146+
$this->_styles = $value;
147+
}
148+
130149
/**
131150
* @return Callable[]|null column callbacks indexed by 0-based column index
132151
* that get called after rendering a cell. The function signature is
@@ -152,10 +171,21 @@ public function setCallbacks($value)
152171
*/
153172
public function render()
154173
{
174+
$this->renderStyles();
155175
$this->renderTitle();
156176
$this->renderRows();
157177
}
158178

179+
/**
180+
* Render styles
181+
*/
182+
protected function renderStyles()
183+
{
184+
foreach ($this->getStyles() as $i => $style) {
185+
$this->_sheet->getStyle($i)->applyFromArray($style);
186+
}
187+
}
188+
159189
/**
160190
* Render the title row if any
161191
*/

0 commit comments

Comments
 (0)