Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Adding row attributes. #877

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Spout/Common/Entity/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

class Row
{
/**
* The extended attributes in this row
* @var string[]
*/
protected $attributes = [];

/**
* The cells in this row
* @var Cell[]
Expand All @@ -30,6 +36,26 @@ public function __construct(array $cells, $style)
->setStyle($style);
}

/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* @param string $name
* @param $value
* @return Row
*/
public function setAttribute(string $name, $value)
{
$this->attributes[$name] = (string) $value;

return $this;
}

/**
* @return Cell[] $cells
*/
Expand Down
21 changes: 20 additions & 1 deletion src/Spout/Writer/XLSX/Manager/WorksheetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ private function addNonEmptyRow(Worksheet $worksheet, Row $row)
$rowIndexOneBased = $worksheet->getLastWrittenRowIndex() + 1;
$numCells = $row->getNumCells();

$rowXML = '<row r="' . $rowIndexOneBased . '" spans="1:' . $numCells . '">';
$defaultAttributes = [
'r' => $rowIndexOneBased,
'spans' => '1:' . $numCells,
];

$rowXML = $this->getRowXML(array_merge($row->getAttributes(), $defaultAttributes));

foreach ($row->getCells() as $columnIndexZeroBased => $cell) {
$registeredStyle = $this->applyStyleAndRegister($cell, $rowStyle);
Expand Down Expand Up @@ -275,6 +280,20 @@ private function getCellXMLFragmentForNonEmptyString($cellValue)
return $cellXMLFragment;
}

/**
* @param array $attributes
* @return string
*/
private function getRowXML(array $attributes)
{
$attributes = array_map(function($key) use ($attributes)
{
return $key . '="' . $this->stringsEscaper->escape($attributes[$key]) . '"';
}, array_keys($attributes));

return '<row ' . implode(' ', $attributes) . '>';
}

/**
* {@inheritdoc}
*/
Expand Down