Class: Rspreadsheet::Cell

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aparent_row, coli, asource_node = nil) ⇒ Cell

Returns a new instance of Cell.



12
13
14
15
16
17
18
19
20
21
# File 'lib/rspreadsheet/cell.rb', line 12

def initialize(aparent_row,coli,asource_node=nil)
  raise "First parameter should be Row object not #{aparent_row.class}" unless aparent_row.kind_of?(Rspreadsheet::Row)
  @mode = :regular
  @parent_row = aparent_row
  if asource_node.nil?
    asource_node = Cell.empty_cell_node
  end
  @xmlnode = asource_node
  @col = coli
end

Instance Attribute Details

#colObject (readonly)

Returns the value of attribute col.



6
7
8
# File 'lib/rspreadsheet/cell.rb', line 6

def col
  @col
end

#modeObject

Returns the value of attribute mode.



8
9
10
# File 'lib/rspreadsheet/cell.rb', line 8

def mode
  @mode
end

#parent_rowObject (readonly)

for debug only



7
8
9
# File 'lib/rspreadsheet/cell.rb', line 7

def parent_row
  @parent_row
end

#xmlnodeObject (readonly)

Returns the value of attribute xmlnode.



6
7
8
# File 'lib/rspreadsheet/cell.rb', line 6

def xmlnode
  @xmlnode
end

Class Method Details

.empty_cell_nodeObject



9
10
11
# File 'lib/rspreadsheet/cell.rb', line 9

def self.empty_cell_node
  LibXML::XML::Node.new('table-cell',nil, Tools.get_namespace('table'))
end

Instance Method Details

#coordinatesObject



28
# File 'lib/rspreadsheet/cell.rb', line 28

def coordinates; [row,col]; end

#guess_cell_type(avalue = nil) ⇒ Object

based on @xmlnode and optionally value which is about to be assigned, guesses which type the result should be



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

def guess_cell_type(avalue=nil)
  # try guessing by value
  valueguess = case avalue
    when Numeric then Float
    when Date then Date
    when String,nil then nil
    else nil
  end
  result = valueguess
  
  if valueguess.nil? # valueguess is most important
    # if not succesfull then try guessing by type
    type = @xmlnode.attributes['value-type'].to_s
    typeguess = case type
      when 'float' then Float
      when 'string' then String
      when 'date' then Date
      when 'percentage' then 'percentage'
      else 
        if @xmlnode.children.size == 0
          nil
        else 
          raise "Unknown type from #{@xmlnode.to_s} / children size=#{@xmlnode.children.size.to_s} / type=#{type}"
        end
    end
    
    result =
    if !typeguess.nil? # if not certain by value, but have a typeguess
      if !avalue.nil?  # with value we may try converting
        if (typeguess(avalue) rescue false) # if convertible then it is typeguess
          typeguess
        elsif (String(avalue) rescue false) # otherwise try string
          String
        else # if not convertible to anyhing concious then nil
          nil 
        end
      else             # without value we just beleive typeguess
        typeguess
      end
    else # it not have a typeguess
      if (String(avalue) rescue false) # convertible to String
        String
      else
        nil
      end
    end
  end
  result
end

#inspectObject



141
142
143
# File 'lib/rspreadsheet/cell.rb', line 141

def inspect
  "#<Cell:[#{row},#{col}]=#{value}(#{guess_cell_type.to_s})"
end

#ns_officeObject



23
# File 'lib/rspreadsheet/cell.rb', line 23

def ns_office; @parent_row.xmlnode.doc.root.namespaces.find_by_prefix('office') end

#ns_tableObject



22
# File 'lib/rspreadsheet/cell.rb', line 22

def ns_table; @parent_row.xmlnode.doc.root.namespaces.find_by_prefix('table') end

#ns_textObject



24
# File 'lib/rspreadsheet/cell.rb', line 24

def ns_text; @parent_row.xmlnode.doc.root.namespaces.find_by_prefix('text') end

#remove_all_value_attributes_and_content(node) ⇒ Object



82
83
84
85
86
# File 'lib/rspreadsheet/cell.rb', line 82

def remove_all_value_attributes_and_content(node)
  if att = Tools.get_ns_attribute(@xmlnode, 'office','value') then att.remove! end
  if att = Tools.get_ns_attribute(@xmlnode, 'office','date-value') then att.remove! end
  @xmlnode.content=''
end

#rowObject



29
# File 'lib/rspreadsheet/cell.rb', line 29

def row; @parent_row.row; end

#set_type_attribute(typestring) ⇒ Object



87
88
89
# File 'lib/rspreadsheet/cell.rb', line 87

def set_type_attribute(typestring)
  Tools.set_ns_attribute(@xmlnode,'office','value-type',typestring)
end

#to_sObject



25
# File 'lib/rspreadsheet/cell.rb', line 25

def to_s; value end

#valueObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rspreadsheet/cell.rb', line 30

def value
  gt = guess_cell_type
  if (@mode == :regular) or (@mode == @repeated)
    case 
      when gt == nil then nil
      when gt == Float then @xmlnode.attributes['value'].to_f
      when gt == String then @xmlnode.elements.first.andand.content.to_s
      when gt == Date then Date.strptime(@xmlnode.attributes['date-value'].to_s, '%Y-%m-%d')
      when gt == 'percentage' then @xmlnode.attributes['value'].to_f
    end
  elsif @mode == :outbound # needs to check whether it is still unbound
    if parent_row.still_out_of_used_range?
      nil
    else
      parent_row.normalize.cells(@col).value
    end  
  else
    raise "Unknown cell mode #{@mode}"
  end
end

#value=(avalue) ⇒ Object



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

def value=(avalue)
  if @mode == :regular
    gt = guess_cell_type(avalue)
    case
      when gt == nil then raise 'This value type is not storable to cell'
      when gt == Float 
        set_type_attribute('float')
        remove_all_value_attributes_and_content(@xmlnode)
        Tools.set_ns_attribute(@xmlnode,'office','value', avalue.to_s) 
        @xmlnode << LibXML::XML::Node.new('p', avalue.to_f.to_s, ns_text)
      when gt == String then
        set_type_attribute('string')
        remove_all_value_attributes_and_content(@xmlnode)
        @xmlnode << LibXML::XML::Node.new('p', avalue.to_s, ns_text)
      when gt == Date then 
        set_type_attribute('date')
        remove_all_value_attributes_and_content(@xmlnode)
        Tools.set_ns_attribute(@xmlnode,'office','date-value', avalue.strftime('%Y-%m-%d'))
        @xmlnode << LibXML::XML::Node.new('p', avalue.strftime('%Y-%m-%d'), ns_text) 
      when gt == 'percentage'
        set_type_attribute('float')
        remove_all_value_attributes_and_content(@xmlnode)
        Tools.set_ns_attribute(@xmlnode,'office','value', avalue.to_f.to_s) 
        @xmlnode << LibXML::XML::Node.new('p', (avalue.to_f*100).round.to_s+'%', ns_text)
    end
  elsif (@mode == :repeated) or (@mode == :outbound ) # Cell did not exist individually yet, detach row and create editable cell
    row = @parent_row.detach
    row.cells(@col).value = avalue
  else
    raise "Unknown cell mode #{@mode}"
  end
end

#value_xmlObject



27
# File 'lib/rspreadsheet/cell.rb', line 27

def value_xml; self.source_node.children.first.children.first.to_s; end

#xmlObject



26
# File 'lib/rspreadsheet/cell.rb', line 26

def xml; self.source_node.to_s; end