File: worksheet.rb

package info (click to toggle)
ruby-spreadsheet 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,964 kB
  • sloc: ruby: 6,943; makefile: 10
file content (147 lines) | stat: -rw-r--r-- 4,586 bytes parent folder | download
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
#!/usr/bin/env ruby
# TestWorksheet -- Spreadheet -- 30.09.2008 -- hwyss@ywesee.com

$: << File.expand_path('../lib', File.dirname(__FILE__))

require 'test/unit'
require 'spreadsheet'

module Spreadsheet
  class TestWorksheet < Test::Unit::TestCase
    def setup
      @book = Workbook.new
      @sheet = @book.create_worksheet
    end
    def test_cell_writer
      assert_nil @sheet[0,0]
      assert_equal 0, @sheet.column_count
      assert_equal 0, @sheet.row_count
      @sheet[0,0] = 'foo'
      assert_equal 'foo', @sheet[0,0]
      assert_equal 1, @sheet.column_count
      assert_equal 1, @sheet.row_count
      @sheet[1,0] = 'bar'
      assert_equal 1, @sheet.column_count
      assert_equal 2, @sheet.row_count
      @sheet[0,1] = 'bar'
      assert_equal 2, @sheet.column_count
      assert_equal 2, @sheet.row_count
      @sheet[1,0] = nil
      assert_equal 2, @sheet.column_count
      assert_equal 2, @sheet.row_count
      @sheet[0,1] = nil
      assert_equal 2, @sheet.column_count
      assert_equal 2, @sheet.row_count
    end
    def test_column_count
      assert_equal 0, @sheet.column_count
      @sheet.replace_row 3, nil, nil, 1, 2, 'foo, bar'
      assert_equal 3, @sheet.column_count
      @sheet.replace_row 8, nil, 'something', 4, 7, nil
      assert_equal 4, @sheet.column_count
      @sheet.replace_row 5, 4, 'something', 4, 7, nil
      assert_equal 5, @sheet.column_count
      @sheet.replace_row 5, nil, 'something', 4, 7, nil
      assert_equal 4, @sheet.column_count
      @sheet.replace_row 3
      assert_equal 4, @sheet.column_count
    end
    def test_row_count
      assert_equal 0, @sheet.row_count
      @sheet.replace_row 3, nil, nil, 1, 2, 'foo, bar'
      assert_equal 1, @sheet.row_count
      @sheet.replace_row 8, nil, 'something', 4, 7, nil
      assert_equal 6, @sheet.row_count
      @sheet.replace_row 5, 4, 'something', 4, 7, nil
      assert_equal 6, @sheet.row_count
      @sheet.replace_row 5, nil, 'something', 4, 7, nil
      assert_equal 6, @sheet.row_count
      @sheet.replace_row 3
      assert_equal 6, @sheet.row_count
      @sheet.delete_row 3
      assert_equal 5, @sheet.row_count
      @sheet.delete_row 3
      assert_equal 4, @sheet.row_count
      @sheet.delete_row 2
      assert_equal 4, @sheet.row_count
      @sheet.delete_row 2
      assert_equal 3, @sheet.row_count
    end
    def test_modify_column
      assert_equal 10, @sheet.column(0).width
      @sheet.column(1).width = 20
      assert_equal 10, @sheet.column(0).width
      assert_equal 20, @sheet.column(1).width
      @sheet.column(0).width = 30
      assert_equal 30, @sheet.column(0).width
      assert_equal 20, @sheet.column(1).width
    end
    def test_format_dates!
      rowi = -1

      @sheet.format_dates!
      # No dates = no new formats
      assert_equal 1, @book.formats.length # Default format

      @sheet.row(rowi+=1).concat(["Hello", "World"])
      @sheet.format_dates!
      # No dates = no new formats
      assert_equal 1, @book.formats.length

      @sheet.row(rowi+=1).concat([Date.new(2010,1,1)])
      @sheet.format_dates!
      # 1 date = 1 new format
      assert_equal 2, @book.formats.length

      @sheet.row(rowi+=1).concat([Date.new(2011,1,1)])
      @sheet.row(rowi+=1).concat([Date.new(2012,1,1)])
      @sheet.row(rowi+=1).concat([Date.new(2013,1,1)])
      @sheet.format_dates!
      # 4 dates = only 1 new format across them:
      assert_equal 3, @book.formats.length

      @sheet.row(rowi+=1).concat([Date.new(2014,1,1)])
      @sheet.row(rowi).default_format = Format.new
      @sheet.row(rowi+=1).concat([Date.new(2015,1,1)])
      @sheet.format_dates!
      # 6 dates = 2 new formats across them:
      assert_equal 6, @book.formats.length

    end

    def test_freeze_panel!
      assert_equal 0, @sheet.froze_top
      assert_equal 0, @sheet.froze_left
      assert_equal false, @sheet.has_frozen_panel?

      @sheet.freeze!(2, 3)
      assert_equal 2, @sheet.froze_top
      assert_equal 3, @sheet.froze_left
      assert_equal true, @sheet.has_frozen_panel?

    end

    def test_each_with_skip
      @sheet[0, 0] = 'foo'
      @sheet[1, 0] = 'bar'

      assert_equal @sheet.each(1).count, 1
      assert_equal @sheet.each(1).first[0], 'bar'
    end

    def test_each_with_index
      @sheet[0, 0] = 'foo'
      @sheet[1, 0] = 'bar'

      @sheet.each.with_index do |row, index|
        assert_equal row[0], @sheet[index, 0]
      end
    end

    def test_name
      worksheet = Worksheet.new(name: '\a/b*c?d:e[f]')
      assert_equal '_a_b_c_d_e_f_', worksheet.name
    end

  end
end