1010use Box \Spout \Common \Helper \Escaper \ODS as ODSEscaper ;
1111use Box \Spout \Common \Helper \StringHelper ;
1212use Box \Spout \Writer \Common \Entity \Worksheet ;
13+ use Box \Spout \Writer \Common \Manager \RegisteredStyle ;
1314use Box \Spout \Writer \Common \Manager \Style \StyleMerger ;
1415use Box \Spout \Writer \Common \Manager \WorksheetManagerInterface ;
1516use Box \Spout \Writer \ODS \Manager \Style \StyleManager ;
@@ -93,7 +94,7 @@ public function getTableElementStartAsString(Worksheet $worksheet)
9394 $ escapedSheetName = $ this ->stringsEscaper ->escape ($ externalSheet ->getName ());
9495 $ tableStyleName = 'ta ' . ($ externalSheet ->getIndex () + 1 );
9596
96- $ tableElement = '<table:table table:style-name=" ' . $ tableStyleName . '" table:name=" ' . $ escapedSheetName . '"> ' ;
97+ $ tableElement = '<table:table table:style-name=" ' . $ tableStyleName . '" table:name=" ' . $ escapedSheetName . '"> ' ;
9798 $ tableElement .= '<table:table-column table:default-cell-style-name="ce1" table:style-name="co1" table:number-columns-repeated=" ' . $ worksheet ->getMaxNumColumns () . '"/> ' ;
9899
99100 return $ tableElement ;
@@ -104,8 +105,8 @@ public function getTableElementStartAsString(Worksheet $worksheet)
104105 *
105106 * @param Worksheet $worksheet The worksheet to add the row to
106107 * @param Row $row The row to be added
107- * @throws IOException If the data cannot be written
108108 * @throws InvalidArgumentException If a cell value's type is not supported
109+ * @throws IOException If the data cannot be written
109110 * @return void
110111 */
111112 public function addRow (Worksheet $ worksheet , Row $ row )
@@ -125,7 +126,13 @@ public function addRow(Worksheet $worksheet, Row $row)
125126 $ nextCell = isset ($ cells [$ nextCellIndex ]) ? $ cells [$ nextCellIndex ] : null ;
126127
127128 if ($ nextCell === null || $ cell ->getValue () !== $ nextCell ->getValue ()) {
128- $ data .= $ this ->applyStyleAndGetCellXML ($ cell , $ rowStyle , $ currentCellIndex , $ nextCellIndex );
129+ $ registeredStyle = $ this ->applyStyleAndRegister ($ cell , $ rowStyle );
130+ $ cellStyle = $ registeredStyle ->getStyle ();
131+ if ($ registeredStyle ->isRowStyle ()) {
132+ $ rowStyle = $ cellStyle ;
133+ }
134+
135+ $ data .= $ this ->getCellXMLWithStyle ($ cell , $ cellStyle , $ currentCellIndex , $ nextCellIndex );
129136 $ currentCellIndex = $ nextCellIndex ;
130137 }
131138
@@ -146,17 +153,15 @@ public function addRow(Worksheet $worksheet, Row $row)
146153
147154 /**
148155 * Applies styles to the given style, merging the cell's style with its row's style
149- * Then builds and returns xml for the cell.
150156 *
151157 * @param Cell $cell
152158 * @param Style $rowStyle
153- * @param int $currentCellIndex
154- * @param int $nextCellIndex
155159 * @throws InvalidArgumentException If a cell value's type is not supported
156- * @return string
160+ * @return RegisteredStyle
157161 */
158- private function applyStyleAndGetCellXML (Cell $ cell , Style & $ rowStyle, $ currentCellIndex , $ nextCellIndex )
162+ private function applyStyleAndRegister (Cell $ cell , Style $ rowStyle) : RegisteredStyle
159163 {
164+ $ isRowStyle = false ;
160165 if ($ cell ->getStyle ()->isEmpty ()) {
161166 $ cell ->setStyle ($ rowStyle );
162167
@@ -166,7 +171,7 @@ private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $currentC
166171 $ registeredStyle = $ this ->styleManager ->registerStyle ($ managedStyle ->getStyle ());
167172 } else {
168173 $ registeredStyle = $ this ->styleManager ->registerStyle ($ rowStyle );
169- $ rowStyle = $ registeredStyle ;
174+ $ isRowStyle = true ;
170175 }
171176 } else {
172177 $ mergedCellAndRowStyle = $ this ->styleMerger ->merge ($ cell ->getStyle (), $ rowStyle );
@@ -182,7 +187,12 @@ private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $currentC
182187 $ registeredStyle = $ this ->styleManager ->registerStyle ($ newCellStyle );
183188 }
184189
185- $ styleIndex = $ registeredStyle ->getId () + 1 ; // 1-based
190+ return new RegisteredStyle ($ registeredStyle , $ isRowStyle );
191+ }
192+
193+ private function getCellXMLWithStyle (Cell $ cell , Style $ style , int $ currentCellIndex , int $ nextCellIndex ) : string
194+ {
195+ $ styleIndex = $ style ->getId () + 1 ; // 1-based
186196
187197 $ numTimesValueRepeated = ($ nextCellIndex - $ currentCellIndex );
188198
0 commit comments