Apply multiple styles
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.
+1
+1
+1
would be very cool, we have to create n styles to mix up different requirements like bg-color, format etc. very nasty
+1
+1
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]
+1
+1
+1
+1
+1
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
3.3.1.3 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] ...>
.....
</c>
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)
indexes.map{ |i| cellXfs[i] }.reduce{ |combined, s| combined + s }
end
@randym 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 :+1:
@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.
+1
+1
I am in the process of adding this feature to the axlsx_styler gem.
If you want a solution without adding another library you could add the following monkey patch which allows a second argument for the add_styles method where you can pass in the cells style index.
module Axlsx
class Styles
# Patches the original Axlsx::Styles#add_style_method so that we can combine
# the existing cell style with the new one by adding the optional 'style_index' argument
def add_style(options={}, style_index=nil)
options[:type] ||= :xf # Default to :xf
raise ArgumentError, "Type must be one of [:xf, :dxf]" unless [:xf, :dxf].include?(options[:type])
fill = parse_fill_options(options)
font = parse_font_options(options)
numFmt = parse_num_fmt_options(options)
border = parse_border_options(options)
alignment = parse_alignment_options(options)
protection = parse_protection_options(options)
if options[:type] == :dxf
style = Dxf.new(fill: fill, font: font, numFmt: numFmt, border: border, alignment: alignment, protection: protection)
else
unless style_index.nil?
style_object = cellXfs[style_index]
unless font.nil?
font = parse_font_options(options, style_object.fontId)
end
fill ||= style_object.fillId
numFmt ||= style_object.numFmtId
border ||= style_object.borderId
alignment ||= style_object.alignment
protection ||= style_object.protection
end
style = Xf.new(fillId: (fill || 0), fontId: (font || 0), numFmtId: (numFmt || 0), borderId: (border || 0), alignment: alignment, protection: protection, applyFill: !fill.nil?, applyFont: !font.nil?, applyNumberFormat: !numFmt.nil?, applyBorder: !border.nil?, applyAlignment: !alignment.nil?, applyProtection: !protection.nil?)
end
if options[:type] == :xf
cellXfs << style
else
dxfs << style
end
end
# allow to pass in fontId instead of always defaulting first, also apply name and color before merging options
def parse_font_options(options={}, fontId=0)
return if (options.keys & [:fg_color, :sz, :b, :i, :u, :strike, :outline, :shadow, :charset, :family, :font_name]).empty?
options[:name] = options[:font_name] unless options[:font_name].nil?
options[:color] = Color.new(rgb: options[:fg_color]) unless options[:fg_color].nil?
options = fonts[fontId].instance_values.merge(options)
font = Font.new(options)
if options[:type] == :dxf
font
else
fonts << font
end
end
end
end
You can then use this as follows for example on the first cell of the worksheet.
merged_style = workbook.styles.add_style({b: true}, worksheet.cells[0].style)
worksheet.cells[0].style = merged_style