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
-
#borders ⇒ SimpleTypedList
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.
-
#cellStyles ⇒ SimpleTypedList
readonly
The collection of named styles, referencing cellStyleXfs items in the workbook.
-
#cellStyleXfs ⇒ SimpleTypedList
readonly
The collection of master formatting records for named cell styles, which means records defined in cellStyles, in the workbook.
-
#cellXfs ⇒ SimpleTypedList
readonly
The collection of master formatting records.
-
#dxfs ⇒ SimpleTypedList
readonly
The collection of non-cell formatting records used in the worksheet.
-
#fills ⇒ SimpleTypedList
readonly
The collection of fills used in this workbook.
-
#fonts ⇒ SimpleTypedList
readonly
The collection of fonts used in this workbook.
-
#numFmts ⇒ SimpleTypedList
readonly
numFmts for your styles.
-
#tableStyles ⇒ SimpleTypedList
readonly
The collection of table styles that will be available to the user in the excel UI.
Instance Method Summary collapse
-
#add_style(options = {}) ⇒ Integer
Drastically simplifies style creation and management.
-
#initialize ⇒ Styles
constructor
Creates a new Styles object and prepopulates it with the requires objects to generate a valid package style part.
-
#to_xml_string(str = '') ⇒ String
Serializes the object.
Constructor Details
#initialize ⇒ Styles
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
#borders ⇒ SimpleTypedList (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 |
#cellStyles ⇒ SimpleTypedList (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 |
#cellStyleXfs ⇒ SimpleTypedList (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 |
#cellXfs ⇒ SimpleTypedList (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 |
#dxfs ⇒ SimpleTypedList (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 |
#fills ⇒ SimpleTypedList (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 |
#fonts ⇒ SimpleTypedList (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 |
#numFmts ⇒ SimpleTypedList (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 |
#tableStyles ⇒ SimpleTypedList (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
#add_style(options = {}) ⇒ Integer
Drastically simplifies style creation and management.
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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/axlsx/stylesheet/styles.rb', line 192 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 if borderId.is_a?(Hash) raise ArgumentError, "border hash definitions must include both style and color" unless borderId.keys.include?(:style) && borderId.keys.include?(:color) s = borderId.delete :style c = borderId.delete :color border = Border.new [:left, :right, :top, :bottom].each {|pr| border.prs << BorderPr.new(:name => pr, :style=>s, :color => Color.new(:rgb => c))} borderId = self.borders << border end 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, :u, :strike, :outline, :shadow, :charset, :family, :font_name).length) font = Font.new() [:b, :i, :u, :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) xf.applyNumberFormat = true if xf.numFmtId > 0 xf.applyBorder = true if borderId > 0 if [:alignment] xf.alignment = CellAlignment.new([:alignment]) xf.applyAlignment = true end if applyProtection xf.protection = CellProtection.new() end cellXfs << xf end |
#to_xml_string(str = '') ⇒ String
Serializes the object
256 257 258 259 260 261 262 |
# File 'lib/axlsx/stylesheet/styles.rb', line 256 def to_xml_string(str = '') str << '<styleSheet xmlns="' << XML_NS << '">' [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :cellStyles, :dxfs, :tableStyles].each do |key| self.instance_values[key.to_s].to_xml_string(str) unless self.instance_values[key.to_s].nil? end str << '</styleSheet>' end |