Class: GenericSpreadsheet
- Inherits:
-
Object
- Object
- GenericSpreadsheet
- Defined in:
- lib/roo/generic_spreadsheet.rb
Overview
Base class for all other types of spreadsheets
Direct Known Subclasses
Constant Summary collapse
- @@class_counter =
0
Instance Attribute Summary collapse
-
#default_sheet ⇒ Object
Returns the value of attribute default_sheet.
-
#header_line ⇒ Object
sets the line with attribute names (default: 1).
Instance Method Summary collapse
-
#column(columnnumber, sheet = nil) ⇒ Object
returns all values in this column as an array column numbers are 1,2,3,…
-
#empty?(row, col, sheet = nil) ⇒ Boolean
true if cell is empty.
-
#find(*args) ⇒ Object
find a row either by row number or a condition Caution: this works only within the default sheet -> set default_sheet before you call this method (experimental. see examples in the test_roo.rb file).
-
#first_column(sheet = nil) ⇒ Object
returns the number of the first non-empty column.
-
#first_column_as_letter(sheet = nil) ⇒ Object
first non-empty column as a letter.
-
#first_row(sheet = nil) ⇒ Object
returns the number of the first non-empty row.
-
#formulas(sheet = nil) ⇒ Object
returns each formula in the selected sheet as an array of elements [row, col, formula].
-
#info ⇒ Object
returns information of the spreadsheet document and all sheets within this document.
-
#last_column(sheet = nil) ⇒ Object
returns the number of the last non-empty column.
-
#last_column_as_letter(sheet = nil) ⇒ Object
last non-empty column as a letter.
-
#last_row(sheet = nil) ⇒ Object
returns the number of the last non-empty row.
-
#method_missing(m, *args) ⇒ Object
when a method like spreadsheet.a42 is called convert it to a call of spreadsheet.cell(‘a’,42).
-
#reload ⇒ Object
reopens and read a spreadsheet document.
-
#remove_tmp ⇒ Object
recursively removes the current temporary directory this is only needed if you work with zipped files or files via the web.
-
#row(rownumber, sheet = nil) ⇒ Object
returns all values in this row as an array row numbers are 1,2,3,…
-
#to_csv(filename = nil, sheet = nil) ⇒ Object
write the current spreadsheet to stdout or into a file.
-
#to_matrix(from_row = nil, from_column = nil, to_row = nil, to_column = nil, sheet = nil) ⇒ Object
returns a matrix object from the whole sheet or a rectangular area of a sheet.
-
#to_xml ⇒ Object
returns an XML representation of all sheets of a spreadsheet file.
-
#to_yaml(prefix = {}, from_row = nil, from_column = nil, to_row = nil, to_column = nil, sheet = nil) ⇒ Object
returns a rectangular area (default: all cells) as yaml-output you can add additional attributes with the prefix parameter like: oo.to_yaml(“sheet” => “1”).
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
when a method like spreadsheet.a42 is called convert it to a call of spreadsheet.cell(‘a’,42)
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/roo/generic_spreadsheet.rb', line 421 def method_missing(m, *args) # #aa42 => #cell('aa',42) # #aa42('Sheet1') => #cell('aa',42,'Sheet1') if m =~ /^([a-z]+)(\d)$/ col = GenericSpreadsheet.letter_to_number($1) row = $2.to_i if args.size > 0 return cell(row,col,args[0]) else return cell(row,col) end else super end end |
Instance Attribute Details
#default_sheet ⇒ Object
Returns the value of attribute default_sheet.
7 8 9 |
# File 'lib/roo/generic_spreadsheet.rb', line 7 def default_sheet @default_sheet end |
#header_line ⇒ Object
sets the line with attribute names (default: 1)
12 13 14 |
# File 'lib/roo/generic_spreadsheet.rb', line 12 def header_line @header_line end |
Instance Method Details
#column(columnnumber, sheet = nil) ⇒ Object
returns all values in this column as an array column numbers are 1,2,3,… like in the spreadsheet
319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/roo/generic_spreadsheet.rb', line 319 def column(columnnumber,sheet=nil) if columnnumber.class == String columnnumber = Excel.letter_to_number(columnnumber) end sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] result = [] first_row(sheet).upto(last_row(sheet)) do |row| result << cell(row,columnnumber,sheet) end result end |
#empty?(row, col, sheet = nil) ⇒ Boolean
true if cell is empty
344 345 346 347 348 349 350 351 352 |
# File 'lib/roo/generic_spreadsheet.rb', line 344 def empty?(row, col, sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] or self.class == Excel row,col = normalize(row,col) return true unless cell(row, col, sheet) return true if celltype(row, col, sheet) == :string && cell(row, col, sheet).empty? return true if row < first_row(sheet) || row > last_row(sheet) || col < first_column(sheet) || col > last_column(sheet) false end |
#find(*args) ⇒ Object
find a row either by row number or a condition Caution: this works only within the default sheet -> set default_sheet before you call this method (experimental. see examples in the test_roo.rb file)
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/roo/generic_spreadsheet.rb', line 217 def find(*args) # :nodoc result_array = false args.each {|arg,val| if arg.class == Hash arg.each { |hkey,hval| if hkey == :array and hval == true result_array = true end } end } column_with = {} 1.upto(last_column) do |col| column_with[cell(@header_line,col)] = col end result = Array.new #-- id if args[0].class == Fixnum rownum = args[0] if @header_line tmp = {} else tmp = [] end 1.upto(self.row(rownum).size) {|j| x = '' column_with.each { |key,val| if val == j x = key end } if @header_line tmp[x] = cell(rownum,j) else tmp[j-1] = cell(rownum,j) end } if @header_line result = [ tmp ] else result = tmp end #-- :all elsif args[0] == :all if args[1].class == Hash args[1].each {|key,val| if key == :conditions column_with = {} 1.upto(last_column) do |col| column_with[cell(@header_line,col)] = col end conditions = val first_row.upto(last_row) do |i| # are all conditions met? found = 1 conditions.each { |key,val| if cell(i,column_with[key]) == val found *= 1 else found *= 0 end } if found > 0 tmp = {} 1.upto(self.row(i).size) {|j| x = '' column_with.each { |key,val| if val == j x = key end } tmp[x] = cell(i,j) } if result_array result << self.row(i) else result << tmp end end end end # :conditions } end end result end |
#first_column(sheet = nil) ⇒ Object
returns the number of the first non-empty column
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/roo/generic_spreadsheet.rb', line 114 def first_column(sheet=nil) if sheet == nil sheet = @default_sheet end read_cells(sheet) unless @cells_read[sheet] if @first_column[sheet] return @first_column[sheet] end impossible_value = 999_999 # more than a spreadsheet can hold result = impossible_value @cell[sheet].each_pair {|key,value| y,x = key # _to_string(key).split(',') x = x # .to_i result = [result, x].min if value } if @cell[sheet] result = nil if result == impossible_value @first_column[sheet] = result result end |
#first_column_as_letter(sheet = nil) ⇒ Object
first non-empty column as a letter
64 65 66 |
# File 'lib/roo/generic_spreadsheet.rb', line 64 def first_column_as_letter(sheet=nil) GenericSpreadsheet.number_to_letter(first_column(sheet)) end |
#first_row(sheet = nil) ⇒ Object
returns the number of the first non-empty row
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/roo/generic_spreadsheet.rb', line 74 def first_row(sheet=nil) if sheet == nil sheet = @default_sheet end read_cells(sheet) unless @cells_read[sheet] if @first_row[sheet] return @first_row[sheet] end impossible_value = 999_999 # more than a spreadsheet can hold result = impossible_value @cell[sheet].each_pair {|key,value| y,x = key # _to_string(key).split(',') y = y.to_i result = [result, y].min if value } if @cell[sheet] result = nil if result == impossible_value @first_row[sheet] = result result end |
#formulas(sheet = nil) ⇒ Object
returns each formula in the selected sheet as an array of elements
- row, col, formula
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/roo/generic_spreadsheet.rb', line 439 def formulas(sheet=nil) theformulas = Array.new sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] return theformulas unless first_row(sheet) # if there is no first row then # there can't be formulas first_row(sheet).upto(last_row(sheet)) {|row| first_column(sheet).upto(last_column(sheet)) {|col| if formula?(row,col,sheet) f = [row, col, formula(row,col,sheet)] theformulas << f end } } theformulas end |
#info ⇒ Object
returns information of the spreadsheet document and all sheets within this document.
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/roo/generic_spreadsheet.rb', line 370 def info result = "File: #{File.basename(@filename)}\n"+ "Number of sheets: #{sheets.size}\n"+ "Sheets: #{sheets.join(', ')}\n" n = 1 sheets.each {|sheet| self.default_sheet = sheet result << "Sheet " + n.to_s + ":\n" unless first_row result << " - empty -" else result << " First row: #{first_row}\n" result << " Last row: #{last_row}\n" result << " First column: #{GenericSpreadsheet.number_to_letter(first_column)}\n" result << " Last column: #{GenericSpreadsheet.number_to_letter(last_column)}" end result << "\n" if sheet != sheets.last n += 1 } result end |
#last_column(sheet = nil) ⇒ Object
returns the number of the last non-empty column
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/roo/generic_spreadsheet.rb', line 135 def last_column(sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] if @last_column[sheet] return @last_column[sheet] end impossible_value = 0 result = impossible_value @cell[sheet].each_pair {|key,value| y,x = key # _to_string(key).split(',') x = x.to_i result = [result, x].max if value } if @cell[sheet] result = nil if result == impossible_value @last_column[sheet] = result result end |
#last_column_as_letter(sheet = nil) ⇒ Object
last non-empty column as a letter
69 70 71 |
# File 'lib/roo/generic_spreadsheet.rb', line 69 def last_column_as_letter(sheet=nil) GenericSpreadsheet.number_to_letter(last_column(sheet)) end |
#last_row(sheet = nil) ⇒ Object
returns the number of the last non-empty row
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/roo/generic_spreadsheet.rb', line 95 def last_row(sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] if @last_row[sheet] return @last_row[sheet] end impossible_value = 0 result = impossible_value @cell[sheet].each_pair {|key,value| y,x = key # _to_string(key).split(',') y = y.to_i result = [result, y].max if value } if @cell[sheet] result = nil if result == impossible_value @last_row[sheet] = result result end |
#reload ⇒ Object
reopens and read a spreadsheet document
333 334 335 336 337 338 339 340 341 |
# File 'lib/roo/generic_spreadsheet.rb', line 333 def reload # von Abfrage der Klasse direkt auf .to_s == '..' umgestellt ds = @default_sheet initialize(@filename) if self.class.to_s == 'Openoffice' or self.class.to_s == 'Excel' initialize(@spreadsheetkey,@user,@password) if self.class.to_s == 'Google' self.default_sheet = ds #@first_row = @last_row = @first_column = @last_column = nil end |
#remove_tmp ⇒ Object
recursively removes the current temporary directory this is only needed if you work with zipped files or files via the web
356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/roo/generic_spreadsheet.rb', line 356 def remove_tmp #to do # "remove_tmp wieder aktivieren" #end #return #$log.debug("remove_tmp('#{@tmpdir}')") if File.exists?(@tmpdir) #$log.debug("#{@tmpdir} exists") FileUtils::rm_r(@tmpdir) end end |
#row(rownumber, sheet = nil) ⇒ Object
returns all values in this row as an array row numbers are 1,2,3,… like in the spreadsheet
307 308 309 310 311 312 313 314 315 |
# File 'lib/roo/generic_spreadsheet.rb', line 307 def row(rownumber,sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] result = [] first_column(sheet).upto(last_column(sheet)) do |col| result << cell(rownumber,col,sheet) end result end |
#to_csv(filename = nil, sheet = nil) ⇒ Object
write the current spreadsheet to stdout or into a file
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/roo/generic_spreadsheet.rb', line 183 def to_csv(filename=nil,sheet=nil) sheet = @default_sheet unless sheet if filename file = File.open(filename,"w") # do |file| write_csv_content(file,sheet) file.close else write_csv_content(STDOUT,sheet) end true end |
#to_matrix(from_row = nil, from_column = nil, to_row = nil, to_column = nil, sheet = nil) ⇒ Object
returns a matrix object from the whole sheet or a rectangular area of a sheet
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/roo/generic_spreadsheet.rb', line 196 def to_matrix(from_row=nil, from_column=nil, to_row=nil, to_column=nil,sheet=nil) sheet = @default_sheet unless sheet arr = [] pos = 0 return Matrix.rows([]) unless first_row (from_row||first_row(sheet)).upto(to_row||last_row(sheet)) do |row| line = [] (from_column||first_column(sheet)).upto(to_column||last_column(sheet)) do |col| line << cell(row,col) end arr[pos] = line pos += 1 end Matrix.rows(arr) end |
#to_xml ⇒ Object
returns an XML representation of all sheets of a spreadsheet file
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/roo/generic_spreadsheet.rb', line 393 def to_xml builder = Nokogiri::XML::Builder.new do |xml| xml.spreadsheet { self.sheets.each do |sheet| self.default_sheet = sheet xml.sheet(:name => sheet) { |x| if first_row and last_row and first_column and last_column # sonst gibt es Fehler bei leeren Blaettern first_row.upto(last_row) do |row| first_column.upto(last_column) do |col| unless empty?(row,col) x.cell(cell(row,col), :row =>row, :column => col, :type => celltype(row,col)) end end end end } end } end return builder.to_xml end |
#to_yaml(prefix = {}, from_row = nil, from_column = nil, to_row = nil, to_column = nil, sheet = nil) ⇒ Object
returns a rectangular area (default: all cells) as yaml-output you can add additional attributes with the prefix parameter like: oo.to_yaml(“sheet” => “1”)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/roo/generic_spreadsheet.rb', line 156 def to_yaml(prefix={}, from_row=nil, from_column=nil, to_row=nil, to_column=nil,sheet=nil) sheet = @default_sheet unless sheet result = "--- \n" return '' unless first_row # empty result if there is no first_row in a sheet (from_row||first_row(sheet)).upto(to_row||last_row(sheet)) do |row| (from_column||first_column(sheet)).upto(to_column||last_column(sheet)) do |col| unless empty?(row,col,sheet) result << "cell_#{row}_#{col}: \n" prefix.each {|k,v| result << " #{k}: #{v} \n" } result << " row: #{row} \n" result << " col: #{col} \n" result << " celltype: #{self.celltype(row,col,sheet)} \n" if self.celltype(row,col,sheet) == :time result << " value: #{GenericSpreadsheet.integer_to_timestring( self.cell(row,col,sheet))} \n" else result << " value: #{self.cell(row,col,sheet)} \n" end end end end result end |