|
| 1 | +module Axlsx |
| 2 | + # Conditional formatting rules specify formulas whose evaluations |
| 3 | + # format cells |
| 4 | + # |
| 5 | + # @note The recommended way to manage these rules is via Worksheet#add_conditional_formatting |
| 6 | + # @see Worksheet#add_conditional_formatting |
| 7 | + class ConditionalFormattingRule |
| 8 | + @@child_elements = [:formula] |
| 9 | + |
| 10 | + # Formula |
| 11 | + # @return [String] |
| 12 | + attr_reader :formula |
| 13 | + |
| 14 | + # Type (ST_CfType) |
| 15 | + # options are expression, cellIs, colorScale, dataBar, iconSet, |
| 16 | + # top10, uniqueValues, duplicateValues, containsText, |
| 17 | + # notContainsText, beginsWith, endsWith, containsBlanks, |
| 18 | + # notContainsBlanks, containsErrors, notContainsErrors, |
| 19 | + # timePeriod, aboveAverage |
| 20 | + # @return [Symbol] |
| 21 | + attr_reader :type |
| 22 | + |
| 23 | + # Above average rule |
| 24 | + # Indicates whether the rule is an "above average" rule. True |
| 25 | + # indicates 'above average'. This attribute is ignored if type is |
| 26 | + # not equal to aboveAverage. |
| 27 | + # @return [Boolean] |
| 28 | + attr_reader :aboveAverage |
| 29 | + |
| 30 | + # Bottom N rule |
| 31 | + # @return [Boolean] |
| 32 | + attr_reader :bottom |
| 33 | + |
| 34 | + # Differential Formatting Id |
| 35 | + # @return [Integer] |
| 36 | + attr_reader :dxfId |
| 37 | + |
| 38 | + # Equal Average |
| 39 | + # Flag indicating whether the 'aboveAverage' and 'belowAverage' |
| 40 | + # criteria is inclusive of the average itself, or exclusive of |
| 41 | + # that value. |
| 42 | + # @return [Boolean] |
| 43 | + attr_reader :equalAverage |
| 44 | + |
| 45 | + # Operator |
| 46 | + # The operator in a "cell value is" conditional formatting |
| 47 | + # rule. This attribute is ignored if type is not equal to cellIs |
| 48 | + # |
| 49 | + # Operator must be one of lessThan, lessThanOrEqual, equal, |
| 50 | + # notEqual, greaterThanOrEqual, greaterThan, between, notBetween, |
| 51 | + # containsText, notContains, beginsWith, endsWith |
| 52 | + # @return [Symbol] |
| 53 | + attr_reader :operator |
| 54 | + |
| 55 | + # Priority |
| 56 | + # The priority of this conditional formatting rule. This value is |
| 57 | + # used to determine which format should be evaluated and |
| 58 | + # rendered. Lower numeric values are higher priority than higher |
| 59 | + # numeric values, where '1' is the highest priority. |
| 60 | + # @return [Integer] |
| 61 | + attr_reader :priority |
| 62 | + |
| 63 | + # Text |
| 64 | + # The text value in a "text contains" conditional formatting |
| 65 | + # rule. |
| 66 | + # @return [String] |
| 67 | + attr_reader :text |
| 68 | + |
| 69 | + # percent (Top 10 Percent) |
| 70 | + # Indicates whether a "top/bottom n" rule is a "top/bottom n |
| 71 | + # percent" rule. This attribute is ignored if type is not equal to |
| 72 | + # top10. |
| 73 | + # @return [Boolean] |
| 74 | + attr_reader :percent |
| 75 | + |
| 76 | + # rank (Rank) |
| 77 | + # The value of "n" in a "top/bottom n" conditional formatting |
| 78 | + # rule. This attribute is ignored if type is not equal to top10. |
| 79 | + # @return [Integer] |
| 80 | + attr_reader :rank |
| 81 | + |
| 82 | + # stdDev (StdDev) |
| 83 | + # The number of standard deviations to include above or below the |
| 84 | + # average in the conditional formatting rule. This attribute is |
| 85 | + # ignored if type is not equal to aboveAverage. If a value is |
| 86 | + # present for stdDev and the rule type = aboveAverage, then this |
| 87 | + # rule is automatically an "above or below N standard deviations" |
| 88 | + # rule. |
| 89 | + # @return [Integer] |
| 90 | + attr_reader :stdDev |
| 91 | + |
| 92 | + # stopIfTrue (Stop If True) |
| 93 | + # If this flag is '1', no rules with lower priority shall be |
| 94 | + # applied over this rule, when this rule evaluates to true. |
| 95 | + # @return [Boolean] |
| 96 | + attr_reader :stopIfTrue |
| 97 | + |
| 98 | + # timePeriod (Time Period) |
| 99 | + # The applicable time period in a "date occurring…" conditional |
| 100 | + # formatting rule. This attribute is ignored if type is not equal |
| 101 | + # to timePeriod. |
| 102 | + # Valid types are today, yesterday, tomorrow, last7Days, |
| 103 | + # thisMonth, lastMonth, nextMonth, thisWeek, lastWeek, nextWeek |
| 104 | + attr_reader :timePeriod |
| 105 | + |
| 106 | + # Creates a new Conditional Formatting Rule object |
| 107 | + # @option options [Symbol] type The type of this formatting rule |
| 108 | + # @option options [Boolean] aboveAverage This is an aboveAverage rule |
| 109 | + # @option options [Boolean] bottom This is a bottom N rule. |
| 110 | + # @option options [Integer] dxfId The formatting id to apply to matches |
| 111 | + # @option options [Boolean] equalAverage Is the aboveAverage or belowAverage rule inclusive |
| 112 | + # @option options [Integer] priority The priority of the rule, 1 is highest |
| 113 | + # @option options [Symbol] operator Which operator to apply |
| 114 | + # @option options [String] text The value to apply a text operator against |
| 115 | + # @option options [Boolean] percent If a top/bottom N rule, evaluate as N% rather than N |
| 116 | + # @option options [Integer] rank If a top/bottom N rule, the value of N |
| 117 | + # @option options [Integer] stdDev The number of standard deviations above or below the average to match |
| 118 | + # @option options [Boolean] stopIfTrue Stop evaluating rules after this rule matches |
| 119 | + # @option options [Symbol] timePeriod The time period in a date occuring... rule |
| 120 | + # @option options [String] formula The formula to match against in i.e. an equal rule |
| 121 | + def initialize(options={}) |
| 122 | + options.each do |o| |
| 123 | + self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}=" |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + # @see type |
| 128 | + def type=(v); Axlsx::validate_conditional_formatting_type(v); @type = v end |
| 129 | + # @see aboveAverage |
| 130 | + def aboveAverage=(v); Axlsx::validate_boolean(v); @aboveAverage = v end |
| 131 | + # @see bottom |
| 132 | + def bottom=(v); Axlsx::validate_boolean(v); @bottom = v end |
| 133 | + # @see dxfId |
| 134 | + def dxfId=(v); Axlsx::validate_unsigned_numeric(v); @dxfId = v end |
| 135 | + # @see equalAverage |
| 136 | + def equalAverage=(v); Axlsx::validate_boolean(v); @equalAverage = v end |
| 137 | + # @see priority |
| 138 | + def priority=(v); Axlsx::validate_unsigned_numeric(v); @priority = v end |
| 139 | + # @see operator |
| 140 | + def operator=(v); Axlsx::validate_conditional_formatting_operator(v); @operator = v end |
| 141 | + # @see text |
| 142 | + def text=(v); Axlsx::validate_string(v); @text = v end |
| 143 | + # @see percent |
| 144 | + def percent=(v); Axlsx::validate_boolean(v); @percent = v end |
| 145 | + # @see rank |
| 146 | + def rank=(v); Axlsx::validate_unsigned_numeric(v); @rank = v end |
| 147 | + # @see stdDev |
| 148 | + def stdDev=(v); Axlsx::validate_unsigned_numeric(v); @stdDev = v end |
| 149 | + # @see stopIfTrue |
| 150 | + def stopIfTrue=(v); Axlsx::validate_boolean(v); @stopIfTrue = v end |
| 151 | + # @see timePeriod |
| 152 | + def timePeriod=(v); Axlsx::validate_time_period_type(v); @timePeriod = v end |
| 153 | + # @see formula |
| 154 | + def formula=(v); Axlsx::validate_string(v); @formula = v end |
| 155 | + |
| 156 | + # Serializes the conditional formatting rule |
| 157 | + # @param [String] str |
| 158 | + # @return [String] |
| 159 | + def to_xml_string(str = '') |
| 160 | + str << '<cfRule ' |
| 161 | + str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' unless @@child_elements.include?(key.to_sym) }.join(' ') |
| 162 | + str << '>' |
| 163 | + @@child_elements.each do |el| |
| 164 | + str << "<#{el}>" << self.send(el) << "</#{el}>" if self.send(el) |
| 165 | + end |
| 166 | + str << '</cfRule>' |
| 167 | + end |
| 168 | + end |
| 169 | +end |
0 commit comments