Class: Labimotion::XlsxExporter::StyleManager
- Inherits:
-
Object
- Object
- Labimotion::XlsxExporter::StyleManager
- Defined in:
- lib/labimotion/libs/xlsx_exporter.rb
Overview
StyleManager Helper class to manage and create cell styles
Instance Method Summary collapse
-
#create_style(options = {}) ⇒ Axlsx::Style
Create or retrieve a cached style.
- #currency_style ⇒ Object
- #date_style ⇒ Object
- #datetime_style ⇒ Object
-
#header_style ⇒ Object
Common predefined styles.
-
#initialize(workbook) ⇒ StyleManager
constructor
A new instance of StyleManager.
- #percentage_style ⇒ Object
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
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( = {}) cache_key = .hash return @style_cache[cache_key] if @style_cache[cache_key] = {} # Font options = {} [:b] = [:bold] if .key?(:bold) [:i] = [:italic] if .key?(:italic) [:name] = [:font_name] if [:font_name] [:sz] = [:font_size] if [:font_size] # font_options[:color] = { rgb: options[:fg_color] } if options[:fg_color] [:font] = if .any? # Fill/background color if [:bg_color] [:bg_color] = [:bg_color] [:fg_color] = [:fg_color] end # Alignment [:alignment] = { horizontal: [:alignment] } if [:alignment] # Border [:border] = [:border] if [:border] # Number format [:format_code] = [:format_code] if [:format_code] @style_cache[cache_key] = @workbook.styles.add_style() end |
#currency_style ⇒ Object
268 269 270 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 268 def currency_style create_style(format_code: '$#,##0.00') end |
#date_style ⇒ Object
276 277 278 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 276 def date_style create_style(format_code: 'yyyy-mm-dd') end |
#datetime_style ⇒ Object
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_style ⇒ Object
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_style ⇒ Object
272 273 274 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 272 def percentage_style create_style(format_code: '0.00%') end |