lib/axlsx/stylesheet/styles.rb in axlsx-1.0.18 vs lib/axlsx/stylesheet/styles.rb in axlsx-1.1.0
- old
+ new
@@ -118,11 +118,11 @@
def initialize()
load_default_styles
end
# Drastically simplifies style creation and management.
- # @return [Integer]
+ # @return [Integer]
# @option options [String] fg_color The text color
# @option options [Integer] sz The text size
# @option options [Boolean] b Indicates if the text should be bold
# @option options [Boolean] i Indicates if the text should be italicised
# @option options [Boolean] u Indicates if the text should be underlined
@@ -131,26 +131,26 @@
# @option options [Integer] charset The character set to use.
# @option options [Integer] family The font family to use.
# @option options [String] font_name The name of the font to use
# @option options [Integer] num_fmt The number format to apply
# @option options [String] format_code The formatting to apply. If this is specified, num_fmt is ignored.
- # @option options [Integer] border The border style to use.
+ # @option options [Integer] border The border style to use.
# @option options [String] bg_color The background color to apply to the cell
# @option options [Boolean] hidden Indicates if the cell should be hidden
# @option options [Boolean] locked Indicates if the cell should be locked
# @option options [Hash] alignment A hash defining any of the attributes used in CellAlignment
# @see CellAlignment
- #
+ #
# @example You Got Style
# require "rubygems" # if that is your preferred way to manage gems!
# require "axlsx"
#
# 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
+ # title = ws.style.add_style(:bg_color => "FFFF0000", :fg_color=>"#FF000000", :sz=>14, :border=> {:style => :thin, :color => "FFFF0000"}
#
# 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')
@@ -163,19 +163,19 @@
#
# p = Axlsx::Package.new
# ws = p.workbook.add_worksheet
#
# # define your styles
- # title = ws.style.add_style(:bg_color => "FFFF0000",
+ # title = ws.style.add_style(:bg_color => "FFFF0000",
# :fg_color=>"#FF000000",
- # :border=>Axlsx::STYLE_THIN_BORDER,
+ # :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,
+ # 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)
#
@@ -188,74 +188,83 @@
# 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)
def add_style(options={})
-
+
numFmtId = if options[:format_code]
n = @numFmts.map{ |f| f.numFmtId }.max + 1
numFmts << NumFmt.new(:numFmtId => n, :formatCode=> options[:format_code])
n
else
options[:num_fmt] || 0
end
-
+
borderId = options[: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 options[:bg_color]
color = Color.new(:rgb=>options[:bg_color])
pattern = PatternFill.new(:patternType =>:solid, :fgColor=>color)
- fills << Fill.new(pattern)
+ fills << Fill.new(pattern)
else
0
end
-
+
fontId = if (options.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}=", options[k]) unless options[k].nil? }
font.color = Color.new(:rgb => options[:fg_color]) unless options[:fg_color].nil?
font.name = options[:font_name] unless options[:font_name].nil?
fonts << font
else
- 0
+ 0
end
-
+
applyProtection = (options[:hidden] || options[: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 options[:alignment]
xf.alignment = CellAlignment.new(options[:alignment])
+ xf.applyAlignment = true
end
-
+
if applyProtection
xf.protection = CellProtection.new(options)
end
-
+
cellXfs << xf
end
-
- # Serializes the styles document
+
+ # Serializes the object
+ # @param [String] str
# @return [String]
- 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) unless self.instance_values[key.to_s].nil?
- end
- }
+ 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
- builder.to_xml(:save_with => 0)
+ str << '</styleSheet>'
end
private
- # Creates the default set of styles the exel requires to be valid as well as setting up the
+ # Creates the default set of styles the exel requires to be valid as well as setting up the
# Axlsx::STYLE_THIN_BORDER
def load_default_styles
@numFmts = SimpleTypedList.new NumFmt, 'numFmts'
@numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDD, :formatCode=> "yyyy/mm/dd")
@numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDDHHMMSS, :formatCode=> "yyyy/mm/dd hh:mm:ss")
@@ -272,12 +281,12 @@
@fills.lock
@borders = SimpleTypedList.new Border, 'borders'
@borders << Border.new
black_border = Border.new
- [:left, :right, :top, :bottom].each do |item|
- black_border.prs << BorderPr.new(:name=>item, :style=>:thin, :color=>Color.new(:rgb=>"FF000000"))
+ [:left, :right, :top, :bottom].each do |item|
+ black_border.prs << BorderPr.new(:name=>item, :style=>:thin, :color=>Color.new(:rgb=>"FF000000"))
end
@borders << black_border
@borders.lock
@cellStyleXfs = SimpleTypedList.new Xf, "cellStyleXfs"
@@ -298,6 +307,6 @@
@dxfs = SimpleTypedList.new(Xf, "dxfs"); @dxfs.lock
@tableStyles = TableStyles.new(:defaultTableStyle => "TableStyleMedium9", :defaultPivotStyle => "PivotStyleLight16"); @tableStyles.lock
end
end
end
-
+