Skip to content

Commit

Permalink
Fix #276: Ability to configure explicit cell formats (header, footer,…
Browse files Browse the repository at this point in the history
… content, before, after)
  • Loading branch information
kartik-v committed Dec 19, 2018
1 parent ad58747 commit 3b1cc51
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
10 changes: 9 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ Change Log: `yii2-export`

## version 1.3.9

**Date:** _under development_
**Date:** 19-Dec-2018

- (enh #288): Correct export column selection when `asDropdown` is `false`.
- (enh #276): Ability to configure explicit cell formats (header, footer, content, before, after).
Explicit cell formats must be one of the `PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_` constants.
This can be set via `cellFormat` settings at one or more of the following levels.
- `Column::headerOptions['cellFormat']` within `columns` array items.
- `Column::contentOptions['cellFormat']` within `columns` array items.
- `Column::footerOptions['cellFormat']` within `columns` array items.
- The `cellFormat` setting for each array item within `ExportMenu::contentBefore`
- The `cellFormat` setting for each array item within `ExportMenu::contentAfter`

## version 1.3.8

Expand Down
39 changes: 28 additions & 11 deletions src/ExportMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ class ExportMenu extends GridView
* @var array an array of rows to prepend in front of the grid used to create things like a title. Each array
* should be set with the following settings:
* - value: string, the value of the merged row
* - cellFormat: string|null, the explicit cell format to apply (should be one of the
* `PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_` constants)
* - styleOptions: array, array of configuration options to set the style. See $styleOptions on how to configure.
*/
public $contentBefore = [];
Expand All @@ -366,6 +368,8 @@ class ExportMenu extends GridView
* @var array an array of rows to append after the footer row. Each array
* should be set with the following settings:
* - value: string, the value of the merged row
* - cellFormat: string|null, the explicit cell format to apply (should be one of the
* `PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_` constants)
* - styleOptions: array, array of configuration options to set the style. See $styleOptions on how to configure.
*/
public $contentAfter = [];
Expand Down Expand Up @@ -1155,7 +1159,8 @@ public function generateBeforeContent()
$colFirst = self::columnName(1);
$sheet = $this->_objWorksheet;
foreach ($this->contentBefore as $contentBefore) {
$this->setOutCellValue($sheet, $colFirst . $this->_beginRow, $contentBefore['value']);
$format = ArrayHelper::getValue($contentBefore, 'cellFormat', null);
$this->setOutCellValue($sheet, $colFirst . $this->_beginRow, $contentBefore['value'], $format);
$opts = $this->getStyleOpts($contentBefore);
$sheet->getStyle($colFirst . $this->_beginRow)->applyFromArray($opts);
$this->_beginRow += 1;
Expand Down Expand Up @@ -1185,7 +1190,8 @@ public function generateHeader()
*/
$head = ($column instanceof DataColumn) ? $this->getColumnHeader($column) : $column->header;
$id = self::columnName($this->_endCol) . $this->_beginRow;
$cell = $this->setOutCellValue($sheet, $id, $head);
$format = ArrayHelper::remove($column->headerOptions, 'cellFormat', null);
$cell = $this->setOutCellValue($sheet, $id, $head, $format);
if (isset($column->hAlign) && !isset($opts['alignment']['horizontal'])) {
$opts['alignment']['horizontal'] = $column->hAlign;
}
Expand Down Expand Up @@ -1225,8 +1231,9 @@ public function setVisibleColumns()
$columns = [];
foreach ($this->columns as $key => $column) {
$isActionColumn = $column instanceof ActionColumn;
$isNoExport = in_array($key, $this->noExportColumns) ||
($this->showColumnSelector && is_array($this->selectedColumns) && !in_array($key, $this->selectedColumns));
$isNoExport = in_array($key, $this->noExportColumns) ||
($this->showColumnSelector && is_array($this->selectedColumns) && !in_array($key,
$this->selectedColumns));
if ($isActionColumn && !$isNoExport) {
$this->noExportColumns[] = $key;
}
Expand Down Expand Up @@ -1361,12 +1368,14 @@ public function generateRow($model, $key, $index)
} else {
$value = '';
}
$format = ArrayHelper::remove($column->contentOptions, 'cellFormat', null);
$cell = $this->setOutCellValue(
$this->_objWorksheet,
self::columnName($this->_endCol) . ($index + $this->_beginRow + 1),
$value
$value,
$format
);
if ($this->enableAutoFormat) {
if ($this->enableAutoFormat && $format === null) {
$this->autoFormat($model, $key, $index, $column, $cell);
}
$this->raiseEvent('onRenderDataCell', [$cell, $value, $model, $key, $index, $this]);
Expand All @@ -1393,10 +1402,12 @@ public function generateFooter()
if ($column->footer) {
$footerExists = true;
$footer = trim($column->footer) !== '' ? $column->footer : $column->grid->blankDisplay;
$format = ArrayHelper::remove($column->footerOptions, 'cellFormat', null);
$cell = $this->setOutCellValue(
$this->_objSpreadsheet->getActiveSheet(),
self::columnName($this->_endCol) . ($row + 1),
$footer
$footer,
$format
);
$this->raiseEvent('onRenderFooterCell', [$cell, $footer, $this]);
}
Expand All @@ -1420,7 +1431,8 @@ public function generateAfterContent($row)
$afterContentBeginRow = $row;
$sheet = $this->_objWorksheet;
foreach ($this->contentAfter as $contentAfter) {
$this->setOutCellValue($sheet, $colFirst . $row, $contentAfter['value']);
$format = ArrayHelper::getValue($contentAfter, 'cellFormat', null);
$this->setOutCellValue($sheet, $colFirst . $row, $contentAfter['value'], $format);
$opts = $this->getStyleOpts($contentAfter);
$sheet->getStyle($colFirst . $row)->applyFromArray($opts);
$row += 1;
Expand Down Expand Up @@ -2083,18 +2095,23 @@ protected function setHttpHeaders()
* @param Worksheet $sheet
* @param string $index coordinate of the cell, eg: 'A1'
* @param mixed $value value of the cell
*
* @param string|null $format the explicit cell format to apply (should be one of the
* `PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_` constants)
* @return Cell
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
protected function setOutCellValue($sheet, $index, $value)
protected function setOutCellValue($sheet, $index, $value, $format = null)
{
if ($this->stripHtml) {
$value = strip_tags($value);
}
$value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
$cell = $sheet->getCell($index);
$cell->setValue($value);
if ($format === null) {
$cell->setValue($value);
} else {
$cell->setValueExplicit($value, $format);
}
return $cell;
}

Expand Down

0 comments on commit 3b1cc51

Please sign in to comment.