Class: Rspreadsheet::Worksheet

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

Instance Attribute Summary collapse

XMLTiedArray connected methods collapse

How to get to cells? (syntactic sugar) collapse

Instance Method Summary collapse

Constructor Details

#initialize(xmlnode_or_sheet_name) ⇒ Worksheet

Returns a new instance of Worksheet.



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

def initialize(xmlnode_or_sheet_name)
  @itemcache = Hash.new  #TODO: move to module XMLTiedArray
  # set up the @xmlnode according to parameter
  case xmlnode_or_sheet_name
    when LibXML::XML::Node
      @xmlnode = xmlnode_or_sheet_name
    when String
      @xmlnode = Tools.prepare_ns_node('table','table')
      self.name = xmlnode_or_sheet_name
    else raise 'Provide name or xml node to create a Worksheet object'
  end
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. TO catch errors easier, allows only up to three uppercase letters in colum part, althought it won't be necessarry to restrict.



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rspreadsheet/worksheet.rb', line 82

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_to_coordinates($~[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

#xmlnodeObject

Returns the value of attribute xmlnode.



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

def xmlnode
  @xmlnode
end

Instance Method Details

#[](*params) ⇒ Object

Returns value of the cell given either by row,column integer coordinates of by address.

Parameters:

  • either ((Integer,Integer), String)

    row and column of the cell (i.e. 3,5) or a string containing it address i.e. 'F12'



63
64
65
# File 'lib/rspreadsheet/worksheet.rb', line 63

def [](*params)
  cells(*params).andand.value
end

#[]=(*params) ⇒ Object

Aets value of the cell given either by row,column integer coordinates of by address. It also sets the type of the cell according to type of the value. For details #see Cell.value=



67
68
69
# File 'lib/rspreadsheet/worksheet.rb', line 67

def []=(*params)
  cells(*params[0..-2]).andand.value = params.last 
end

#add_row_above(arowi) ⇒ Object



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

def add_row_above(arowi)
  add_empty_subitem_before(arowi)
end

#cells(*params) ⇒ Object

Returns a Cell object placed in row and column or on a Cell on string address

Parameters:

  • either ((Integer,Integer), String)

    row and column of the cell (i.e. 3,5) or a string containing it address i.e. 'F12'



72
73
74
75
76
77
78
79
80
# File 'lib/rspreadsheet/worksheet.rb', line 72

def cells(*params)
  case params.length 
    when 0 then raise 'Not implemented yet' #TODO: return list of all cells
    when 1..2
      r,c = Rspreadsheet::Tools.a2c(*params)
      rows(r).andand.cells(c)
    else raise Exception.new('Wrong number of arguments.')
  end
end

#detach_row_in_xml(rowi) ⇒ Object



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

def detach_row_in_xml(rowi)
  return detach_my_subnode_respect_repeated(rowi, {:xml_items_node_name => 'table-row', :xml_repeated_attribute => 'number-rows-repeated'})
end

#first_unused_row_indexObject



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

def first_unused_row_index
  find_first_unused_index_respect_repeated({:xml_items_node_name => 'table-row', :xml_repeated_attribute => 'number-rows-repeated'})
end

#insert_cell_before(arowi, acoli) ⇒ Object



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

def insert_cell_before(arowi,acoli)
  detach_row_in_xml(arowi)
  rows(arowi).add_empty_subitem_before(acoli)
end

#nameObject

name of the worksheet



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

def name; Tools.get_ns_attribute_value(@xmlnode,'table','name') end

#name=(value) ⇒ Object



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

def name=(value); Tools.set_ns_attribute(@xmlnode,'table','name', value) end

#nonemptycellsObject



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

def nonemptycells
  used_rows_range.collect{ |rowi| rows(rowi).nonemptycells }.flatten
end

#prepare_subitem(rowi) ⇒ Object



57
# File 'lib/rspreadsheet/worksheet.rb', line 57

def prepare_subitem(rowi); Row.new(self,rowi) end

#rowcacheObject



58
# File 'lib/rspreadsheet/worksheet.rb', line 58

def rowcache; @itemcache end

#rows(*params) ⇒ Object



56
# File 'lib/rspreadsheet/worksheet.rb', line 56

def rows(*params); subitems(*params) end

#rowxmlnode(rowi) ⇒ Object



30
31
32
# File 'lib/rspreadsheet/worksheet.rb', line 30

def rowxmlnode(rowi)
  find_my_subnode_respect_repeated(rowi, {:xml_items_node_name => 'table-row', :xml_repeated_attribute => 'number-rows-repeated'})
end

#subitem_xml_optionsObject



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

def subitem_xml_options; {:xml_items_node_name => 'table-row', :xml_repeated_attribute => 'number-rows-repeated'} end

#used_rows_rangeObject



95
96
97
# File 'lib/rspreadsheet/worksheet.rb', line 95

def used_rows_range
  1..self.first_unused_row_index-1
end