Class: Axlsx::Styles
- Inherits:
-
Object
- Object
- Axlsx::Styles
- Defined in:
- lib/axlsx/stylesheet/styles.rb
Overview
The recommended way to manage styles is with add_style
The Styles class manages worksheet styles In addition to creating the require style objects for a valid xlsx package, this class provides the key mechanism for adding styles to your workbook, and safely applying them to the cells of your worksheet. All portions of the stylesheet are implemented here exception colors, which specify legacy and modified pallete colors, and exLst, whic is used as a future feature data storage area.
Instance Attribute Summary (collapse)
-
- (SimpleTypedList) borders
readonly
The collection of borders used in this workbook Axlsx predefines THIN_BORDER which can be used to put a border around all of your cells.
-
- (SimpleTypedList) cellStyles
readonly
The collection of named styles, referencing cellStyleXfs items in the workbook.
-
- (SimpleTypedList) cellStyleXfs
readonly
The collection of master formatting records for named cell styles, which means records defined in cellStyles, in the workbook.
-
- (SimpleTypedList) cellXfs
readonly
The collection of master formatting records.
-
- (SimpleTypedList) dxfs
readonly
The collection of non-cell formatting records used in the worksheet.
-
- (SimpleTypedList) fills
readonly
The collection of fills used in this workbook.
-
- (SimpleTypedList) fonts
readonly
The collection of fonts used in this workbook.
-
- (SimpleTypedList) numFmts
readonly
numFmts for your styles.
-
- (SimpleTypedList) tableStyles
readonly
The collection of table styles that will be available to the user in the excel UI.
Instance Method Summary (collapse)
-
- (Integer) add_style(options = {})
Drastically simplifies style creation and management.
-
- (Styles) initialize
constructor
Creates a new Styles object and prepopulates it with the requires objects to generate a valid package style part.
-
- (String) to_xml
Serializes the styles document.
Constructor Details
- (Styles) initialize
Creates a new Styles object and prepopulates it with the requires objects to generate a valid package style part.
118 119 120 |
# File 'lib/axlsx/stylesheet/styles.rb', line 118 def initialize() load_default_styles end |
Instance Attribute Details
- (SimpleTypedList) borders (readonly)
The recommended way to manage styles is with add_style
The collection of borders used in this workbook Axlsx predefines THIN_BORDER which can be used to put a border around all of your cells.
85 86 87 |
# File 'lib/axlsx/stylesheet/styles.rb', line 85 def borders @borders end |
- (SimpleTypedList) cellStyles (readonly)
The recommended way to manage styles is with add_style
The collection of named styles, referencing cellStyleXfs items in the workbook.
97 98 99 |
# File 'lib/axlsx/stylesheet/styles.rb', line 97 def cellStyles @cellStyles end |
- (SimpleTypedList) cellStyleXfs (readonly)
The recommended way to manage styles is with add_style
The collection of master formatting records for named cell styles, which means records defined in cellStyles, in the workbook
91 92 93 |
# File 'lib/axlsx/stylesheet/styles.rb', line 91 def cellStyleXfs @cellStyleXfs end |
- (SimpleTypedList) cellXfs (readonly)
The recommended way to manage styles is with add_style
The collection of master formatting records. This is the list that you will actually use in styling a workbook.
103 104 105 |
# File 'lib/axlsx/stylesheet/styles.rb', line 103 def cellXfs @cellXfs end |
- (SimpleTypedList) dxfs (readonly)
The recommended way to manage styles is with add_style
The collection of non-cell formatting records used in the worksheet.
109 110 111 |
# File 'lib/axlsx/stylesheet/styles.rb', line 109 def dxfs @dxfs end |
- (SimpleTypedList) fills (readonly)
The recommended way to manage styles is with add_style
The collection of fills used in this workbook
78 79 80 |
# File 'lib/axlsx/stylesheet/styles.rb', line 78 def fills @fills end |
- (SimpleTypedList) fonts (readonly)
The recommended way to manage styles is with add_style
The collection of fonts used in this workbook
72 73 74 |
# File 'lib/axlsx/stylesheet/styles.rb', line 72 def fonts @fonts end |
- (SimpleTypedList) numFmts (readonly)
The recommended way to manage styles is with add_style
numFmts for your styles.
The default styles, which change based on the system local, are as follows. id formatCode 0 General 1 0 2 0.00 3 #,##0 4 #,##0.00 9 0% 10 0.00% 11 0.00E+00 12 # ?/? 13 # ??/?? 14 mm-dd-yy 15 d-mmm-yy 16 d-mmm 17 mmm-yy 18 h:mm AM/PM 19 h:mm:ss AM/PM 20 h:mm 21 h:mm:ss 22 m/d/yy h:mm 37 #,##0 ;(#,##0) 38 #,##0 ;[Red](#,##0) 39 #,##0.00;(#,##0.00) 40 #,##0.00;[Red](#,##0.00) 45 mm:ss 46 [h]:mm:ss 47 mmss.0 48 ##0.0E+0 49 @ Axlsx also defines the following constants which you can use in add_style. NUM_FMT_PERCENT formats to "0%" NUM_FMT_YYYYMMDD formats to "yyyy/mm/dd" NUM_FMT_YYYYMMDDHHMMSS formats to "yyyy/mm/dd hh:mm:ss"
66 67 68 |
# File 'lib/axlsx/stylesheet/styles.rb', line 66 def numFmts @numFmts end |
- (SimpleTypedList) tableStyles (readonly)
The recommended way to manage styles is with add_style
The collection of table styles that will be available to the user in the excel UI
115 116 117 |
# File 'lib/axlsx/stylesheet/styles.rb', line 115 def tableStyles @tableStyles end |
Instance Method Details
- (Integer) add_style(options = {})
Drastically simplifies style creation and management.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/axlsx/stylesheet/styles.rb', line 191 def add_style(={}) numFmtId = if [:format_code] n = @numFmts.map{ |f| f.numFmtId }.max + 1 numFmts << NumFmt.new(:numFmtId => n, :formatCode=> [:format_code]) n else [:num_fmt] || 0 end borderId = [:border] || 0 raise ArgumentError, "Invalid borderId" unless borderId < borders.size fill = if [:bg_color] color = Color.new(:rgb=>[:bg_color]) pattern = PatternFill.new(:patternType =>:solid, :fgColor=>color) fills << Fill.new(pattern) else 0 end fontId = if (.values_at(:fg_color, :sz, :b, :i, :strike, :outline, :shadow, :charset, :family, :font_name).length) font = Font.new() [:b, :i, :strike, :outline, :shadow, :charset, :family, :sz].each { |k| font.send("#{k}=", [k]) unless [k].nil? } font.color = Color.new(:rgb => [:fg_color]) unless [:fg_color].nil? font.name = [:font_name] unless [:font_name].nil? fonts << font else 0 end applyProtection = ([:hidden] || [:locked]) ? 1 : 0 xf = Xf.new(:fillId => fill, :fontId=>fontId, :applyFill=>1, :applyFont=>1, :numFmtId=>numFmtId, :borderId=>borderId, :applyProtection=>applyProtection) if [:alignment] xf.alignment = CellAlignment.new([:alignment]) end if applyProtection xf.protection = CellProtection.new() end cellXfs << xf end |
- (String) to_xml
Serializes the styles document
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/axlsx/stylesheet/styles.rb', line 239 def to_xml() builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| xml.styleSheet(:xmlns => XML_NS) { [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :dxfs, :tableStyles].each do |key| self.instance_values[key.to_s].to_xml(xml) end } end builder.to_xml end |