From 43d31eb7c4f04e3ddf3bf865d55e6ce8de7b7a69 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Fri, 23 Mar 2012 00:48:42 +0900 Subject: update add Styles#add_style to allow a simple hash to create a new border style --- lib/axlsx/stylesheet/border.rb | 8 ++++---- lib/axlsx/stylesheet/styles.rb | 12 +++++++++++- test/stylesheet/tc_styles.rb | 15 +++++++++++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/axlsx/stylesheet/border.rb b/lib/axlsx/stylesheet/border.rb index 534521e6..f3d329fa 100644 --- a/lib/axlsx/stylesheet/border.rb +++ b/lib/axlsx/stylesheet/border.rb @@ -21,13 +21,13 @@ module Axlsx # @option options [Boolean] outline # @example Making a border # p = Axlsx::Package.new - # red_border = Axlsx::Border.new - # [:left, :right, :top, :bottom].each {|item| red_border.prs << Axlsx::BorderPr.new(:name => item, :style=>:thin, :color => Axlsx::Color.new(:rgb => "FFFF0000"))} - # red_border = p.workbook.styles.borders << red_border - # red_border = p.workbook.styles.add_style :border => red_border + # red_border = p.workbook.styles.add_style :border => {:style =>: thin, :color => "FFFF0000"} # ws = p.workbook.add_worksheet # ws.add_row [1,2,3], :style => red_border # p.serialize('red_border.xlsx') + # + # @note The recommended way to manage borders is with Style#add_style + # @see Style#add_style def initialize(options={}) @prs = SimpleTypedList.new BorderPr options.each do |o| diff --git a/lib/axlsx/stylesheet/styles.rb b/lib/axlsx/stylesheet/styles.rb index cb40ee74..a05067cd 100644 --- a/lib/axlsx/stylesheet/styles.rb +++ b/lib/axlsx/stylesheet/styles.rb @@ -133,7 +133,7 @@ module Axlsx # @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. This can be the index of an existing border or a hash like {:style => :thin, :color => "FFFF0000"} to create a new border style # @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 @@ -201,6 +201,16 @@ module Axlsx 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] diff --git a/test/stylesheet/tc_styles.rb b/test/stylesheet/tc_styles.rb index 1b7365b2..61a87bf7 100644 --- a/test/stylesheet/tc_styles.rb +++ b/test/stylesheet/tc_styles.rb @@ -2,12 +2,12 @@ require 'test/unit' require 'axlsx.rb' class TestStyles < Test::Unit::TestCase - def setup + def setup @styles = Axlsx::Styles.new end def teardown end - + def test_valid_document schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) doc = Nokogiri::XML(@styles.to_xml) @@ -18,7 +18,14 @@ class TestStyles < Test::Unit::TestCase end assert(errors.size == 0) end + def test_add_style_border_hash + border_count = @styles.borders.size + s = @styles.add_style :border => {:style=>:thin, :color => "FFFF0000"} + assert_equal(@styles.borders.size, border_count + 1) + assert_equal(@styles.borders.last.prs.last.color.rgb, "FFFF0000") + assert_raise(ArgumentError) { @styles.add_style :border => {:color => "FFFF0000"} } + end def test_add_style fill_count = @styles.fills.size @@ -44,9 +51,9 @@ class TestStyles < Test::Unit::TestCase assert_equal(xf.alignment.horizontal, :left, "horizontal alignment applied") assert_equal(xf.protection.hidden, true, "hidden protection set") assert_equal(xf.protection.locked, true, "cell locking set") - assert_raise(ArgumentError, "should reject invalid borderId") { @styles.add_style :border => 2 } + assert_raise(ArgumentError, "should reject invalid borderId") { @styles.add_style :border => 2 } + - assert_equal(xf.applyProtection, 1, "protection applied") assert_equal(xf.applyBorder, true, "border applied") assert_equal(xf.applyNumberFormat, true, "number format applied") -- cgit v1.2.3