Class: Axlsx::ConditionalFormatting
- Inherits:
-
Object
- Object
- Axlsx::ConditionalFormatting
- Defined in:
- lib/axlsx/workbook/worksheet/conditional_formatting.rb
Overview
The recommended way to manage conditional formatting is via Worksheet#add_conditional_formatting
Conditional formatting allows styling of ranges based on functions
Instance Attribute Summary collapse
-
#rules ⇒ Array
Rules to apply the formatting to.
-
#sqref ⇒ String
Range over which the formatting is applied, in "A1:B2" format.
Instance Method Summary collapse
-
#add_rule(rule) ⇒ Object
Add a ConditionalFormattingRule.
-
#add_rules(rules) ⇒ Object
Add Conditional Formatting Rules to this object.
-
#initialize(options = {}) ⇒ ConditionalFormatting
constructor
Creates a new ConditionalFormatting object.
-
#to_xml_string(str = '') ⇒ String
Serializes the conditional formatting element.
Constructor Details
#initialize(options = {}) ⇒ ConditionalFormatting
Creates a new Axlsx::ConditionalFormatting object
24 25 26 27 28 29 |
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 24 def initialize(={}) @rules = [] .each do |o| self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" end end |
Instance Attribute Details
#rules ⇒ Array
Rules to apply the formatting to. Can be either a hash of options to create a Axlsx::ConditionalFormattingRule, an array of hashes for multiple ConditionalFormattingRules, or an array of already created ConditionalFormattingRules.
19 20 21 |
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 19 def rules @rules end |
#sqref ⇒ String
Range over which the formatting is applied, in "A1:B2" format
11 12 13 |
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 11 def sqref @sqref end |
Instance Method Details
#add_rule(rule) ⇒ Object
Add a ConditionalFormattingRule. If a hash of options is passed in create a rule on the fly.
54 55 56 57 58 59 60 |
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 54 def add_rule(rule) if rule.is_a? Axlsx::ConditionalFormattingRule @rules << rule elsif rule.is_a? Hash @rules << ConditionalFormattingRule.new(rule) end end |
#add_rules(rules) ⇒ Object
Add Conditional Formatting Rules to this object. Rules can either be already created Axlsx::ConditionalFormattingRule elements or hashes of options for automatic creation. If rules is a hash instead of an array, assume only one rule being added.
43 44 45 46 47 48 |
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 43 def add_rules(rules) rules = [rules] if rules.is_a? Hash rules.each do |rule| add_rule rule end end |
#to_xml_string(str = '') ⇒ String
Serializes the conditional formatting element
76 77 78 79 80 |
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 76 def to_xml_string(str = '') str << '<conditionalFormatting sqref="' << sqref << '">' str << rules.collect{ |rule| rule.to_xml_string }.join(' ') str << '</conditionalFormatting>' end |