Class: Row

Inherits:
Object
  • Object
show all
Defined in:
lib/surpass/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, parent_sheet) ⇒ Row

Returns a new instance of Row.



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
# File 'lib/surpass/row.rb', line 22

def initialize(index, parent_sheet)
  is_int = index.is_a?(Integer)
  in_range = (index >= 0) && (index <= 65535)
  raise "row index #{index} is not valid" unless is_int && in_range
  
  @index = index
  @parent = parent_sheet
  @parent_wb = parent_sheet.parent()
  @cells = []
  @min_col_index = 0
  @max_col_index = 0
  @total_str = 0
  @xf_index = 0x0F
  @has_default_format = 0
  @height_in_pixels = 0x11
  
  @height = 0x00FF
  @has_default_height = 0x00
  @height_mismatch = 0
  @level = 0
  @collapse = 0
  @hidden = 0
  @space_above = 0
  @space_below = 0
end

Instance Attribute Details

#cellsObject

Returns the value of attribute cells.



5
6
7
# File 'lib/surpass/row.rb', line 5

def cells
  @cells
end

#collapseObject

Returns the value of attribute collapse.



17
18
19
# File 'lib/surpass/row.rb', line 17

def collapse
  @collapse
end

#has_default_formatObject

Returns the value of attribute has_default_format.



10
11
12
# File 'lib/surpass/row.rb', line 10

def has_default_format
  @has_default_format
end

#has_default_heightObject

Returns the value of attribute has_default_height.



14
15
16
# File 'lib/surpass/row.rb', line 14

def has_default_height
  @has_default_height
end

#heightObject

Returns the value of attribute height.



13
14
15
# File 'lib/surpass/row.rb', line 13

def height
  @height
end

#height_in_pixelsObject

Returns the value of attribute height_in_pixels.



11
12
13
# File 'lib/surpass/row.rb', line 11

def height_in_pixels
  @height_in_pixels
end

#height_mismatchObject

Returns the value of attribute height_mismatch.



15
16
17
# File 'lib/surpass/row.rb', line 15

def height_mismatch
  @height_mismatch
end

#hiddenObject

Returns the value of attribute hidden.



18
19
20
# File 'lib/surpass/row.rb', line 18

def hidden
  @hidden
end

#indexObject

Returns the value of attribute index.



2
3
4
# File 'lib/surpass/row.rb', line 2

def index
  @index
end

#levelObject

Returns the value of attribute level.



16
17
18
# File 'lib/surpass/row.rb', line 16

def level
  @level
end

#max_col_indexObject

Returns the value of attribute max_col_index.



7
8
9
# File 'lib/surpass/row.rb', line 7

def max_col_index
  @max_col_index
end

#min_col_indexObject

Returns the value of attribute min_col_index.



6
7
8
# File 'lib/surpass/row.rb', line 6

def min_col_index
  @min_col_index
end

#parentObject

Returns the value of attribute parent.



3
4
5
# File 'lib/surpass/row.rb', line 3

def parent
  @parent
end

#parent_wbObject

Returns the value of attribute parent_wb.



4
5
6
# File 'lib/surpass/row.rb', line 4

def parent_wb
  @parent_wb
end

#space_aboveObject

Returns the value of attribute space_above.



19
20
21
# File 'lib/surpass/row.rb', line 19

def space_above
  @space_above
end

#space_belowObject

Returns the value of attribute space_below.



20
21
22
# File 'lib/surpass/row.rb', line 20

def space_below
  @space_below
end

#total_strObject

Returns the value of attribute total_str.



8
9
10
# File 'lib/surpass/row.rb', line 8

def total_str
  @total_str
end

#xf_indexObject

Returns the value of attribute xf_index.



9
10
11
# File 'lib/surpass/row.rb', line 9

def xf_index
  @xf_index
end

Instance Method Details

#adjust_boundary_column_indexes(*args) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/surpass/row.rb', line 63

def adjust_boundary_column_indexes(*args)
  args.each do |a|
    is_int = (a.to_i == a)
    in_range = (0 <= a) && (a <= 255)
    raise "invalid boundary index #{a}" unless is_int && in_range
    @min_col_index = a if a < @min_col_index
    @max_col_index = a if a > @max_col_index
  end
end

