module Workbook::Modules::Cell

Constants

CHARACTER_REPACEMENTS
CLASS_CELLTYPE_MAPPING

Public Instance Methods

<=>(other) click to toggle source

Compare

@param [Workbook::Cell] other cell to compare against (based on value), can compare different value-types using compare_on_class @return [Fixnum] -1, 0, 1

# File lib/workbook/modules/cell.rb, line 213
def <=> other
  rv = nil
  begin
    rv = self.value <=> other.value
  rescue NoMethodError
    rv = compare_on_class other
  end
  if rv == nil
    rv = compare_on_class other
  end
  return rv
end
==(other) click to toggle source

Tests for equality based on its value (formatting is irrelevant)

@param [Workbook::Cell] other cell to compare against @return [Boolean]

# File lib/workbook/modules/cell.rb, line 156
def ==(other)
  if other.is_a? Cell
    other.value == self.value
  else
    other == self.value
  end
end
cell_type() click to toggle source

Returns column type, either :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :date, :binary, :boolean

@return [Symbol] the type of cell, compatible with Workbook::Column'types

# File lib/workbook/modules/cell.rb, line 101
def cell_type
  CLASS_CELLTYPE_MAPPING[value.class.to_s]
end
colspan() click to toggle source
# File lib/workbook/modules/cell.rb, line 291
def colspan
  @colspan.to_i if defined?(@colspan) and @colspan.to_i > 1
end
colspan=(c) click to toggle source
# File lib/workbook/modules/cell.rb, line 284
def colspan= c
  @colspan = c
end
compare_on_class(other) click to toggle source

Compare on class level

@param [Workbook::Cell] other cell to compare against

# File lib/workbook/modules/cell.rb, line 229
def compare_on_class other
  other_value = nil
  other_value = other.value if other
  self_value = importance_of_class self.value
  other_value = importance_of_class other_value
  self_value <=> other_value
end
format() click to toggle source

Returns current format

@return [Workbook::Format] the current format

# File lib/workbook/modules/cell.rb, line 142
def format
  # return @workbook_format if @workbook_format
  if row and template and row.header? and !defined?(@workbook_format)
    @workbook_format = template.create_or_find_format_by(:header)
  else
    @workbook_format ||= Workbook::Format.new
  end
  @workbook_format
end
format=(f) click to toggle source

Change the current format

@param [Workbook::Format, Hash] f set the formatting properties of this Cell, see Workbook::Format#initialize

# File lib/workbook/modules/cell.rb, line 129
def format= f
  if f.is_a? Workbook::Format
    @workbook_format = f
  elsif f.is_a? Hash
    @workbook_format = Workbook::Format.new(f)
  elsif f.class == NilClass
    @workbook_format = Workbook::Format.new
  end
end
format?() click to toggle source

Returns whether special formatting is present on this cell

@return [Boolean] index of the cell

# File lib/workbook/modules/cell.rb, line 247
def format?
  format and format.keys.count > 0
end
formula() click to toggle source
# File lib/workbook/modules/cell.rb, line 70
def formula
  @formula
end
formula=(f) click to toggle source
# File lib/workbook/modules/cell.rb, line 74
def formula= f
  @formula = f
end
importance_of_class(value) click to toggle source

Returns the importance of a value's class

@param value a potential value for a cell

# File lib/workbook/modules/cell.rb, line 240
def importance_of_class value
  CLASS_CELLTYPE_MAPPING.keys.index value.class.to_s
end
index() click to toggle source

Returns the index of the cell within the row, returns nil if no row is present

@return [Integer, NilClass] index of the cell

# File lib/workbook/modules/cell.rb, line 254
def index
  row.index self if row
end
inspect() click to toggle source
# File lib/workbook/modules/cell.rb, line 265
def inspect
  txt = "<Workbook::Cell @value=#{value}"
  txt += " @format=#{format}" if format?
  txt += ">"
  txt
end
key() click to toggle source

Returns the key (a Symbol) of the cell, based on its table's header

@return [Symbol, NilClass] key of the cell, returns nil if the cell doesn't belong to a table

# File lib/workbook/modules/cell.rb, line 261
def key
  table.header[index].to_sym if table
