Class: Axlsx::ConditionalFormatting

Inherits:
Object
  • Object
show all
Includes:
OptionsParser
Defined in:
lib/axlsx/workbook/worksheet/conditional_formatting.rb

Overview

Note:

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

Instance Method Summary collapse

Methods included from OptionsParser

#parse_options

Constructor Details

#initialize(options = {}) ⇒ ConditionalFormatting

Creates a new Axlsx::ConditionalFormatting object

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • rules (Array)

    The rules to apply

  • sqref (String)

    The range to apply the rules to



15
16
17
18
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 15

def initialize(options = {})
  @rules = []
  parse_options options
end

Instance Attribute Details

#rulesArray

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.

Returns:

  • (Array)

See Also:



30
31
32
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 30

def rules
  @rules
end

#sqrefString

Range over which the formatting is applied, in "A1:B2" format

Returns:

  • (String)


22
23
24
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 22

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.

Parameters:

See Also:



55
56
57
58
59
60
61
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 55

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.

Examples:

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 } ]

Parameters:

  • rules (Array|Hash)

    the rules to apply, can be just one in hash form

See Also:



44
45
46
47
48
49
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 44

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

Examples:

Conditional Formatting XML looks like:

<conditionalFormatting sqref="E3:E9">
    <cfRule type="cellIs" dxfId="0" priority="1" operator="greaterThan">
         <formula>0.5</formula>
    </cfRule>
</conditionalFormatting>

Parameters:

  • str (String) (defaults to: +''))

Returns:

  • (String)


83
84
85
86
87
88
89
90
# File 'lib/axlsx/workbook/worksheet/conditional_formatting.rb', line 83

def to_xml_string(str = +'')
  str << '<conditionalFormatting sqref="' << sqref << '">'
  rules.each_with_index do |rule, index|
    str << ' ' unless index == 0
    rule.to_xml_string(str)
  end
  str << '</conditionalFormatting>'
end