Class: RubyFromExcel::Cell

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

Direct Known Subclasses

FormulaCell, ValueCell

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worksheet, xml = nil) ⇒ Cell

Returns a new instance of Cell.



6
7
8
9
10
# File 'lib/cells/cell.rb', line 6

def initialize(worksheet,xml = nil)
  self.worksheet = worksheet
  parse_xml xml
  debug
end

Instance Attribute Details

#dependenciesObject

Returns the value of attribute dependencies.



4
5
6
# File 'lib/cells/cell.rb', line 4

def dependencies
  @dependencies
end

#referenceObject

Returns the value of attribute reference.



4
5
6
# File 'lib/cells/cell.rb', line 4

def reference
  @reference
end

#worksheetObject

Returns the value of attribute worksheet.



4
5
6
# File 'lib/cells/cell.rb', line 4

def worksheet
  @worksheet
end

#xml_typeObject

Returns the value of attribute xml_type.



4
5
6
# File 'lib/cells/cell.rb', line 4

def xml_type
  @xml_type
end

#xml_valueObject

Returns the value of attribute xml_value.



4
5
6
# File 'lib/cells/cell.rb', line 4

def xml_value
  @xml_value
end

Instance Method Details

#alter_other_cells_if_requiredObject



19
20
21
# File 'lib/cells/cell.rb', line 19

def alter_other_cells_if_required
  # nil
end

#can_be_replaced_with_value?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cells/cell.rb', line 50

def can_be_replaced_with_value?
  false
end

#debugObject



98
99
100
# File 'lib/cells/cell.rb', line 98

def debug
  RubyFromExcel.debug(:cells,"#{worksheet.name}.#{reference} -> #{} -> #{xml_value} (#{xml_type})")
end

#inspectObject



94
95
96
# File 'lib/cells/cell.rb', line 94

def inspect
  "(cell: #{to_s} with formula:#{respond_to?(:ast) ? ast.inspect : "na"} and value '#{value}' of type '#{xml_type}')"
end

#parse_xml(xml) ⇒ Object



12
13
14
15
16
17
# File 'lib/cells/cell.rb', line 12

def parse_xml(xml)
  return nil unless xml
  self.reference = Reference.new(xml['r'],worksheet)
  self.xml_value = xml.at_css("v").content
  self.xml_type = xml['t']
end

#testObject



39
40
41
42
# File 'lib/cells/cell.rb', line 39

def test
  return "== #{value}" if xml_type
  "be_within(#{tolerance_for(value.to_f)}).of(#{value.to_f})"
end

#to_ruby(r = RubyScriptWriter.new) ⇒ Object



27
28
29
30
# File 'lib/cells/cell.rb', line 27

def to_ruby(r = RubyScriptWriter.new)
  r.put_simple_method reference.to_ruby, ruby_value
  r.to_s
end

#to_sObject



90
91
92
# File 'lib/cells/cell.rb', line 90

def to_s
  "#{worksheet}.#{reference}"
end

#to_test(r = RubySpecWriter.new) ⇒ Object



32
33
34
35
36
37
# File 'lib/cells/cell.rb', line 32

def to_test(r = RubySpecWriter.new)
  r.put_spec "cell #{reference.to_ruby} should equal #{value}" do
    r.puts "#{worksheet}.#{reference.to_ruby}.should #{test}"
  end
  r.to_s
end

#tolerance_for(value, tolerance = 0.1, maximum = 1e-8) ⇒ Object



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

def tolerance_for(value,tolerance = 0.1, maximum = 1e-8)
  tolerance = value.abs * tolerance
  tolerance = maximum if tolerance < maximum
  tolerance
end

#valueObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cells/cell.rb', line 54

def value
  value = xml_value
  case xml_type
  when 'str'
    return value.inspect
  when 's'
    return SharedStrings.shared_string_for(value.to_i).inspect
  when 'e'
    return ":#{value.gsub(/[^a-zA-Z]/,'').downcase}"
  when 'b'
    return 'true' if value == '1'
    return 'false' if value == '0'
    return Formula.parse(value).visit(FormulaBuilder.new)
  else
    return Formula.parse(value).visit(FormulaBuilder.new)
  end
end

#value_for_includingObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cells/cell.rb', line 72

def value_for_including
  value = xml_value
  case xml_type
  when 'str'
    return value
  when 's'
    return SharedStrings.shared_string_for(value.to_i)
  when 'e'
    return ":#{value.gsub(/[^a-zA-Z]/,'').downcase}"
  when 'b'
    return 'true' if value == '1'
    return 'false' if value == '0'
    return Formula.parse(value).visit(FormulaBuilder.new)
  else
    return Formula.parse(value).visit(FormulaBuilder.new)
  end    
end

#work_out_dependenciesObject



23
24
25
# File 'lib/cells/cell.rb', line 23

def work_out_dependencies
  self.dependencies = []
end