From 0962f913904343db03d253cc72681c8213aacea3 Mon Sep 17 00:00:00 2001 From: raiis Date: Mon, 26 Nov 2012 21:13:55 +0200 Subject: Added individual border override options. --- examples/styles.rb | 4 ++++ lib/axlsx/stylesheet/styles.rb | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/examples/styles.rb b/examples/styles.rb index b69251b4..e8047ab1 100644 --- a/examples/styles.rb +++ b/examples/styles.rb @@ -48,6 +48,9 @@ wb.styles do |style| # A style that a applies a font size and a custom formatting code custom_format = wb.styles.add_style :sz => 20, :format_code => 'yyyy-mm-dd' + # A style that overrides top and left border style + one_border = wb.styles.add_style :border => { :style => :thin, :color =>"FAAC58", :edges => [:right, :top, :left] }, :border_top => { :style => :thick, :color => "01DF74" }, :border_left => { :color => "0101DF" } + wb.add_worksheet do |sheet| @@ -55,6 +58,7 @@ wb.styles do |style| sheet.add_row [123, "123", Time.now], style: [nil, large_font, predefined_format] sheet.add_row [123, "123", Date.new(2012, 9, 14)], style: [large_font, nil, custom_format] sheet.add_row [123, "123", Date.new(2000, 9, 12)] # This uses the axlsx default format_code (14) + sheet.add_row [123, "123", Time.now], style: [large_font, one_border, predefined_format] end end diff --git a/lib/axlsx/stylesheet/styles.rb b/lib/axlsx/stylesheet/styles.rb index da5b277f..ab2903f2 100644 --- a/lib/axlsx/stylesheet/styles.rb +++ b/lib/axlsx/stylesheet/styles.rb @@ -308,6 +308,8 @@ module Axlsx # may include an :edges entry that references an array of symbols identifying which border edges # you wish to apply the style or any other valid Border initializer options. # If the :edges entity is not provided the style is applied to all edges of cells that reference this style. + # Also available :border_top, :border_right, :border_bottom and :border_left options with :style and/or :color + # key-value entries, which override :border values. # @example # #apply a thick red border to the top and bottom # { :border => { :style => :thick, :color => "FFFF0000", :edges => [:top, :bottom] } @@ -319,7 +321,10 @@ module Axlsx raise ArgumentError, (ERR_INVALID_BORDER_OPTIONS % b_opts) unless b_opts.keys.include?(:style) && b_opts.keys.include?(:color) border = Border.new b_opts (b_opts[:edges] || [:left, :right, :top, :bottom]).each do |edge| - b_options = { :name => edge, :style => b_opts[:style], :color => Color.new(:rgb => b_opts[:color]) } + sb_opts = options["border_#{edge}".to_sym] + style = (!sb_opts.nil? && sb_opts.keys.include?(:style)) ? sb_opts[:style] : b_opts[:style] + color = (!sb_opts.nil? && sb_opts.keys.include?(:color)) ? sb_opts[:color] : b_opts[:color] + b_options = { :name => edge, :style => style, :color => Color.new(:rgb => color) } border.prs << BorderPr.new(b_options) end options[:type] == :dxf ? border : borders << border -- cgit v1.2.3 From e5c43799db3c88938a8abaefab61d8018c07a82e Mon Sep 17 00:00:00 2001 From: raiis Date: Wed, 28 Nov 2012 00:36:51 +0200 Subject: Better way hot to override borders style. --- examples/styles.rb | 4 ++-- lib/axlsx/stylesheet/styles.rb | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/styles.rb b/examples/styles.rb index e8047ab1..7a589e2e 100644 --- a/examples/styles.rb +++ b/examples/styles.rb @@ -49,7 +49,7 @@ wb.styles do |style| custom_format = wb.styles.add_style :sz => 20, :format_code => 'yyyy-mm-dd' # A style that overrides top and left border style - one_border = wb.styles.add_style :border => { :style => :thin, :color =>"FAAC58", :edges => [:right, :top, :left] }, :border_top => { :style => :thick, :color => "01DF74" }, :border_left => { :color => "0101DF" } + override_border = wb.styles.add_style :border => { :style => :thin, :color =>"FAAC58", :edges => [:right, :top, :left] }, :border_top => { :style => :thick, :color => "01DF74" }, :border_left => { :color => "0101DF" } wb.add_worksheet do |sheet| @@ -58,7 +58,7 @@ wb.styles do |style| sheet.add_row [123, "123", Time.now], style: [nil, large_font, predefined_format] sheet.add_row [123, "123", Date.new(2012, 9, 14)], style: [large_font, nil, custom_format] sheet.add_row [123, "123", Date.new(2000, 9, 12)] # This uses the axlsx default format_code (14) - sheet.add_row [123, "123", Time.now], style: [large_font, one_border, predefined_format] + sheet.add_row [123, "123", Time.now], style: [large_font, override_border, predefined_format] end end diff --git a/lib/axlsx/stylesheet/styles.rb b/lib/axlsx/stylesheet/styles.rb index ab2903f2..74894052 100644 --- a/lib/axlsx/stylesheet/styles.rb +++ b/lib/axlsx/stylesheet/styles.rb @@ -321,10 +321,9 @@ module Axlsx raise ArgumentError, (ERR_INVALID_BORDER_OPTIONS % b_opts) unless b_opts.keys.include?(:style) && b_opts.keys.include?(:color) border = Border.new b_opts (b_opts[:edges] || [:left, :right, :top, :bottom]).each do |edge| - sb_opts = options["border_#{edge}".to_sym] - style = (!sb_opts.nil? && sb_opts.keys.include?(:style)) ? sb_opts[:style] : b_opts[:style] - color = (!sb_opts.nil? && sb_opts.keys.include?(:color)) ? sb_opts[:color] : b_opts[:color] - b_options = { :name => edge, :style => style, :color => Color.new(:rgb => color) } + edge_options = options["border_#{edge}".to_sym] || {} + border_edge = b_opts.merge(edge_options) + b_options = { :name => edge, :style => border_edge[:style], :color => Color.new(:rgb => border_edge[:color]) } border.prs << BorderPr.new(b_options) end options[:type] == :dxf ? border : borders << border -- cgit v1.2.3