Class: Labimotion::XlsxExporter::StyleManager

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/libs/xlsx_exporter.rb

Overview

StyleManager Helper class to manage and create cell styles

Instance Method Summary collapse

Constructor Details

#initialize(workbook) ⇒ StyleManager

Returns a new instance of StyleManager.



213
214
215
216
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 213

def initialize(workbook)
  @workbook = workbook
  @style_cache = {}
end

Instance Method Details

#create_style(options = {}) ⇒ Axlsx::Style

Create or retrieve a cached style

Parameters:

  • options (Hash) (defaults to: {})

    style options

Options Hash (options):

  • :bold (Boolean)
  • :italic (Boolean)
  • :font_name (String)
  • :font_size (Integer)
  • :fg_color (String)

    foreground/text color

  • :bg_color (String)

    background color

  • :alignment (Symbol) — default: :left, :center, :right
  • :border (Hash)

    border options

  • :format_code (String)

    number format

Returns:

  • (Axlsx::Style)

    the created or cached style



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
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 230

def create_style(options = {})
  cache_key = options.hash
  return @style_cache[cache_key] if @style_cache[cache_key]

  style_options = {}

  # Font options
  font_options = {}
  font_options[:b] = options[:bold] if options.key?(:bold)
  font_options[:i] = options[:italic] if options.key?(:italic)
  font_options[:name] = options[:font_name] if options[:font_name]
  font_options[:sz] = options[:font_size] if options[:font_size]
  # font_options[:color] = { rgb: options[:fg_color] } if options[:fg_color]
  style_options[:font] = font_options if font_options.any?

  # Fill/background color
  if options[:bg_color]
    style_options[:bg_color] = options[:bg_color]
    style_options[:fg_color] = options[:fg_color]
  end

  # Alignment
  style_options[:alignment] = { horizontal: options[:alignment] } if options[:alignment]

  # Border
  style_options[:border] = options[:border] if options[:border]

  # Number format
  style_options[:format_code] = options[:format_code] if options[:format_code]

  @style_cache[cache_key] = @workbook.styles.add_style(style_options)
end

#currency_styleObject



268
269
270
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 268

def currency_style
  create_style(format_code: '$#,##0.00')
end

#date_styleObject



276
277
278
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 276

def date_style
  create_style(format_code: 'yyyy-mm-dd')
end

#datetime_styleObject



280
281
282
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 280

def datetime_style
  create_style(format_code: 'yyyy-mm-dd hh:mm:ss')
end

#header_styleObject

Common predefined styles



264
265
266
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 264

def header_style
  create_style(bold: true, bg_color: 'DDDDDD', alignment: :center)
end

#percentage_styleObject



272
273
274
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 272

def percentage_style
  create_style(format_code: '0.00%')
end