summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStephen Pike <[email protected]>2012-04-20 11:19:32 -0400
committerStephen Pike <[email protected]>2012-04-20 15:09:50 -0400
commit7ac97a793f7d2b20c23d455b3965a0af8bd5682b (patch)
tree8aa7974900bace4824bb90fd73a11ffa4302d829
parent7ac3cd9ffb464f306520066dbfddd5c24ff29e9c (diff)
downloadcaxlsx-7ac97a793f7d2b20c23d455b3965a0af8bd5682b.tar.gz
caxlsx-7ac97a793f7d2b20c23d455b3965a0af8bd5682b.zip
Formatting and documentation cleanup
-rw-r--r--lib/axlsx/workbook/worksheet/conditional_formatting.rb35
-rw-r--r--lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb11
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb12
3 files changed, 36 insertions, 22 deletions
diff --git a/lib/axlsx/workbook/worksheet/conditional_formatting.rb b/lib/axlsx/workbook/worksheet/conditional_formatting.rb
index 01a1d719..13f6183d 100644
--- a/lib/axlsx/workbook/worksheet/conditional_formatting.rb
+++ b/lib/axlsx/workbook/worksheet/conditional_formatting.rb
@@ -3,22 +3,22 @@ module Axlsx
#
# @note The recommended way to manage conditional formatting is via Worksheet#add_conditional_formatting
# @see Worksheet#add_conditional_formatting
+ # @see ConditionalFormattingRule
class ConditionalFormatting
- # Sqref
- # Range over which the formatting is applied
+ # Range over which the formatting is applied, in "A1:B2" format
# @return [String]
attr_reader :sqref
- # Rules
# Rules to apply the formatting to. Can be either a hash of
- # options for one ConditionalFormattingRule, an array of hashes
+ # options to create a {ConditionalFormattingRule}, an array of hashes
# for multiple ConditionalFormattingRules, or an array of already
# created ConditionalFormattingRules.
+ # @see ConditionalFormattingRule#initialize
# @return [Array]
attr_reader :rules
- # Creates a new ConditionalFormatting object
+ # Creates a new {ConditionalFormatting} object
# @option options [Array] rules The rules to apply
# @option options [String] sqref The range to apply the rules to
def initialize(options={})
@@ -28,10 +28,17 @@ module Axlsx
end
end
- # Add ConditionalFormattingRules to this object. Rules can either
- # be already created objects or hashes of options for automatic
- # creation.
- # @option rules [Array, Hash] the rules apply, can be just one in hash form
+ # Add Conditional Formatting Rules to this object. Rules can either
+ # be already created {ConditionalFormattingRule} elements or
+ # hashes of options for automatic creation. If rules is a hash
+ # instead of an array, assume only one rule being added.
+ #
+ # @example This would apply formatting "1" to cells > 20, and formatting "2" to cells < 1
+ # conditional_formatting.add_rules [
+ # { :type => :cellIs, :operator => :greaterThan, :formula => "20", :dxfId => 1, :priority=> 1 },
+ # { :type => :cellIs, :operator => :lessThan, :formula => "10", :dxfId => 2, :priority=> 2 } ]
+ #
+ # @param [Array|Hash] rules the rules to apply, can be just one in hash form
# @see ConditionalFormattingRule#initialize
def add_rules(rules)
rules = [rules] if rules.is_a? Hash
@@ -41,8 +48,8 @@ module Axlsx
end
# Add a ConditionalFormattingRule. If a hash of options is passed
- # in create a rule on the fly
- # @option rule [ConditionalFormattingRule, Hash] A rule to create
+ # in create a rule on the fly.
+ # @param [ConditionalFormattingRule|Hash] rule A rule to use, or the options necessary to create one.
# @see ConditionalFormattingRule#initialize
def add_rule(rule)
if rule.is_a? Axlsx::ConditionalFormattingRule
@@ -58,6 +65,12 @@ module Axlsx
def sqref=(v); Axlsx::validate_string(v); @sqref = v end
# Serializes the conditional formatting element
+ # @example Conditional Formatting XML looks like:
+ # <conditionalFormatting sqref="E3:E9">
+ # <cfRule type="cellIs" dxfId="0" priority="1" operator="greaterThan">
+ # <formula>0.5</formula>
+ # </cfRule>
+ # </conditionalFormatting>
# @param [String] str
# @return [String]
def to_xml_string(str = '')
diff --git a/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb b/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb
index bddea5a5..e7cba96f 100644
--- a/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb
+++ b/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb
@@ -4,8 +4,9 @@ module Axlsx
#
# @note The recommended way to manage these rules is via Worksheet#add_conditional_formatting
# @see Worksheet#add_conditional_formatting
+ # @see ConditionalFormattingRule#initialize
class ConditionalFormattingRule
- @@child_elements = [:formula]
+ CHILD_ELEMENTS = [:formula]
# Formula
# @return [String]
@@ -61,13 +62,13 @@ module Axlsx
attr_reader :priority
# Text
- # The text value in a "text contains" conditional formatting
+ # used in a "text contains" conditional formatting
# rule.
# @return [String]
attr_reader :text
# percent (Top 10 Percent)
- # Indicates whether a "top/bottom n" rule is a "top/bottom n
+ # indicates whether a "top/bottom n" rule is a "top/bottom n
# percent" rule. This attribute is ignored if type is not equal to
# top10.
# @return [Boolean]
@@ -158,9 +159,9 @@ module Axlsx
# @return [String]
def to_xml_string(str = '')
str << '<cfRule '
- str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' unless @@child_elements.include?(key.to_sym) }.join(' ')
+ str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' unless CHILD_ELEMENTS.include?(key.to_sym) }.join(' ')
str << '>'
- @@child_elements.each do |el|
+ CHILD_ELEMENTS.each do |el|
str << "<#{el}>" << self.send(el) << "</#{el}>" if self.send(el)
end
str << '</cfRule>'
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index 44975736..59e57ac2 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -124,14 +124,14 @@ module Axlsx
rows.flatten
end
- # Add conditinoal formatting to this worksheet.
+ # Add conditional formatting to this worksheet.
#
- # @option cells [String] The range to apply the formatting to
- # @option rules [Array, Hash] An array of hashes (or just one) to create Conditional formatting rules from
- # @example This would color column A whenever it is FALSE
- # worksheet.add_conditional_formatting( "A1:A1048576", { :type => :cellIs, :operator => :equal, :formula => "FALSE", :dxfId => 0, priority => 1 }
+ # @param [String] cells The range to apply the formatting to
+ # @param [Array|Hash] rules An array of hashes (or just one) to create Conditional formatting rules from.
+ # @example This would format column A whenever it is FALSE
+ # worksheet.add_conditional_formatting( "A1:A1048576", { :type => :cellIs, :operator => :equal, :formula => "FALSE", :dxfId => 1, :priority => 1 }
#
- # @see ConditionalFormattingRule.#initialize
+ # @see ConditionalFormattingRule#initialize
def add_conditional_formatting(cells, rules)
cf = ConditionalFormatting.new( :sqref => cells )
cf.add_rules rules