Class: Rspreadsheet::Worksheet
- Inherits:
-
Object
- Object
- Rspreadsheet::Worksheet
show all
- Defined in:
- lib/rspreadsheet/worksheet.rb
Instance Attribute Summary collapse
XMLTiedArray_WithRepeatableItems connected methods
collapse
How to get to cells? (syntactic sugar)
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(xmlnode_or_sheet_name, workbook) ⇒ Worksheet
workbook is here ONLY because of inserting images - to find unique name - it would be much better if it should bot be there
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/rspreadsheet/worksheet.rb', line 15
def initialize(xmlnode_or_sheet_name,workbook) initialize_xml_tied_array
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.
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/rspreadsheet/worksheet.rb', line 118
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
#xmlnode ⇒ Object
Returns the value of attribute xmlnode.
12
13
14
|
# File 'lib/rspreadsheet/worksheet.rb', line 12
def xmlnode
@xmlnode
end
|
Instance Method Details
#[](*params) ⇒ Object
Returns value of the cell given either by row,column integer coordinates of by address.
86
87
88
|
# File 'lib/rspreadsheet/worksheet.rb', line 86
def [](*params)
cells(*params).andand.value
end
|
#[]=(*params) ⇒ Object
Sets 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=
This also allows syntax like
@sheet[1] = ['Jan', 'Feb', 'Mar']
94
95
96
97
98
99
100
|
# File 'lib/rspreadsheet/worksheet.rb', line 94
def []=(*params)
if (params.size == 2) and params[0].kind_of?(Integer)
rows(params[0]).cellvalues = params[1]
else
cells(*params[0..-2]).andand.value = params.last
end
end
|
#add_row_above(arowi) ⇒ Object
41
42
43
|
# File 'lib/rspreadsheet/worksheet.rb', line 41
def add_row_above(arowi)
insert_new_empty_subitem_before(arowi)
end
|
#cells(*params) ⇒ Object
Also known as:
cell
Returns a Cell object placed in row and column or on a Cell on string address
103
104
105
106
107
108
109
110
111
|
# File 'lib/rspreadsheet/worksheet.rb', line 103
def cells(*params)
case params.length
when 0 then raise 'Not implemented yet' when 1..2
r,c = Rspreadsheet::Tools.a2c(*params)
row(r).andand.cell(c)
else raise ArgumentError.new('Wrong number of arguments.')
end
end
|
#column(param) ⇒ Object
113
114
115
116
|
# File 'lib/rspreadsheet/worksheet.rb', line 113
def column(param)
r,coli = Rspreadsheet::Tools.a2c(1,param)
Column.new(self,coli)
end
|
#detach_row_in_xml(rowi) ⇒ Object
50
51
52
|
# File 'lib/rspreadsheet/worksheet.rb', line 50
def detach_row_in_xml(rowi)
return detach_my_subnode_respect_repeated(rowi)
end
|
#first_unused_row_index ⇒ Object
37
38
39
|
# File 'lib/rspreadsheet/worksheet.rb', line 37
def first_unused_row_index
first_unused_subitem_index
end
|
#images(*params) ⇒ Object
65
66
67
|
# File 'lib/rspreadsheet/worksheet.rb', line 65
def images(*params)
worksheet_images.subitems(*params)
end
|
#images_count ⇒ Object
62
63
64
|
# File 'lib/rspreadsheet/worksheet.rb', line 62
def images_count
worksheet_images.size
end
|
#insert_cell_before(arowi, acoli) ⇒ Object
TODO: maybe move this to row level
45
46
47
48
|
# File 'lib/rspreadsheet/worksheet.rb', line 45
def insert_cell_before(arowi,acoli) detach_row_in_xml(arowi)
rows(arowi).insert_new_item(acoli)
end
|
#insert_image(filename, mime = 'image/png') ⇒ Object
68
69
70
|
# File 'lib/rspreadsheet/worksheet.rb', line 68
def insert_image(filename,mime='image/png')
worksheet_images.insert_image(filename,mime)
end
|
#insert_image_to(x, y, filename, mime = 'image/png') ⇒ Object
71
72
73
74
75
|
# File 'lib/rspreadsheet/worksheet.rb', line 71
def insert_image_to(x,y,filename,mime='image/png')
img = insert_image(filename,mime)
img.move_to(x,y)
img
end
|
#name=(value) ⇒ Object
31
|
# File 'lib/rspreadsheet/worksheet.rb', line 31
def name=(value); Tools.set_ns_attribute(@xmlnode,'table','name', value) end
|
#nonemptycells ⇒ Object
54
55
56
|
# File 'lib/rspreadsheet/worksheet.rb', line 54
def nonemptycells
used_rows_range.collect{ |rowi| rows(rowi).nonemptycells }.flatten
end
|
#prepare_subitem(rowi) ⇒ Object
80
|
# File 'lib/rspreadsheet/worksheet.rb', line 80
def prepare_subitem(rowi); Row.new(self,rowi) end
|
#rowcache ⇒ Object
81
|
# File 'lib/rspreadsheet/worksheet.rb', line 81
def rowcache; @itemcache end
|
#rows(*params) ⇒ Object
Also known as:
row
78
|
# File 'lib/rspreadsheet/worksheet.rb', line 78
def rows(*params); subitems(*params) end
|
#rowxmlnode(rowi) ⇒ Object
33
34
35
|
# File 'lib/rspreadsheet/worksheet.rb', line 33
def rowxmlnode(rowi)
my_subnode(rowi)
end
|
#subitem_xml_options ⇒ Object
13
|
# File 'lib/rspreadsheet/worksheet.rb', line 13
def subitem_xml_options; {:xml_items_node_name => 'table-row', :xml_repeated_attribute => 'number-rows-repeated'} end
|
#used_rows_range ⇒ Object
132
133
134
|
# File 'lib/rspreadsheet/worksheet.rb', line 132
def used_rows_range
1..self.rowcount
end
|
#worksheet_images ⇒ Object
59
60
61
|
# File 'lib/rspreadsheet/worksheet.rb', line 59
def worksheet_images
@worksheet_images ||= WorksheetImages.new(self)
end
|