4
4
5
5
use Box \Spout \Common \Entity \Style \BorderPart ;
6
6
use Box \Spout \Common \Entity \Style \CellAlignment ;
7
+ use Box \Spout \Common \Manager \OptionsManagerInterface ;
8
+ use Box \Spout \Writer \Common \Entity \Options ;
7
9
use Box \Spout \Writer \Common \Entity \Worksheet ;
10
+ use Box \Spout \Writer \Common \Manager \ManagesCellSize ;
8
11
use Box \Spout \Writer \ODS \Helper \BorderHelper ;
9
12
10
13
/**
13
16
*/
14
17
class StyleManager extends \Box \Spout \Writer \Common \Manager \Style \StyleManager
15
18
{
19
+ use ManagesCellSize;
20
+
16
21
/** @var StyleRegistry */
17
22
protected $ styleRegistry ;
18
23
24
+ /**
25
+ * @param StyleRegistry $styleRegistry
26
+ */
27
+ public function __construct (StyleRegistry $ styleRegistry , OptionsManagerInterface $ optionsManager )
28
+ {
29
+ parent ::__construct ($ styleRegistry );
30
+ $ this ->setDefaultColumnWidth ($ optionsManager ->getOption (Options::DEFAULT_COLUMN_WIDTH ));
31
+ $ this ->setDefaultRowHeight ($ optionsManager ->getOption (Options::DEFAULT_ROW_HEIGHT ));
32
+ $ this ->columnWidths = $ optionsManager ->getOption (Options::COLUMN_WIDTHS ) ?? [];
33
+ }
34
+
19
35
/**
20
36
* Returns the content of the "styles.xml" file, given a list of styles.
21
37
*
@@ -162,12 +178,16 @@ public function getContentXmlAutomaticStylesSectionContent($worksheets)
162
178
$ content .= $ this ->getStyleSectionContent ($ style );
163
179
}
164
180
165
- $ content .= <<<'EOD'
166
- <style:style style:family="table-column" style:name="co1">
167
- <style:table-column-properties fo:break-before="auto"/>
181
+ $ useOptimalRowHeight = empty ($ this ->defaultRowHeight ) ? 'true ' : 'false ' ;
182
+ $ defaultRowHeight = empty ($ this ->defaultRowHeight ) ? '15pt ' : "{$ this ->defaultRowHeight }pt " ;
183
+ $ defaultColumnWidth = empty ($ this ->defaultColumnWidth ) ? '' : "style:column-width= \"{$ this ->defaultColumnWidth }pt \"" ;
184
+
185
+ $ content .= <<<EOD
186
+ <style:style style:family="table-column" style:name="default-column-style">
187
+ <style:table-column-properties fo:break-before="auto" {$ defaultColumnWidth }/>
168
188
</style:style>
169
189
<style:style style:family="table-row" style:name="ro1">
170
- <style:table-row-properties fo:break-before="auto" style:row-height="15pt " style:use-optimal-row-height="true "/>
190
+ <style:table-row-properties fo:break-before="auto" style:row-height=" { $ defaultRowHeight } " style:use-optimal-row-height=" { $ useOptimalRowHeight } "/>
171
191
</style:style>
172
192
EOD ;
173
193
@@ -182,6 +202,16 @@ public function getContentXmlAutomaticStylesSectionContent($worksheets)
182
202
EOD ;
183
203
}
184
204
205
+ // Sort column widths since ODS cares about order
206
+ usort ($ this ->columnWidths , function ($ a , $ b ) {
207
+ if ($ a [0 ] === $ b [0 ]) {
208
+ return 0 ;
209
+ }
210
+
211
+ return ($ a [0 ] < $ b [0 ]) ? -1 : 1 ;
212
+ });
213
+ $ content .= $ this ->getTableColumnStylesXMLContent ();
214
+
185
215
$ content .= '</office:automatic-styles> ' ;
186
216
187
217
return $ content ;
@@ -381,4 +411,42 @@ private function getBackgroundColorXMLContent($style)
381
411
{
382
412
return \sprintf (' fo:background-color="#%s" ' , $ style ->getBackgroundColor ());
383
413
}
414
+
415
+ public function getTableColumnStylesXMLContent () : string
416
+ {
417
+ if (empty ($ this ->columnWidths )) {
418
+ return '' ;
419
+ }
420
+
421
+ $ content = '' ;
422
+ foreach ($ this ->columnWidths as $ styleIndex => $ entry ) {
423
+ $ content .= <<<EOD
424
+ <style:style style:family="table-column" style:name="co {$ styleIndex }">
425
+ <style:table-column-properties fo:break-before="auto" style:use-optimal-column-width="false" style:column-width=" {$ entry [2 ]}pt"/>
426
+ </style:style>
427
+ EOD ;
428
+ }
429
+
430
+ return $ content ;
431
+ }
432
+
433
+ public function getStyledTableColumnXMLContent (int $ maxNumColumns ) : string
434
+ {
435
+ if (empty ($ this ->columnWidths )) {
436
+ return '' ;
437
+ }
438
+
439
+ $ content = '' ;
440
+ foreach ($ this ->columnWidths as $ styleIndex => $ entry ) {
441
+ $ numCols = $ entry [1 ] - $ entry [0 ] + 1 ;
442
+ $ content .= <<<EOD
443
+ <table:table-column table:default-cell-style-name='Default' table:style-name="co {$ styleIndex }" table:number-columns-repeated=" {$ numCols }"/>
444
+ EOD ;
445
+ }
446
+ // Note: This assumes the column widths are contiguous and default width is
447
+ // only applied to columns after the last custom column with a custom width
448
+ $ content .= '<table:table-column table:default-cell-style-name="ce1" table:style-name="default-column-style" table:number-columns-repeated=" ' . ($ maxNumColumns - $ entry [1 ]) . '"/> ' ;
449
+
450
+ return $ content ;
451
+ }
384
452
}
0 commit comments