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.



387
388
389
# File 'lib/lxl.rb', line 387

def book
  @book
end

#sheetObject

Worksheet name.



390
391
392
# File 'lib/lxl.rb', line 390

def sheet
  @sheet
end

Class Method Details

.new(first, last) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/lxl.rb', line 392

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



439
440
441
442
443
444
445
446
447
448
449
# File 'lib/lxl.rb', line 439

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



407
408
409
# File 'lib/lxl.rb', line 407

def excel!
  @excel = true
end

#excel?Boolean

Returns:

  • (Boolean)


411
412
413
# File 'lib/lxl.rb', line 411

def excel?
  @excel ? true : false
end

#first_cellObject



431
432
433
# File 'lib/lxl.rb', line 431

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

#first_colnumObject



423
424
425
# File 'lib/lxl.rb', line 423

def first_colnum
  LXL.xlcolnum(first_column)
end

#first_columnObject



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

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

#last_cellObject



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

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

#last_colnumObject



427
428
429
# File 'lib/lxl.rb', line 427

def last_colnum
  LXL.xlcolnum(last_column)
end

#last_columnObject



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

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