From c84e62cb7a91fefae0457baabbd9b2f3bef3259e Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sat, 21 Apr 2012 12:53:54 +0900 Subject: add data bar conditional formatting support. --- lib/axlsx/workbook/workbook.rb | 1 + lib/axlsx/workbook/worksheet/color_scale.rb | 5 +- lib/axlsx/workbook/worksheet/data_bar.rb | 95 +++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 lib/axlsx/workbook/worksheet/data_bar.rb (limited to 'lib') 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 << '' @value_objects.each { |cfvo| cfvo.to_xml_string(str) } @@ -53,7 +56,7 @@ module Axlsx str << '' 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 << '' + @value_objects.each { |cfvo| cfvo.to_xml_string(str) } + self.color.to_xml_string(str) + str << '' + 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 -- cgit v1.2.3