Method: Axlsx::Styles#add_style
- Defined in:
- lib/axlsx/stylesheet/styles.rb
#add_style(options = {}) ⇒ Integer
Drastically simplifies style creation and management.
An index for cell styles where keys are styles codes as per Axlsx::Style and values are Cell#raw_style
The reason for the backward key/value ordering is that style lookup must be most efficient, while add_style
can be less efficient
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/axlsx/stylesheet/styles.rb', line 227 def add_style( = {}) # Default to :xf [:type] ||= :xf raise ArgumentError, "Type must be one of [:xf, :dxf]" unless [:xf, :dxf].include?([:type]) if [:border].is_a?(Hash) if [:border][:edges] == :all [:border][:edges] = Axlsx::Border::EDGES elsif [:border][:edges] [:border][:edges] = [:border][:edges].map(&:to_sym) ### normalize for style caching end end if [:type] == :xf # Check to see if style in cache already font_defaults = { name: @fonts.first.name, sz: @fonts.first.sz, family: @fonts.first.family } raw_style = { type: :xf }.merge(font_defaults).merge() if raw_style[:format_code] raw_style.delete(:num_fmt) end xf_index = style_index.key(raw_style) if xf_index return xf_index end end fill = font = numFmt = border = alignment = protection = case [:type] when :dxf style = Dxf.new :fill => fill, :font => font, :numFmt => numFmt, :border => border, :alignment => alignment, :protection => protection else 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 [:type] == :xf xf_index = (cellXfs << style) # Add styles to style_index cache for re-use style_index[xf_index] = raw_style return xf_index else dxf_index = (dxfs << style) return dxf_index end end |