summaryrefslogtreecommitdiffhomepage
path: root/test/workbook/worksheet/tc_conditional_formatting.rb
blob: 104ce456bebe0ec756bf366bdfafa66733e448ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# frozen_string_literal: true

require 'tc_helper'

class TestConditionalFormatting < Test::Unit::TestCase
  def setup
    p = Axlsx::Package.new
    @ws = p.workbook.add_worksheet name: "hmmm"
    @cfs = @ws.add_conditional_formatting("A1:A1", [{ type: :cellIs, dxfId: 0, priority: 1, operator: :greaterThan, formula: "0.5" }])
    @cf = @cfs.first
    @cfr = @cf.rules.first
  end

  def test_initialize_with_options
    optioned = Axlsx::ConditionalFormatting.new(sqref: "AA1:AB100", rules: [1, 2])

    assert_equal("AA1:AB100", optioned.sqref)
    assert_equal([1, 2], optioned.rules)
  end

  def test_add_as_rule
    color_scale = Axlsx::ColorScale.new do |cs|
      cs.colors.first.rgb = "FFDFDFDF"
      cs.colors.last.rgb = "FF00FF00"
      cs.value_objects.first.type = :percentile
      cs.value_objects.first.val = 5
    end

    data_bar = Axlsx::DataBar.new color: "FFFF0000"
    icon_set = Axlsx::IconSet.new iconSet: "5Rating"
    cfr = Axlsx::ConditionalFormattingRule.new({ type: :containsText, text: "TRUE",
                                                 dxfId: 0, priority: 1,
                                                 formula: 'NOT(ISERROR(SEARCH("FALSE",AB1)))',
                                                 color_scale: color_scale,
                                                 data_bar: data_bar,
                                                 icon_set: icon_set })

    assert(cfr.data_bar.is_a?(Axlsx::DataBar))
    assert(cfr.icon_set.is_a?(Axlsx::IconSet))
    assert(cfr.color_scale.is_a?(Axlsx::ColorScale))
    cfs = @ws.add_conditional_formatting("B2:B2", [cfr])
    doc = Nokogiri::XML.parse(cfs.last.to_xml_string)

    assert_equal(1, doc.xpath(".//conditionalFormatting[@sqref='B2:B2']//cfRule[@type='containsText'][@dxfId=0][@priority=1]").size)
    assert doc.xpath(".//conditionalFormatting//cfRule[@type='containsText'][@dxfId=0][@priority=1]//formula='NOT(ISERROR(SEARCH(\"FALSE\",AB1)))'")

    cfs.last.rules.last.type = :colorScale
    doc = Nokogiri::XML.parse(cfs.last.to_xml_string)

    assert_equal(2, doc.xpath(".//conditionalFormatting//cfRule//colorScale//cfvo").size)
    assert_equal(2, doc.xpath(".//conditionalFormatting//cfRule//colorScale//color").size)

    cfs.last.rules.last.type = :dataBar
    doc = Nokogiri::XML.parse(cfs.last.to_xml_string)

    assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule//dataBar").size)
    assert_equal(2, doc.xpath(".//conditionalFormatting//cfRule//dataBar//cfvo").size)
    assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule//dataBar//color[@rgb='FFFF0000']").size)

    cfs.last.rules.last.type = :iconSet
    doc = Nokogiri::XML.parse(cfs.last.to_xml_string)

    assert_equal(3, doc.xpath(".//conditionalFormatting//cfRule//iconSet//cfvo").size)
    assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule//iconSet[@iconSet='5Rating']").size)
  end

  def test_add_as_hash
    color_scale = Axlsx::ColorScale.new do |cs|
      cs.colors.first.rgb = "FFDFDFDF"
      cs.colors.last.rgb = "FF00FF00"
      cs.value_objects.first.type = :percentile
      cs.value_objects.first.val = 5
    end

    data_bar = Axlsx::DataBar.new color: "FFFF0000"
    icon_set = Axlsx::IconSet.new iconSet: "5Rating"

    cfs = @ws.add_conditional_formatting("B2:B2", [{ type: :containsText, text: "TRUE",
                                                     dxfId: 0, priority: 1,
                                                     formula: 'NOT(ISERROR(SEARCH("FALSE",AB1)))',
                                                     color_scale: color_scale,
                                                     data_bar: data_bar,
                                                     icon_set: icon_set }])
    doc = Nokogiri::XML.parse(cfs.last.to_xml_string)

    assert_equal(1, doc.xpath(".//conditionalFormatting[@sqref='B2:B2']//cfRule[@type='containsText'][@dxfId=0][@priority=1]").size)
    assert doc.xpath(".//conditionalFormatting//cfRule[@type='containsText'][@dxfId=0][@priority=1]//formula='NOT(ISERROR(SEARCH(\"FALSE\",AB1)))'")

    cfs.last.rules.last.type = :colorScale
    doc = Nokogiri::XML.parse(cfs.last.to_xml_string)

    assert_equal(2, doc.xpath(".//conditionalFormatting//cfRule//colorScale//cfvo").size)
    assert_equal(2, doc.xpath(".//conditionalFormatting//cfRule//colorScale//color").size)

    cfs.last.rules.last.type = :dataBar
    doc = Nokogiri::XML.parse(cfs.last.to_xml_string)

    assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule//dataBar").size)
    assert_equal(2, doc.xpath(".//conditionalFormatting//cfRule//dataBar//cfvo").size)
    assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule//dataBar//color[@rgb='FFFF0000']").size)

    cfs.last.rules.last.type = :iconSet
    doc = Nokogiri::XML.parse(cfs.last.to_xml_string)

    assert_equal(3, doc.xpath(".//conditionalFormatting//cfRule//iconSet//cfvo").size)
    assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule//iconSet[@iconSet='5Rating']").size)
  end

  def test_single_rule
    doc = Nokogiri::XML.parse(@cf.to_xml_string)

    assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='greaterThan']").size)
    assert doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='greaterThan']//formula='0.5'")
  end

  def test_many_options
    cf = Axlsx::ConditionalFormatting.new(sqref: "B3:B4")
    cf.add_rule({ type: :cellIs, aboveAverage: false, bottom: false, dxfId: 0,
                  equalAverage: false, priority: 2, operator: :lessThan, text: "",
                  percent: false, rank: 0, stdDev: 1, stopIfTrue: true, timePeriod: :today,
                  formula: "0.0" })
    doc = Nokogiri::XML.parse(cf.to_xml_string)

    assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@aboveAverage=0][@bottom=0][@dxfId=0][@equalAverage=0][@priority=2][@operator='lessThan'][@text=''][@percent=0][@rank=0][@stdDev=1][@stopIfTrue=1][@timePeriod='today']").size)
    assert doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@aboveAverage=0][@bottom=0][@dxfId=0][@equalAverage=0][@priority=2][@operator='lessThan'][@text=''][@percent=0][@rank=0][@stdDev=1][@stopIfTrue=1][@timePeriod='today']//formula=0.0")
  end

  def test_to_xml
    doc = Nokogiri::XML.parse(@ws.to_xml_string)

    assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='greaterThan']").size)
    assert doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='greaterThan']//xmlns:formula='0.5'")
  end

  def test_multiple_formats
    @ws.add_conditional_formatting "B3:B3", { type: :cellIs, dxfId: 0, priority: 1, operator: :greaterThan, formula: "1" }
    doc = Nokogiri::XML.parse(@ws.to_xml_string)

    assert doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='greaterThan']//xmlns:formula='1'")
    assert doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='greaterThan']//xmlns:formula='0.5'")
  end

  def test_multiple_formulas
    @ws.add_conditional_formatting "B3:B3", { type: :cellIs, dxfId: 0, priority: 1, operator: :between, formula: ["1 <> 2", "5"] }
    doc = Nokogiri::XML.parse(@ws.to_xml_string)

    assert doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='between']//xmlns:formula='1 <> 2'")
    assert doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='between']//xmlns:formula='5'")
  end

  def test_sqref
    assert_raise(ArgumentError) { @cf.sqref = 10 }
    assert_nothing_raised { @cf.sqref = "A1:A1" }
    assert_equal("A1:A1", @cf.sqref)
  end

  def test_type
    assert_raise(ArgumentError) { @cfr.type = "illegal" }
    assert_nothing_raised { @cfr.type = :containsBlanks }
    assert_equal(:containsBlanks, @cfr.type)
  end

  def test_above_average
    assert_raise(ArgumentError) { @cfr.aboveAverage = "illegal" }
    assert_nothing_raised { @cfr.aboveAverage = true }
    assert(@cfr.aboveAverage)
  end

  def test_equal_average
    assert_raise(ArgumentError) { @cfr.equalAverage = "illegal" }
    assert_nothing_raised { @cfr.equalAverage = true }
    assert(@cfr.equalAverage)
  end

  def test_bottom
    assert_raise(ArgumentError) { @cfr.bottom = "illegal" }
    assert_nothing_raised { @cfr.bottom = true }
    assert(@cfr.bottom)
  end

  def test_operator
    assert_raise(ArgumentError) { @cfr.operator = "cellIs" }
    assert_nothing_raised { @cfr.operator = :notBetween }
    assert_equal(:notBetween, @cfr.operator)
  end

  def test_dxf_id
    assert_raise(ArgumentError) { @cfr.dxfId = "illegal" }
    assert_nothing_raised { @cfr.dxfId = 1 }
    assert_equal(1, @cfr.dxfId)
  end

  def test_priority
    assert_raise(ArgumentError) { @cfr.priority = -1.0 }
    assert_nothing_raised { @cfr.priority = 1 }
    assert_equal(1, @cfr.priority)
  end

  def test_text
    assert_raise(ArgumentError) { @cfr.text = 1.0 }
    assert_nothing_raised { @cfr.text = "testing" }
    assert_equal("testing", @cfr.text)
  end

  def test_percent
    assert_raise(ArgumentError) { @cfr.percent = "10%" } # WRONG!
    assert_nothing_raised { @cfr.percent = true }
    assert(@cfr.percent)
  end

  def test_rank
    assert_raise(ArgumentError) { @cfr.rank = -1 }
    assert_nothing_raised { @cfr.rank = 1 }
    assert_equal(1, @cfr.rank)
  end

  def test_std_dev
    assert_raise(ArgumentError) { @cfr.stdDev = -1 }
    assert_nothing_raised { @cfr.stdDev = 1 }
    assert_equal(1, @cfr.stdDev)
  end

  def test_stop_if_true
    assert_raise(ArgumentError) { @cfr.stopIfTrue = "illegal" }
    assert_nothing_raised { @cfr.stopIfTrue = false }
    refute(@cfr.stopIfTrue)
  end

  def test_time_period
    assert_raise(ArgumentError) { @cfr.timePeriod = "illegal" }
    assert_nothing_raised { @cfr.timePeriod = :today }
    assert_equal(:today, @cfr.timePeriod)
  end
end