end
nil?() click to toggle source

returns true when the value of the cell is nil. @return [Boolean]

# File lib/workbook/modules/cell.rb, line 166
def nil?
  value.nil?
end
nil_or_empty?() click to toggle source
# File lib/workbook/modules/cell.rb, line 170
def nil_or_empty?
  value.nil? || value.to_s == ""
end
row() click to toggle source
# File lib/workbook/modules/cell.rb, line 78
def row
  @row
end
row=(r) click to toggle source
# File lib/workbook/modules/cell.rb, line 82
def row= r
  @row= r
end
rowspan() click to toggle source
# File lib/workbook/modules/cell.rb, line 294
def rowspan
  @rowspan.to_i if defined?(@rowspan) and @rowspan.to_i > 1
end
rowspan=(r) click to toggle source
# File lib/workbook/modules/cell.rb, line 287
def rowspan= r
  @rowspan = r
end
table() click to toggle source

Returns the sheet its at.

@return [Workbook::Table]

# File lib/workbook/modules/cell.rb, line 115
def table
  row.table if row
end
template() click to toggle source

Quick assessor to the book's template, if it exists

@return [Workbook::Template]

# File lib/workbook/modules/cell.rb, line 122
def template
  row.template if row
end
to_s() click to toggle source

convert value to string, and in case of a Date or Time value, apply formatting @return [String]

Calls superclass method
# File lib/workbook/modules/cell.rb, line 274
def to_s
  if (self.is_a? Date or self.is_a? Time) and format[:number_format]
    value.strftime(format[:number_format])
  elsif (self.class == Workbook::Cell)
    value.to_s
  else
    super
  end
end
to_sym() click to toggle source

returns a symbol representation of the cell's value @return [Symbol] a symbol representation @example

<Workbook::Cell value="yet another value">.to_sym # returns :yet_another_value
# File lib/workbook/modules/cell.rb, line 183
def to_sym
  return @to_sym if @to_sym
  v = nil
  unless nil_or_empty?
    if cell_type == :integer
      v = "num#{value}".to_sym
    elsif cell_type == :float
      v = "num#{value}".sub(".","_").to_sym
    else
      v = value_to_s.strip
      ends_with_exclamationmark = (v[-1] == '!')
      ends_with_questionmark = (v[-1] == '?')

      v = _replace_possibly_problematic_characters_from_string(v)

      v = v.encode(Encoding.find('ASCII'), {:invalid => :replace, :undef => :replace, :replace => ''})

      v = "#{v}!" if ends_with_exclamationmark
      v = "#{v}?" if ends_with_questionmark
      v = v.downcase.to_sym
    end
  end
  @to_sym = v
  return @to_sym
end
valid_value?(value) click to toggle source

Evaluates a value for class-validity

@param [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass,Object] value the value to evaluate @return [Boolean] returns true when the value is a valid cell value

# File lib/workbook/modules/cell.rb, line 66
def valid_value? value
  !CLASS_CELLTYPE_MAPPING[value.class.to_s].nil?
end
value() click to toggle source

Returns the current value

@return [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass] a valid value

# File lib/workbook/modules/cell.rb, line 108
def value
  @value
end
value=(value) click to toggle source

Change the current value

@param [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass,Symbol] value a valid value

# File lib/workbook/modules/cell.rb, line 89
def value= value
  if valid_value? value
    @value = value
    @to_sym = nil
  else
    raise ArgumentError, "value should be of a primitive type, e.g. a string, or an integer, not a #{value.class} (is_a? [TrueClass,FalseClass,Date,Time,Numeric,String, NilClass, Symbol])"
  end
end
value_to_s() click to toggle source
# File lib/workbook/modules/cell.rb, line 174
def value_to_s
  value.to_s.downcase
end

Private Instance Methods

_replace_possibly_problematic_characters_from_string(string) click to toggle source
# File lib/workbook/modules/cell.rb, line 300
def _replace_possibly_problematic_characters_from_string(string)
  Workbook::Modules::Cell::CHARACTER_REPACEMENTS.each do |ac,rep|
    ac.each do |s|
      string = string.gsub(s, rep)
    end
  end
  string
end