Class: LXL::Range

Inherits:
Range
  • Object
show all
Defined in:
lib/lxl.rb

Overview

Excel-style ranges.

Recognized formats

B3 | B3: | B3:D5 | Sheet1!B3:D5 | [Book1]Sheet1!B3:D5 | [file.xls]Sheet1!B3:D5

(the first two become B3:B3)

Collection

# Ruby Range
Range.new("B3", "D5").collect
# => ["B3", "B4", "B5", "B6", "B7", "B8", "B9",
      "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9",
      "D0", "D1", "D2", "D3", "D4", "D5"]

# LXL Range
LXL::Range.new("B3", "D5").collect
 # => ["B3", "B4", "B5", "C3", "C4", "C5", "D3", "D4", "D5"]

Constant Summary collapse

EXCEL_RANGE =
/\A(\[([\w\.]+)\])?((\w+)!)?([A-Z]+[1-9]+)(:([A-Z]+[1-9]+)?)?/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bookObject

Workbook name.



414
415
416
# File 'lib/lxl.rb', line 414

def book
  @book
end

#sheetObject

Worksheet name.



417
418
419
# File 'lib/lxl.rb', line 417

def sheet
  @sheet
end

Class Method Details

.new(first, last) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/lxl.rb', line 419

def self.new(first, last)
  excel = (first =~ EXCEL_RANGE && last =~ EXCEL_RANGE)
  if excel
    first =~ EXCEL_RANGE
    book,sheet,first = $2,$4,$5
    obj = super(*[first.upcase,last.upcase].sort)
    obj.excel!
    obj.book = book
    obj.sheet = sheet
    obj
  else
    super
  end
end

Instance Method Details

#eachObject



466
467
468
469
470
471
472
473
474
475
476
# File 'lib/lxl.rb', line 466

def each
  if excel?
    (first_column..last_column).each do |column|
      (first_cell..last_cell).each do |cell|
        yield column+cell.to_s
      end
    end
  else
    super
  end
end

#excel!Object



434
435
436
# File 'lib/lxl.rb', line 434

def excel!
  @excel = true
end

#excel?Boolean

Returns:

  • (Boolean)


438
439
440
# File 'lib/lxl.rb', line 438

def excel?
  @excel ? true : false
end

#first_cellObject



458
459
460
# File 'lib/lxl.rb', line 458

def first_cell
  first.to_s.gsub(/[^1-9]/,'').to_i
end

#first_colnumObject



450
451
452
# File 'lib/lxl.rb', line 450

def first_colnum
  LXL.xlcolnum(first_column)
end

#first_columnObject



442
443
444
# File 'lib/lxl.rb', line 442

def first_column
  first.to_s.upcase.gsub(/[^A-Z]/,'')
end

#last_cellObject



462
463
464
# File 'lib/lxl.rb', line 462

def last_cell
  last.to_s.gsub(/[^1-9]/,'').to_i
end

#last_colnumObject



454
455
456
# File 'lib/lxl.rb', line 454

def last_colnum
  LXL.xlcolnum(last_column)
end

#last_columnObject



446
447
448
# File 'lib/lxl.rb', line 446

def last_column
  last.to_s.upcase.gsub(/[^A-Z]/,'')
end