summaryrefslogtreecommitdiffhomepage
path: root/test/workbook/worksheet/tc_cell.rb
blob: 94c424b3134aac5c914a6e368eb0b35999e7cebb (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# frozen_string_literal: true

require 'tc_helper'

class TestCell < Test::Unit::TestCase
  def setup
    p = Axlsx::Package.new
    p.use_shared_strings = true
    @ws = p.workbook.add_worksheet name: "hmmm"
    p.workbook.styles.add_style sz: 20
    @row = @ws.add_row
    @c = @row.add_cell 1, type: :float, style: 1, escape_formulas: true
    data = (0..26).map { |index| index }
    @ws.add_row data
    @cAA = @ws["AA2"]
  end

  def test_initialize
    assert_equal(@row.cells.last, @c, "the cell was added to the row")
    assert_equal(:float, @c.type, "type option is applied")
    assert_equal(1, @c.style, "style option is applied")
    assert_in_delta(@c.value, 1.0, 0.001, "type option is applied and value is casted")
    assert(@c.escape_formulas, "escape formulas option is applied")
  end

  def test_style_date_data
    c = Axlsx::Cell.new(@c.row, Time.now)

    assert_equal(Axlsx::STYLE_DATE, c.style)
  end

  def test_row
    assert_equal(@c.row, @row)
  end

  def test_index
    assert_equal(@c.index, @row.cells.index(@c))
  end

  def test_pos
    assert_equal(@c.pos, [@c.index, @c.row.index(@c)])
  end

  def test_r
    assert_equal("A1", @c.r, "calculate cell reference")
  end

  def test_wide_r
    assert_equal("AA2", @cAA.r, "calculate cell reference")
  end

  def test_r_abs
    assert_equal("$A$1", @c.r_abs, "calculate absolute cell reference")
    assert_equal("$AA$2", @cAA.r_abs, "needs to accept multi-digit columns")
  end

  def test_name
    @c.name = 'foo'

    assert_equal(1, @ws.workbook.defined_names.size)
    assert_equal('foo', @ws.workbook.defined_names.last.name)
  end

  def test_autowidth
    style = @c.row.worksheet.workbook.styles.add_style({ alignment: { horizontal: :center, vertical: :center, wrap_text: true } })
    @c.style = style

    assert_in_delta(6.6, @c.autowidth, 0.01)
  end

  def test_autowidth_with_bold_font_multiplier
    style = @c.row.worksheet.workbook.styles.add_style(b: true)
    @c.row.worksheet.workbook.bold_font_multiplier = 1.05
    @c.style = style

    assert_in_delta(6.93, @c.autowidth, 0.01)
  end

  def test_autowidth_with_font_scale_divisor
    @c.row.worksheet.workbook.font_scale_divisor = 11.0

    assert_in_delta(6.0, @c.autowidth, 0.01)
  end

  def test_time
    @c.type = :time
    now = DateTime.now
    @c.value = now

    assert_equal(@c.value, now.to_time)
  end

  def test_date
    @c.type = :date
    now = Time.now
    @c.value = now

    assert_equal(@c.value, now.to_date)
  end

  def test_style
    assert_raise(ArgumentError, "must reject invalid style indexes") { @c.style = @c.row.worksheet.workbook.styles.cellXfs.size }
    assert_nothing_raised("must allow valid style index changes") { @c.style = 1 }
    assert_equal(1, @c.style)
  end

  def test_type
    assert_raise(ArgumentError, "type must be :string, :integer, :float, :date, :time, :boolean") { @c.type = :array }
    assert_nothing_raised("type can be changed") { @c.type = :string }
    assert_equal("1.0", @c.value, "changing type casts the value")
    assert_equal(:float, @row.add_cell(1.0 / (10**7)).type, 'properly identify exponential floats as float type')
    assert_equal(:time, @row.add_cell(Time.now).type, 'time should be time')
    assert_equal(:date, @row.add_cell(Date.today).type, 'date should be date')
    assert_equal(:boolean, @row.add_cell(true).type, 'boolean should be boolean')
  end

  def test_value
    assert_raise(ArgumentError, "type must be :string, :integer, :float, :date, :time, :boolean") { @c.type = :array }
    assert_nothing_raised("type can be changed") { @c.type = :string }
    assert_equal("1.0", @c.value, "changing type casts the value")
  end

  def test_col_ref
    # TODO: move to axlsx spec
    assert_equal("A", Axlsx.col_ref(0))
  end

  def test_cell_type_from_value
    assert_equal(:float, @c.send(:cell_type_from_value, 1.0))
    assert_equal(:float, @c.send(:cell_type_from_value, "1e1"))
    assert_equal(:float, @c.send(:cell_type_from_value, "1e#{Float::MAX_10_EXP}"))
    assert_equal(:string, @c.send(:cell_type_from_value, "1e#{Float::MAX_10_EXP + 1}"))
    assert_equal(:float, @c.send(:cell_type_from_value, "1e-1"))
    assert_equal(:float, @c.send(:cell_type_from_value, "1e#{Float::MIN_10_EXP}"))
    assert_equal(:string, @c.send(:cell_type_from_value, "1e#{Float::MIN_10_EXP - 1}"))
    assert_equal(:integer, @c.send(:cell_type_from_value, 1))
    assert_equal(:date, @c.send(:cell_type_from_value, Date.today))
    assert_equal(:time, @c.send(:cell_type_from_value, Time.now))
    assert_equal(:string, @c.send(:cell_type_from_value, []))
    assert_equal(:string, @c.send(:cell_type_from_value, "d"))
    assert_equal(:string, @c.send(:cell_type_from_value, nil))
    assert_equal(:integer, @c.send(:cell_type_from_value, -1))
    assert_equal(:boolean, @c.send(:cell_type_from_value, true))
    assert_equal(:boolean, @c.send(:cell_type_from_value, false))
    assert_equal(:float, @c.send(:cell_type_from_value, 1.0 / (10**6)))
    assert_equal(:richtext, @c.send(:cell_type_from_value, Axlsx::RichText.new))
    assert_equal(:iso_8601, @c.send(:cell_type_from_value, '2008-08-30T01:45:36.123+09:00'))
  end

  def test_cell_type_from_value_looks_like_number_but_is_not
    mimic_number = Class.new do
      def initialize(to_s_value)
        @to_s_value = to_s_value
      end

      def to_s
        @to_s_value
      end
    end

    number_strings = [
      '1',
      '1234567890',
      '1.0',
      '1e1',
      '0',
      "1e#{Float::MIN_10_EXP}"
    ]

    number_strings.each do |number_string|
      assert_equal(:string, @c.send(:cell_type_from_value, mimic_number.new(number_string)))
    end
  end

  def test_cast_value
    @c.type = :string

    assert_equal("1.0", @c.send(:cast_value, 1.0))
    @c.type = :integer

    assert_equal(1, @c.send(:cast_value, 1.0))
    @c.type = :float

    assert_in_delta(@c.send(:cast_value, "1.0"), 1.0)
    @c.type = :string

    assert_nil(@c.send(:cast_value, nil))
    @c.type = :richtext

    assert_nil(@c.send(:cast_value, nil))
    @c.type = :float

    assert_nil(@c.send(:cast_value, nil))
    @c.type = :boolean

    assert_equal(1, @c.send(:cast_value, true))
    assert_equal(0, @c.send(:cast_value, false))
    @c.type = :iso_8601

    assert_equal("2012-10-10T12:24", @c.send(:cast_value, "2012-10-10T12:24"))
  end

  def test_cast_time_subclass
    subtime = Class.new(Time) do
      def to_time
        raise "#to_time of Time subclass should not be called"
      end
    end

    time = subtime.now

    @c.type = :time

    assert_equal(time, @c.send(:cast_value, time))
  end

  def test_color
    assert_raise(ArgumentError) { @c.color = -1.1 }
    assert_nothing_raised { @c.color = "FF00FF00" }
    assert_equal("FF00FF00", @c.color.rgb)
  end

  def test_scheme
    assert_raise(ArgumentError) { @c.scheme = -1.1 }
    assert_nothing_raised { @c.scheme = :major }
    assert_equal(:major, @c.scheme)
  end

  def test_vertAlign
    assert_raise(ArgumentError) { @c.vertAlign = -1.1 }
    assert_nothing_raised { @c.vertAlign = :baseline }
    assert_equal(:baseline, @c.vertAlign)
  end

  def test_sz
    assert_raise(ArgumentError) { @c.sz = -1.1 }
    assert_nothing_raised { @c.sz = 12 }
    assert_equal(12, @c.sz)
  end

  def test_extend
    assert_raise(ArgumentError) { @c.extend = -1.1 }
    assert_nothing_raised { @c.extend = false }
    refute(@c.extend)
  end

  def test_condense
    assert_raise(ArgumentError) { @c.condense = -1.1 }
    assert_nothing_raised { @c.condense = false }
    refute(@c.condense)
  end

  def test_shadow
    assert_raise(ArgumentError) { @c.shadow = -1.1 }
    assert_nothing_raised { @c.shadow = false }
    refute(@c.shadow)
  end

  def test_outline
    assert_raise(ArgumentError) { @c.outline = -1.1 }
    assert_nothing_raised { @c.outline = false }
    refute(@c.outline)
  end

  def test_strike
    assert_raise(ArgumentError) { @c.strike = -1.1 }
    assert_nothing_raised { @c.strike = false }
    refute(@c.strike)
  end

  def test_u
    @c.type = :string
    assert_raise(ArgumentError) { @c.u = -1.1 }
    assert_nothing_raised { @c.u = :single }
    assert_equal(:single, @c.u)
    doc = Nokogiri::XML(@c.to_xml_string(1, 1))

    assert(doc.xpath('//u[@val="single"]'))
  end

  def test_i
    assert_raise(ArgumentError) { @c.i = -1.1 }
    assert_nothing_raised { @c.i = false }
    refute(@c.i)
  end

  def test_rFont
    assert_raise(ArgumentError) { @c.font_name = -1.1 }
    assert_nothing_raised { @c.font_name = "Arial" }
    assert_equal("Arial", @c.font_name)
  end

  def test_charset
    assert_raise(ArgumentError) { @c.charset = -1.1 }
    assert_nothing_raised { @c.charset = 1 }
    assert_equal(1, @c.charset)
  end

  def test_family
    assert_raise(ArgumentError) { @c.family = -1.1 }
    assert_nothing_raised { @c.family = 5 }
    assert_equal(5, @c.family)
  end

  def test_b
    assert_raise(ArgumentError) { @c.b = -1.1 }
    assert_nothing_raised { @c.b = false }
    refute(@c.b)
  end

  def test_merge_with_string
    @c.row.add_cell 2
    @c.row.add_cell 3
    @c.merge "A2"

    assert_equal("A1:A2", @c.row.worksheet.send(:merged_cells).last)
  end

  def test_merge_with_cell
    @c.row.add_cell 2
    @c.row.add_cell 3
    @c.merge @row.cells.last

    assert_equal("A1:C1", @c.row.worksheet.send(:merged_cells).last)
  end

  def test_reverse_merge_with_cell
    @c.row.add_cell 2
    @c.row.add_cell 3
    @row.cells.last.merge @c

    assert_equal("A1:C1", @c.row.worksheet.send(:merged_cells).last)
  end

  def test_ssti
    assert_raise(ArgumentError, "ssti must be an unsigned integer!") { @c.send(:ssti=, -1) }
    @c.send :ssti=, 1

    assert_equal(1, @c.ssti)
  end

  def test_plain_string
    @c.escape_formulas = false

    @c.type = :integer

    refute_predicate(@c, :plain_string?)

    @c.type = :string
    @c.value = 'plain string'

    assert_predicate(@c, :plain_string?)

    @c.value = nil

    refute_predicate(@c, :plain_string?)

    @c.value = ''

    refute_predicate(@c, :plain_string?)

    @c.value = '=sum'

    refute_predicate(@c, :plain_string?)

    @c.value = '{=sum}'

    refute_predicate(@c, :plain_string?)

    @c.escape_formulas = true

    @c.value = '=sum'

    assert_predicate(@c, :plain_string?)

    @c.value = '{=sum}'

    assert_predicate(@c, :plain_string?)

    @c.value = 'plain string'
    @c.font_name = 'Arial'

    refute_predicate(@c, :plain_string?)
  end

  def test_to_xml_string
    c_xml = Nokogiri::XML(@c.to_xml_string(1, 1))

    assert_equal(1, c_xml.xpath("/c[@s=1]").size)
  end

  def test_to_xml_string_nil
    @c.value = nil
    c_xml = Nokogiri::XML(@c.to_xml_string(1, 1))

    assert_equal(1, c_xml.xpath("/c[@s=1]").size)
  end

  def test_to_xml_string_with_run
    # Actually quite a number of similar run styles
    # but the processing should be the same
    @c.b = true
    @c.type = :string
    @c.value = "a"
    @c.font_name = 'arial'
    @c.color = 'FF0000'
    c_xml = Nokogiri::XML(@c.to_xml_string(1, 1))

    assert_predicate(c_xml.xpath("//b"), :any?)
  end

  def test_to_xml_string_formula
    p = Axlsx::Package.new
    ws = p.workbook.add_worksheet(escape_formulas: false) do |sheet|
      sheet.add_row ["=IF(2+2=4,4,5)"]
    end
    doc = Nokogiri::XML(ws.to_xml_string)
    doc.remove_namespaces!

    assert_predicate(doc.xpath("//f[text()='IF(2+2=4,4,5)']"), :any?)
  end

  def test_to_xml_string_formula_escaped
    p = Axlsx::Package.new
    ws = p.workbook.add_worksheet do |sheet|
      sheet.add_row ["=IF(2+2=4,4,5)"], escape_formulas: true
    end
    doc = Nokogiri::XML(ws.to_xml_string)
    doc.remove_namespaces!

    assert_predicate(doc.xpath("//t[text()='=IF(2+2=4,4,5)']"), :any?)
  end

  def test_to_xml_string_numeric_escaped
    p = Axlsx::Package.new
    ws = p.workbook.add_worksheet do |sheet|
      sheet.add_row ["-1", "+2"], escape_formulas: true, types: :text
    end
    doc = Nokogiri::XML(ws.to_xml_string)
    doc.remove_namespaces!

    assert_predicate(doc.xpath("//t[text()='-1']"), :any?)
    assert_predicate(doc.xpath("//t[text()='+2']"), :any?)
  end

  def test_to_xml_string_owasp_prefixes_that_are_no_excel_formulas
    # OWASP mentions various prefixes that might designate formulas when data is read as CSV:
    # https://owasp.org/www-community/attacks/CSV_Injection
    # Except for `=` none of these prefixes are valid prefixes for formulas in Excel however,
    # so they should never be interpreted / serialized as formulas by Caxlsx.
    p = Axlsx::Package.new
    ws = p.workbook.add_worksheet do |sheet|
      sheet.add_row [
        "@1",
        "%2",
        "|3",
        "\rfoo",
        "\tbar"
      ], escape_formulas: false
    end
    doc = Nokogiri::XML(ws.to_xml_string)
    doc.remove_namespaces!

    assert_predicate(doc.xpath("//t[text()='@1']"), :any?)
    assert_predicate(doc.xpath("//t[text()='%2']"), :any?)
    assert_predicate(doc.xpath("//t[text()='|3']"), :any?)
    assert_predicate(doc.xpath("//t[text()='\nfoo']"), :any?)
    assert_predicate(doc.xpath("//t[text()='\tbar']"), :any?)
  end

  def test_to_xml_string_owasp_prefixes_that_are_no_excel_formulas_with_escape_formulas
    # OWASP mentions various prefixes that might designate formulas when data is read as CSV:
    # https://owasp.org/www-community/attacks/CSV_Injection
    # Except for `=` none of these prefixes are valid prefixes for formulas in Excel however,
    # so they should never be interpreted / serialized as formulas by Caxlsx.
    p = Axlsx::Package.new
    ws = p.workbook.add_worksheet do |sheet|
      sheet.add_row [
        "@1",
        "%2",
        "|3",
        "\rfoo",
        "\tbar"
      ], escape_formulas: true
    end
    doc = Nokogiri::XML(ws.to_xml_string)
    doc.remove_namespaces!

    assert_predicate(doc.xpath("//t[text()='@1']"), :any?)
    assert_predicate(doc.xpath("//t[text()='%2']"), :any?)
    assert_predicate(doc.xpath("//t[text()='|3']"), :any?)
    assert_predicate(doc.xpath("//t[text()='\nfoo']"), :any?)
    assert_predicate(doc.xpath("//t[text()='\tbar']"), :any?)
  end

  def test_to_xml_string_formula_escape_array_parameter
    p = Axlsx::Package.new
    ws = p.workbook.add_worksheet do |sheet|
      sheet.add_row [
        "=IF(2+2=4,4,5)",
        "=IF(13+13=4,4,5)",
        "=IF(99+99=4,4,5)"
      ], escape_formulas: [true, false, true]
    end
    doc = Nokogiri::XML(ws.to_xml_string)
    doc.remove_namespaces!

    assert_predicate(doc.xpath("//t[text()='=IF(2+2=4,4,5)']"), :any?)
    assert_predicate(doc.xpath("//f[text()='IF(13+13=4,4,5)']"), :any?)
    assert_predicate(doc.xpath("//t[text()='=IF(99+99=4,4,5)']"), :any?)
  end

  def test_to_xml_string_array_formula
    p = Axlsx::Package.new
    ws = p.workbook.add_worksheet(escape_formulas: false) do |sheet|
      sheet.add_row ["{=SUM(C2:C11*D2:D11)}"]
    end
    doc = Nokogiri::XML(ws.to_xml_string)
    doc.remove_namespaces!

    assert_predicate(doc.xpath("//f[text()='SUM(C2:C11*D2:D11)']"), :any?)
    assert_predicate(doc.xpath("//f[@t='array']"), :any?)
    assert_predicate(doc.xpath("//f[@ref='A1']"), :any?)
  end

  def test_to_xml_string_text_formula
    p = Axlsx::Package.new
    ws = p.workbook.add_worksheet do |sheet|
      sheet.add_row ["=1+1", "-1+1"], types: :text
    end
    doc = Nokogiri::XML(ws.to_xml_string)
    doc.remove_namespaces!

    assert_empty(doc.xpath("//f[text()='1+1']"))
    assert_predicate(doc.xpath("//t[text()='=1+1']"), :any?)

    assert_empty(doc.xpath("//f[text()='1+1']"))
    assert_predicate(doc.xpath("//t[text()='-1+1']"), :any?)
  end

  def test_font_size_with_custom_style_and_no_sz
    @c.style = @c.row.worksheet.workbook.styles.add_style bg_color: 'FF00FF'
    sz = @c.send(:font_size)

    assert_equal(sz, @c.row.worksheet.workbook.styles.fonts.first.sz)
  end

  def test_font_size_with_bolding
    @c.style = @c.row.worksheet.workbook.styles.add_style b: true

    assert_equal(@c.row.worksheet.workbook.styles.fonts.first.sz * 1.5, @c.send(:font_size))
  end

  def test_font_size_with_custom_sz
    @c.style = @c.row.worksheet.workbook.styles.add_style sz: 52
    sz = @c.send(:font_size)

    assert_equal(52, sz)
  end

  def test_cell_with_sz
    @c.sz = 25

    assert_equal(25, @c.send(:font_size))
  end

  def test_to_xml
    # TODO: This could use some much more stringent testing related to the xml content generated!
    @ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)", "2013-01-13T13:31:25.123"]
    @ws.rows.last.cells[5].u = :single

    schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
    doc = Nokogiri::XML(@ws.to_xml_string)
    errors = []
    schema.validate(doc).each do |error|
      errors.push error
      puts error.message
    end

    assert_empty(errors, "error free validation")
  end
end