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.



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

def book
  @book
end

#sheetObject

Worksheet name.



419
420
421
# File 'lib/lxl.rb', line 419

def sheet
  @sheet
end

Class Method Details

.new(first, last) ⇒ Object



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

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



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

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



436
437
438
# File 'lib/lxl.rb', line 436

def excel!
  @excel = true
end

#excel?Boolean

Returns:

  • (Boolean)


440
441
442
# File 'lib/lxl.rb', line 440

def excel?
  @excel ? true : false
end

#first_cellObject



460
461
462
# File 'lib/lxl.rb', line 460

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

#first_colnumObject



452
453
454
# File 'lib/lxl.rb', line 452

def first_colnum
  LXL.xlcolnum(first_column)
end

#first_columnObject



444
445
446
# File 'lib/lxl.rb', line 444

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

#last_cellObject



464
465
466
# File 'lib/lxl.rb', line 464

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

#last_colnumObject



456
457
458
# File 'lib/lxl.rb', line 456

def last_colnum
  LXL.xlcolnum(last_column)
end

#last_columnObject



448
449
450
# File 'lib/lxl.rb', line 448

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

#to_sObject



480
481
482
# File 'lib/lxl.rb', line 480

def to_s
  "#{first}:#{last}"
end