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 =

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

/\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.



359
360
361
# File 'lib/lxl.rb', line 359

def book
  @book
end

#sheetObject

Worksheet name.



362
363
364
# File 'lib/lxl.rb', line 362

def sheet
  @sheet
end

Class Method Details

.new(first, last) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/lxl.rb', line 364

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



411
412
413
414
415
416
417
418
419
420
421
# File 'lib/lxl.rb', line 411

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

#excel!Object



379
380
381
# File 'lib/lxl.rb', line 379

def excel!
  @excel = true
end

#excel?Boolean

Returns:

  • (Boolean)


383
384
385
# File 'lib/lxl.rb', line 383

def excel?
  @excel ? true : false
end

#first_cellObject



403
404
405
# File 'lib/lxl.rb', line 403

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

#first_colnumObject



395
396
397
# File 'lib/lxl.rb', line 395

def first_colnum
  LXL.xlcolnum(first_column)
end

#first_columnObject



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

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

#last_cellObject



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

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

#last_colnumObject



399
400
401
# File 'lib/lxl.rb', line 399

def last_colnum
  LXL.xlcolnum(last_column)
end

#last_columnObject



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

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