From 4d16bfc43780e5d3f7368625700b583e3e98217a Mon Sep 17 00:00:00 2001
From: Randy Morgan The recommended way to manage styles is with add_style
+The recommended way to manage styles is with add_style
+ The Styles class manages worksheet styles In addition to creating the
+
+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.
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.
++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. +
The collection of named styles, referencing cellStyleXfs items in the -workbook.
++The collection of named styles, referencing cellStyleXfs items in the +workbook. +
The collection of master formatting records for named cell styles, which -means records defined in cellStyles, in the workbook.
++The collection of master formatting records for named cell styles, which +means records defined in cellStyles, in the workbook. +
The collection of master formatting records.
++The collection of master formatting records. +
The collection of non-cell formatting records used in the worksheet.
++The collection of non-cell formatting records used in the worksheet. +
The collection of fills used in this workbook.
++The collection of fills used in this workbook. +
The collection of fonts used in this workbook.
++The collection of fonts used in this workbook. +
numFmts for your styles.
++numFmts for your styles. +
The collection of table styles that will be available to the user in the -excel UI.
++The collection of table styles that will be available to the user in the +excel UI. +
Drastically simplifies style creation and management.
++Drastically simplifies style creation and management. +
Creates a new Styles object and prepopulates it with the requires objects -to generate a valid package style part.
++Creates a new Styles object and prepopulates it with the requires objects +to generate a valid package style part. +
Serializes the styles document.
++Serializes the styles document. +
Creates a new Styles object and prepopulates it with the requires objects -to generate a valid package style part.
++Creates a new Styles object and prepopulates it with the requires objects +to generate a valid package style part. +
# File 'lib/axlsx/stylesheet/styles.rb', line 118 -def initialize() - load_default_styles -end+def initialize() + load_default_styles +end +
The recommended way to manage styles is with add_style
++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.
++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. +
# File 'lib/axlsx/stylesheet/styles.rb', line 85 -def borders - @borders -end+def borders + @borders +end +
The recommended way to manage styles is with add_style
++The recommended way to manage styles is with add_style +
The collection of named styles, referencing cellStyleXfs items in the -workbook.
++The collection of named styles, referencing cellStyleXfs items in the +workbook. +
# File 'lib/axlsx/stylesheet/styles.rb', line 97 -def cellStyles - @cellStyles -end+def cellStyles + @cellStyles +end +
The recommended way to manage styles is with add_style
++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
++The collection of master formatting records for named cell styles, which +means records defined in cellStyles, in the workbook +
# File 'lib/axlsx/stylesheet/styles.rb', line 91 -def cellStyleXfs - @cellStyleXfs -end+def cellStyleXfs + @cellStyleXfs +end +
The recommended way to manage styles is with add_style
++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.
++The collection of master formatting records. This is the list that you will +actually use in styling a workbook. +
# File 'lib/axlsx/stylesheet/styles.rb', line 103 -def cellXfs - @cellXfs -end+def cellXfs + @cellXfs +end +
The recommended way to manage styles is with add_style
++The recommended way to manage styles is with add_style +
The collection of non-cell formatting records used in the worksheet.
++The collection of non-cell formatting records used in the worksheet. +
# File 'lib/axlsx/stylesheet/styles.rb', line 109 -def dxfs - @dxfs -end+def dxfs + @dxfs +end +
The recommended way to manage styles is with add_style
++The recommended way to manage styles is with add_style +
The collection of fills used in this workbook
++The collection of fills used in this workbook +
# File 'lib/axlsx/stylesheet/styles.rb', line 78 -def fills - @fills -end+def fills + @fills +end +
The recommended way to manage styles is with add_style
++The recommended way to manage styles is with add_style +
The collection of fonts used in this workbook
++The collection of fonts used in this workbook +
# File 'lib/axlsx/stylesheet/styles.rb', line 72 -def fonts - @fonts -end+def fonts + @fonts +end +
The recommended way to manage styles is with add_style
++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"+
+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" +
# File 'lib/axlsx/stylesheet/styles.rb', line 66 -def numFmts - @numFmts -end+def numFmts + @numFmts +end +
The recommended way to manage styles is with add_style
++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
++The collection of table styles that will be available to the user in the +excel UI +
# File 'lib/axlsx/stylesheet/styles.rb', line 115 -def tableStyles - @tableStyles -end+def tableStyles + @tableStyles +end +
Drastically simplifies style creation and management.
++Drastically simplifies style creation and management. +
You Got Style
++You Got Style +
require "rubygems" # if that is your preferred way to manage gems! -require "axlsx" +-require "rubygems" # if that is your preferred way to manage gems! +require "axlsx" -p = Axlsx::Package.new -ws = p.workbook.add_worksheet +p = Axlsx::Package.new +ws = p.workbook.add_worksheet -# black text on a white background at 14pt with thin borders! -title = ws.style.add_style(:bg_color => "FFFF0000", :fg_color=>"#FF000000", :sz=>14, :border=>Axlsx::STYLE_THIN_BORDER +# black text on a white background at 14pt with thin borders! +title = ws.style.add_style(:bg_color => "FFFF0000", :fg_color=>"#FF000000", :sz=>14, :border=>Axlsx::STYLE_THIN_BORDER -ws.add_row :values => ["Least Popular Pets"] -ws.add_row :values => ["", "Dry Skinned Reptiles", "Bald Cats", "Violent Parrots"], :style=>title -ws.add_row :values => ["Votes", 6, 4, 1], :style=>Axlsx::STYLE_THIN_BORDER -f = File.open('example_you_got_style.xlsx', 'w') -p.serialize(f)+ws.add_row :values => ["Least Popular Pets"] +ws.add_row :values => ["", "Dry Skinned Reptiles", "Bald Cats", "Violent Parrots"], :style=>title +ws.add_row :values => ["Votes", 6, 4, 1], :style=>Axlsx::STYLE_THIN_BORDER +f = File.open('example_you_got_style.xlsx', 'w') +p.serialize(f) +
Styling specifically
++Styling specifically +
# an example of applying specific styles to specific cells -require "rubygems" # if that is your preferred way to manage gems! -require "axlsx" - -p = Axlsx::Package.new -ws = p.workbook.add_worksheet - -# define your styles -title = ws.style.add_style(:bg_color => "FFFF0000", - :fg_color=>"#FF000000", - :border=>Axlsx::STYLE_THIN_BORDER, - :alignment=>{:horizontal => :center}) - -date_time = ws.style.add_style(:num_fmt => Axlsx::NUM_FMT_YYYYMMDDHHMMSS, - :border=>Axlsx::STYLE_THIN_BORDER) - -percent = ws.style.add_style(:num_fmt => Axlsx::NUM_FMT_PERCENT, - :border=>Axlsx::STYLE_THIN_BORDER) - -currency = ws.style.add_style(:format_code=>"¥#,##0;[Red]¥-#,##0", - :border=>Axlsx::STYLE_THIN_BORDER) - -# build your rows -ws.add_row :values => ["Genreated At:", Time.now], :styles=>[nil, date_time] -ws.add_row :values => ["Previous Year Quarterly Profits (JPY)"], :style=>title -ws.add_row :values => ["Quarter", "Profit", "% of Total"], :style=>title -ws.add_row :values => ["Q1", 4000, 40], :style=>[title, currency, percent] -ws.add_row :values => ["Q2", 3000, 30], :style=>[title, currency, percent] -ws.add_row :values => ["Q3", 1000, 10], :style=>[title, currency, percent] -ws.add_row :values => ["Q4", 2000, 20], :style=>[title, currency, percent] -f = File.open('example_you_got_style.xlsx', 'w') -p.serialize(f)+
# an example of applying specific styles to specific cells +require "rubygems" # if that is your preferred way to manage gems! +require "axlsx" + +p = Axlsx::Package.new +ws = p.workbook.add_worksheet + +# define your styles +title = ws.style.add_style(:bg_color => "FFFF0000", + :fg_color=>"#FF000000", + :border=>Axlsx::STYLE_THIN_BORDER, + :alignment=>{:horizontal => :center}) + +date_time = ws.style.add_style(:num_fmt => Axlsx::NUM_FMT_YYYYMMDDHHMMSS, + :border=>Axlsx::STYLE_THIN_BORDER) + +percent = ws.style.add_style(:num_fmt => Axlsx::NUM_FMT_PERCENT, + :border=>Axlsx::STYLE_THIN_BORDER) + +currency = ws.style.add_style(:format_code=>"¥#,##0;[Red]¥-#,##0", + :border=>Axlsx::STYLE_THIN_BORDER) + +# build your rows +ws.add_row :values => ["Genreated At:", Time.now], :styles=>[nil, date_time] +ws.add_row :values => ["Previous Year Quarterly Profits (JPY)"], :style=>title +ws.add_row :values => ["Quarter", "Profit", "% of Total"], :style=>title +ws.add_row :values => ["Q1", 4000, 40], :style=>[title, currency, percent] +ws.add_row :values => ["Q2", 3000, 30], :style=>[title, currency, percent] +ws.add_row :values => ["Q3", 1000, 10], :style=>[title, currency, percent] +ws.add_row :values => ["Q4", 2000, 20], :style=>[title, currency, percent] +f = File.open('example_you_got_style.xlsx', 'w') +p.serialize(f) +
a customizable set of options
++a customizable set of options +
The text color
+ —+The text color +
The text size
+ —+The text size +
Indicates if the text should be bold
+ —+Indicates if the text should be bold +
Indicates if the text should be italicised
+ —+Indicates if the text should be italicised +
Indicates if the text should be rendered with a strikethrough
+ —+Indicates if the text should be rendered with a strikethrough +
Indicates if the text should be rendered with a shadow
+ —+Indicates if the text should be rendered with a shadow +
The character set to use.
+ —+The character set to use. +
The font family to use.
+ —+The font family to use. +
The name of the font to use
+ —+The name of the font to use +
The number format to apply
+ —+The number format to apply +
The formatting to apply. If this is specified, num_fmt is ignored.
+ —+The formatting to apply. If this is specified, num_fmt is ignored. +
The border style to use.
+ —+The border style to use. +
The background color to apply to the cell
+ —+The background color to apply to the cell +
Indicates if the cell should be hidden
+ —+Indicates if the cell should be hidden +
Indicates if the cell should be locked
+ —+Indicates if the cell should be locked +
A hash defining any of the attributes used in CellAlignment
+ —+A hash defining any of the attributes used in CellAlignment +
# 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+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 +
Serializes the styles document
++Serializes the styles document +
# 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, :cellStyles, :dxfs, :tableStyles].each do |key| - self.instance_values[key.to_s].to_xml(xml) - end - } - end - builder.to_xml -end+def to_xml() + builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml| + xml.styleSheet(:xmlns => XML_NS) { + [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :cellStyles, :dxfs, :tableStyles].each do |key| + self.instance_values[key.to_s].to_xml(xml) + end + } + end + builder.to_xml +end +