#adjust_height(style) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/surpass/row.rb', line 48

def adjust_height(style)
  twips = style.font.height
  points = twips/20.0
  # Cell height in pixels can be calcuted by following approx. formula:
  # cell height in pixels = font height in points * 83/50 + 2/5
  # It works when screen resolution is 96 dpi 
  pix = (points*83.0/50.0 + 2.0/5.0).round
  @height_in_pixels = pix if (pix > @height_in_pixels)
end

#cell(col_index) ⇒ Object



106
107
108
# File 'lib/surpass/row.rb', line 106

def cell(col_index)
  cells.select {|c| c.index == col_index}.first
end

#cells_biffObject



102
103
104
# File 'lib/surpass/row.rb', line 102

def cells_biff
  cells.collect {|c| c.to_biff }.join
end

#cells_countObject



80
81
82
# File 'lib/surpass/row.rb', line 80

def cells_count
  @cells.length
end

#set_height(height) ⇒ Object



58
59
60
61
# File 'lib/surpass/row.rb', line 58

def set_height(height)
  @height = height * 20 #This seems to correspond to row height in excel.
  @height_mismatch = 1
end

#style=(style) ⇒ Object

TODO can we get rid of this? Tests pass if it is commented out.



74
75
76
77
78
# File 'lib/surpass/row.rb', line 74

def style=(style)
  adjust_height(style)
  @xf_index = @parent_wb.styles.add(style)
  @has_default_format = 1
end

#to_biffObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/surpass/row.rb', line 84

def to_biff
  height_options = (@height & 0x07FFF)
  height_options |= (@has_default_height & 0x01) << 15
  
  options =  (@level & 0x07) << 0
  options |= (@collapse & 0x01) << 4
  options |= (@hidden & 0x01) << 5
  options |= (@height_mismatch & 0x01) << 6
  options |= (@has_default_format & 0x01) << 7
  options |= (0x01 & 0x01) << 8
  options |= (@xf_index & 0x0FFF) << 16
  options |= (@space_above & 0x01) << 28
  options |= (@space_below & 0x01) << 29

  args = [@index, @min_col_index, @max_col_index, height_options, options]
  RowRecord.new(*args).to_biff
end

#write(col, label, style) ⇒ Object



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
# File 'lib/surpass/row.rb', line 110

def write(col, label, style)
  case style
  when StyleFormat
    # leave it alone
  when Hash
    style = StyleFormat.new(style)
### @export "autoformats"
  when TrueClass # Automatically apply a nice numeric format.
    case label
    when DateTime, Time
      style = @parent_wb.styles.default_datetime_style
    when Date
      style = @parent_wb.styles.default_date_style
    when Float
      style = @parent_wb.styles.default_float_style
    else
      style = @parent_wb.styles.default_style
    end
### @end
  when NilClass
    style = @parent_wb.styles.default_style
  else
    raise "I don't know how to use this to format a cell #{style.inspect}"
  end

  style_index = @parent_wb.styles.add(style)

  raise "trying to write to cell #{self.index}, #{col} - already exists!" if cell(col)
  
  adjust_height(style)
  adjust_boundary_column_indexes(col)

  case label
  when TrueClass, FalseClass
    @cells << BooleanCell.new(self, col, style_index, label)
  when String, NilClass
    if label.to_s.length == 0
      @cells << BlankCell.new(self, col, style_index)
    else
      @cells << StringCell.new(self, col, style_index, @parent_wb.sst.add_str(label))
      @total_str += 1
    end
  when Numeric
    @cells << NumberCell.new(self, col, style_index, label)
  when Date, DateTime, Time
    @cells << NumberCell.new(self, col, style_index, as_excel_date(label))
  when Formula
    @cells << FormulaCell.new(self, col, style_index, label)
  else
    raise "You are trying to write an object of class #{label.class.name} to a spreadsheet. Please convert this to a supported class such as String."
  end
end

#write_blanks(c1, c2, style) ⇒ Object



163
164
165
166
167
168
# File 'lib/surpass/row.rb', line 163

def write_blanks(c1, c2, style)
  raise unless c1 <= c2
  adjust_height(style)
  adjust_boundary_column_indexes(c1, c2)
  @cells << MulBlankCell.new(self, c1, c2, @parent_wb.styles.add(style))
end