Apply multiple styles #26

kleine2 opened this Issue Feb 15, 2012 · 18 comments

A way to apply multiple styles to the same cell. To encourage style reuse and not duplication when there are several orthangonal styles which need to be applied. My example was I wanted a yellow background to highlight rows which were updated since the last report. This is in addition to any other styles that are applied to all the rows.

Also would be ideal if the styling could be in a separate file (maybe a view)? Kind of like css.








would be very cool, we have to create n styles to mix up different requirements like bg-color, format etc. very nasty


CFP: what kind of syntax are we looking for? Send in examples of how YOU want to define styles.


Probably following syntax
sheet.rows.last.cells[0].style = [style1, style2]








I digged a little into it and it seems that the Office Open XML Format does not support
applying multiple styles on a cell, so this job would be up to ruby c(Cell)
s (Style Index)
The index of this cell's style. Style records are stored in the Styles Part.
The possible values for this attribute are defined by the XML Schema unsignedInt datatype.

<c s=[style_index] ...>

So the proposed Syntax above would be very hard to implement

 sheet.rows.last.cells[0].style = [style1, style2]

And it also doesn't fit into the add_row api

#apply mutliple cell styles on a row
sheet.add_row([value_1, value_2, value_3], [style_id_1, style_id_2, style_id_3])
#apply one style on a row
sheet.add_row([value_1, value_2, value_3], style_id)

It already differentiates between an Arry and Integer Parameter.

I would suggest the following:

We should introduce style merging/combination

@bold                   = @workbook.styles.add_style  :b => true
@money               = @workbook.styles.add_style  :format_code => '#,###,##0.00'

@bold_money      = @workbook.styles.add_styles [@bold, @money]
#or in case of dxfs styles
@workbook.styles.add_styles [@bold, @money], :dxfs

This could be more or less easily implemented by making all the XML Node
style Objects mergable

def add_styles(indexes){ |i| cellXfs[i] }.reduce{ |combined, s| combined + s }

Gues i could provide a pull request by next month, if you agree to the general strategy


@randym @dre-hh

There any preview for that feature? Add multiple styles in that way would turn everything much easier 👍


@dre-hh @randym Is this feature available now?


Nope. The above solution was just a suggestion from me. Unfortionately nobody provided feedback.

Actuallly i had another couple of thoughts on my solution and the benefit of it is marginal as styles are configured by the set of hashed options. You can keep the options as separate variables and merge them by yourself.

@bold_money_options = @bold_options.merge @money_options
@workbook.styles.add_styles @bold_money_options

The real benefit would be the Open XML Format of Excel definiting multiple Style references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment