Class: Rasta::Spreadsheet::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/rasta/spreadsheet.rb

Overview

Cells store information on a specific worksheet cell. The name is the Excel name for the cell (ie: A1, B2, etc), the value is the value of the cell and the header is the header for that row/column (usually the attribute/function parameter we’re trying to set)

Constant Summary collapse

ARRAY =
/\A\s*\[.+\]\s*\Z/ms
HASH =
/\A\s*\{.+\}\s*\Z/ms
BOOL =
/\A\s*(true|false)\s*\Z/i
NUMBER =
/\A\s*-?\d+\.??\d*?\s*\Z/
REGEXP =
/\A\s*(\/.+\/)\s*\Z/ms

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheet, record_index, cell_index) ⇒ Cell

Returns a new instance of Cell.



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/rasta/spreadsheet.rb', line 453

def initialize(sheet, record_index, cell_index)
  @sheet       = sheet
  @book        = sheet.book
  @o           = sheet.ole_object
  @cellindex   = cell_index
  @recordindex = record_index
  case @sheet.style
  when :row
    @row = @recordindex 
    @col = @cellindex + @sheet.firstcol - 1 # taking into account the start of the used data range
    @cell = @o.Cells(@row,@col)
    @cell.NumberFormat == "@" ? @value = @cell.Value : @value = @cell.Text
  when :col
    @row = @cellindex + @sheet.firstrow - 1 # taking into account the start of the used data range
    @col = @recordindex 
    @cell = @o.Cells(@row,@col)
    @cell.NumberFormat == "@" ? @value = @cell.Value : @value = @cell.Text
  end
  # Ignore blank values. There's not much use for cells 
  # that are not set so skip them and normalize the return 
  # to nil so we know that's the case
  @value = @value.to_s.strip
  if @value =~ NUMBER
    @value = eval(@value) unless @value =~ /^0\d/ 
  elsif @value =~ ARRAY || @value =~ HASH || @value =~ BOOL || @value =~ REGEXP 
    @value = @value.downcase if @value =~ BOOL  # make sure it's not confused with a constant
    @value = eval(@value)
  end        
  
  # Put together the cell's name
  column_letter = @sheet.colname(@col)
  @name = column_letter + @row.to_s
  
  # The recordid is the row/col for the record
  case @sheet.style
  when :row
    @recordid = @row.to_s
  when :col
    @recordid = column_letter
  end
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



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

def book
  @book
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#recordidObject (readonly)

Returns the value of attribute recordid.



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

def recordid
  @recordid
end

#recordindexObject (readonly)

Returns the value of attribute recordindex.



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

def recordindex
  @recordindex
end

#sheetObject (readonly)

Returns the value of attribute sheet.



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

def sheet
  @sheet
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#colorObject



502
503
504
# File 'lib/rasta/spreadsheet.rb', line 502

def color
  @cell.Interior.ColorIndex
end

#color=(c) ⇒ Object



499
500
501
# File 'lib/rasta/spreadsheet.rb', line 499

def color=(c)
  @cell.Interior.ColorIndex = c
end

#comment=(c) ⇒ Object



508
509
510
511
512
513
514
# File 'lib/rasta/spreadsheet.rb', line 508

def comment=(c)
  if c && !@cell.Comment
    @cell.AddComment(c) 
    @cell.Comment.Shape.TextFrame.AutoSize = true
    @cell.Comment.Shape.TextFrame.Characters.Font.Size=8
  end
end

#headerObject



494
495
496
497
498
# File 'lib/rasta/spreadsheet.rb', line 494

def header
  # Return the header but strip off a trailing () if
  # the user added for clarification purposes
  @sheet.headers[@cellindex-1].gsub(/\(\)$/,'') if @sheet.headers[@cellindex-1]
end

#italicObject



505
506
507
# File 'lib/rasta/spreadsheet.rb', line 505

def italic
  @cell.Font.Italic
end

#ole_objectObject



522
523
524
# File 'lib/rasta/spreadsheet.rb', line 522

def ole_object 
  @cell
end

#selectrecordObject



515
516
517
518
# File 'lib/rasta/spreadsheet.rb', line 515

def selectrecord
  range = @sheet.cellrange(@recordindex)
  range.Select
end