Class: Rspreadsheet::Worksheet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rspreadsheet/worksheet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_node = nil) ⇒ Worksheet

Returns a new instance of Worksheet.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rspreadsheet/worksheet.rb', line 12

def initialize(source_node=nil)
  @source_node = source_node
  ## initialize cells
  @cells = Hash.new do |hash, coords|
    # we create empty cell and place it to hash, we do not have to check whether there is a cell in XML already, because it would be in hash as well
    hash[coords]=Cell.new(coords[0],coords[1])
    # TODO: create XML empty node here or upon save?
  end
  rowi = 1
  unless @source_node.nil?
    @source_node.elements.select{ |node| node.name == 'table-row'}.each do |row_source_node|
      coli = 1
      row_source_node.elements.select{ |node| node.name == 'table-cell'}.each do |cell_source_node|
        initialize_cell(rowi,coli,cell_source_node)
        coli += 1
      end
      rowi += 1
    end
  end
  ## initialize rows
  @spredsheetrows=Array.new()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

allows syntax like sheet.F15



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rspreadsheet/worksheet.rb', line 54

def method_missing method_name, *args, &block
  if method_name.to_s.match(/^([A-Z]{1,3})(\d{1,8})(=?)$/)
    row,col = Rspreadsheet::Tools.convert_cell_address($~[1],$~[2])
    assignchar = $~[3]
    if assignchar == '='
      self.cells(row,col).value = args.first
    else
      self.cells(row,col).value
    end
  else
    super
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#[](r, c) ⇒ Object

syntactic sugar follows



47
48
49
# File 'lib/rspreadsheet/worksheet.rb', line 47

def [](r,c)
  cells(r,c).value
end

#[]=(r, c, avalue) ⇒ Object



50
51
52
# File 'lib/rspreadsheet/worksheet.rb', line 50

def []=(r,c,avalue)
  cells(r,c).value=avalue
end

#cells(r, c) ⇒ Object



37
38
39
# File 'lib/rspreadsheet/worksheet.rb', line 37

def cells(r,c)
  @cells[[r,c]]
end

#initialize_cell(r, c, source_node) ⇒ Object



34
35
36
# File 'lib/rspreadsheet/worksheet.rb', line 34

def initialize_cell(r,c,source_node)
  @cells[[r,c]]=Cell.new(r,c,source_node)
end

#nonemptycellsObject



40
41
42
# File 'lib/rspreadsheet/worksheet.rb', line 40

def nonemptycells
  @cells.values
end

#rows(rowi) ⇒ Object



43
44
45
# File 'lib/rspreadsheet/worksheet.rb', line 43

def rows(rowi)
  @spredsheetrows[rowi] ||= Row.new(self,rowi)
end