diff options
| author | Randy Morgan <[email protected]> | 2012-04-21 12:53:54 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-04-21 12:53:54 +0900 |
| commit | c84e62cb7a91fefae0457baabbd9b2f3bef3259e (patch) | |
| tree | 3f379cbd38a74d7d20fe449b57c07d3307f0421f | |
| parent | a7072a25772a7613620ee60cb607e62ad2db743e (diff) | |
| download | caxlsx-c84e62cb7a91fefae0457baabbd9b2f3bef3259e.tar.gz caxlsx-c84e62cb7a91fefae0457baabbd9b2f3bef3259e.zip | |
add data bar conditional formatting support.
| -rw-r--r-- | lib/axlsx/workbook/workbook.rb | 1 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/color_scale.rb | 5 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/data_bar.rb | 95 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_data_bar.rb | 39 |
4 files changed, 139 insertions, 1 deletions
diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index 22407a84..b71a345a 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -6,6 +6,7 @@ require 'axlsx/workbook/worksheet/cell.rb' require 'axlsx/workbook/worksheet/page_margins.rb' require 'axlsx/workbook/worksheet/cfvo.rb' require 'axlsx/workbook/worksheet/color_scale.rb' +require 'axlsx/workbook/worksheet/data_bar.rb' require 'axlsx/workbook/worksheet/conditional_formatting.rb' require 'axlsx/workbook/worksheet/conditional_formatting_rule.rb' require 'axlsx/workbook/worksheet/row.rb' diff --git a/lib/axlsx/workbook/worksheet/color_scale.rb b/lib/axlsx/workbook/worksheet/color_scale.rb index c2d3a5ac..11f97e21 100644 --- a/lib/axlsx/workbook/worksheet/color_scale.rb +++ b/lib/axlsx/workbook/worksheet/color_scale.rb @@ -46,6 +46,9 @@ module Axlsx @colors.delete_at index end + # Serialize this color_scale object data to an xml string + # @param [String] str + # @return [String] def to_xml_string(str = '') str << '<colorScale>' @value_objects.each { |cfvo| cfvo.to_xml_string(str) } @@ -53,7 +56,7 @@ module Axlsx str << '</colorScale>' end - protected + private # creates the initial cfvo objects def initialize_value_objects diff --git a/lib/axlsx/workbook/worksheet/data_bar.rb b/lib/axlsx/workbook/worksheet/data_bar.rb new file mode 100644 index 00000000..701fba84 --- /dev/null +++ b/lib/axlsx/workbook/worksheet/data_bar.rb @@ -0,0 +1,95 @@ +module Axlsx + # Conditional Format Rule data bar object + # Describes a data bar conditional formatting rule. + + # @note The recommended way to manage these rules is via Worksheet#add_conditional_formatting + # @see Worksheet#add_conditional_formatting + # @see ConditionalFormattingRule#initialize + class DataBar + CHILD_ELEMENTS = [:value_objects, :color] + + # minLength attribute + # The minimum length of the data bar, as a percentage of the cell width. + # The default value is 10 + # @return [Integer] + attr_reader :minLength + + # maxLength attribute + # The maximum length of the data bar, as a percentage of the cell width. + # The default value is 90 + # @return [Integer] + attr_reader :maxLength + + # maxLength attribute + # Indicates whether to show the values of the cells on which this data bar is applied. + # The default value is true + # @return [Boolean] + attr_reader :showValue + + # A simple typed list of cfvos + # @return [SimpleTypedList] + # @see Cfvo + attr_reader :value_objects + + # color + # the color object used in the data bar formatting + # @return [Color] + def color + @color ||= Color.new :rgb => "FF0000FF" + end + + # Creates a new data bar conditional formatting object + # @option options [Integer] minLength + # @option options [Integer] maxLength + # @option options [Boolean] showValue + # @option options [String] color - the rbg value used to color the bars + def initialize(options = {}) + @minLength = 10 + @maxLength = 90 + @showValue = true + initialize_value_objects + options.each do |o| + self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" + end + yield self if block_given? + end + + # @see minLength + def minLength=(v); Axlsx.validate_unsigned_int(v); @minLength = v end + # @see maxLength + def maxLength=(v); Axlsx.validate_unsigned_int(v); @maxLength = v end + + # @see showValue + def showValue=(v); Axlsx.validate_boolean(v); @showValue = v end + + # Sets the color for the data bars. + # @param [Color|String] The color object, or rgb string value to apply + def color=(v) + @color = v if v.is_a? Color + self.color.rgb = v if v.is_a? String + @color + end + + # Serialize this object to an xml string + # @param [String] str + # @return [String] + def to_xml_string(str="") + str << '<dataBar ' + str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' unless CHILD_ELEMENTS.include?(key.to_sym) }.join(' ') + str << '>' + @value_objects.each { |cfvo| cfvo.to_xml_string(str) } + self.color.to_xml_string(str) + str << '</dataBar>' + end + + private + + # Initalize the simple typed list of value objects + # I am keeping this private for now as I am not sure what impact changes to the required two cfvo objects will do. + def initialize_value_objects + @value_objects = SimpleTypedList.new Cfvo + @value_objects.concat [Cfvo.new(:type => :min, :val => 0), Cfvo.new(:type => :max, :val => 0)] + @value_objects.lock + end + end +end diff --git a/test/workbook/worksheet/tc_data_bar.rb b/test/workbook/worksheet/tc_data_bar.rb new file mode 100644 index 00000000..644ce6bd --- /dev/null +++ b/test/workbook/worksheet/tc_data_bar.rb @@ -0,0 +1,39 @@ +require 'tc_helper.rb' + +class TestDataBar < Test::Unit::TestCase + def setup + @data_bar = Axlsx::DataBar.new :color => "FF638EC6" + end + + def test_defaults + assert_equal @data_bar.minLength, 10 + assert_equal @data_bar.maxLength, 90 + assert_equal @data_bar.showValue, true + end + + def test_minLength + assert_raise(ArgumentError) { @data_bar.minLength = :invalid_type } + assert_nothing_raised { @data_bar.minLength = 0} + assert_equal(@data_bar.minLength, 0) + end + + def test_maxLength + assert_raise(ArgumentError) { @data_bar.maxLength = :invalid_type } + assert_nothing_raised { @data_bar.maxLength = 0} + assert_equal(@data_bar.maxLength, 0) + end + + def test_maxLength + assert_raise(ArgumentError) { @data_bar.showValue = :invalid_type } + assert_nothing_raised { @data_bar.showValue = false} + assert_equal(@data_bar.showValue, false) + end + + def test_to_xml_string + doc = Nokogiri::XML.parse(@data_bar.to_xml_string) + assert_equal(doc.xpath(".//dataBar[@minLength=10][@maxLength=90][@showValue='true']").size, 1) + assert_equal(doc.xpath(".//dataBar//cfvo").size, 2) + assert_equal(doc.xpath(".//dataBar//color").size, 1) + end + +end |
