Class: NumberCell

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

Instance Attribute Summary

Attributes inherited from Cell

#index

Instance Method Summary collapse

Methods inherited from Cell

#col, #row, #set_style

Constructor Details

#initialize(parent, index, format_index, number) ⇒ NumberCell

Returns a new instance of NumberCell.



44
45
46
47
48
49
# File 'lib/surpass/cell.rb', line 44

def initialize(parent, index, format_index, number)
  @parent = parent
  @index = index
  @format_index = format_index
  @number = number
end

Instance Method Details

#rk_record(rk_encoded) ⇒ Object



51
52
53
# File 'lib/surpass/cell.rb', line 51

def rk_record(rk_encoded)
  RKRecord.new(@parent.index, @index, @format_index, rk_encoded).to_biff
end

#to_biffObject

TODO test this section to be sure numbers are categorized and packed correctly.



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

def to_biff
  # 30 bit signed int
  in_range = (-0x20000000 <= @number) && (@number < 0x20000000)
  is_int = (@number.to_i == @number)
  if in_range && is_int
    rk_encoded = 2 | (@number.to_i << 2)
    return rk_record(rk_encoded)
  end
  
  # try scaling by 100 then using a 30 bit signed int
  in_range = (-0x20000000 <= @number * 100) && (@number * 100 < 0x20000000)
  round_trip = (@number.to_i*100) == @number*100
  if in_range && round_trip
    rk_encoded = (3 | (@number.to_i*100 << 2))
    return rk_record(rk_encoded)
  end
  
  w0, w1, w2, w3 = [@number].pack('E').unpack('v4')
  
  is_float_rk = (w0 == 0) && (w1 == 0) && (w2 & 0xFFFC) == w2
  if is_float_rk
    rk_encoded = (w3 << 16) | w2
    return rk_record(rk_encoded)
  end
  
  w0, w1, w2, w3 = [@number * 100].pack('E').unpack('v4')

  is_float_rk_100 = w0 == 0 && w1 == 0 && w2 & 0xFFFC == w2
  if is_float_rk_100
    rk_encoded = 1 | (w3 << 16) | w2
    return rk_record(rk_encoded)
  end

  # If not an RK value, use a NumberRecord instead.
  NumberRecord.new(@parent.index, @index, @format_index, @number).to_biff
end