Class: POI::Cell

Inherits:
Facade
  • Object
show all
Defined in:
lib/poi/workbook/cell.rb

Constant Summary collapse

DATE_UTIL =
Java::org.apache.poi.ss.usermodel.DateUtil
CELL =
Java::org.apache.poi.ss.usermodel.Cell
CELL_VALUE =
Java::org.apache.poi.ss.usermodel.CellValue
CELL_TYPE_BLANK =
CELL::CELL_TYPE_BLANK
CELL_TYPE_BOOLEAN =
CELL::CELL_TYPE_BOOLEAN
CELL_TYPE_ERROR =
CELL::CELL_TYPE_ERROR
CELL_TYPE_FORMULA =
CELL::CELL_TYPE_FORMULA
CELL_TYPE_NUMERIC =
CELL::CELL_TYPE_NUMERIC
CELL_TYPE_STRING =
CELL::CELL_TYPE_STRING

Instance Method Summary collapse

Constructor Details

#initialize(cell, row) ⇒ Cell

Returns a new instance of Cell.



38
39
40
41
# File 'lib/poi/workbook/cell.rb', line 38

def initialize(cell, row)
  @cell = cell
  @row  = row
end

Instance Method Details

#<=>(other) ⇒ Object



43
44
45
46
# File 'lib/poi/workbook/cell.rb', line 43

def <=> other
  return 1 if other.nil?
  return self.index <=> other.index
end

#commentObject



94
95
96
# File 'lib/poi/workbook/cell.rb', line 94

def comment
  poi_cell.cell_comment
end

#error_valueObject

This is NOT an inexpensive operation. The purpose of this method is merely to get more information out of cell when one thinks the value returned is incorrect. It may have more value in development than in production.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/poi/workbook/cell.rb', line 51

def error_value
  if poi_cell.cell_type == CELL_TYPE_ERROR
    error_value_from(poi_cell.error_cell_value)
  elsif poi_cell.cell_type == CELL_TYPE_FORMULA && 
        poi_cell.cached_formula_result_type == CELL_TYPE_ERROR
        
    cell_value = formula_evaluator.evaluate(poi_cell)
    cell_value && error_value_from(cell_value.error_value)
  else
    nil
  end
end

#formulaObject



80
81
82
# File 'lib/poi/workbook/cell.rb', line 80

def formula
  poi_cell.cell_formula
end

#formula=(new_value) ⇒ Object



74
75
76
77
78
# File 'lib/poi/workbook/cell.rb', line 74

def formula= new_value
  poi_cell.cell_formula = new_value
  @row.worksheet.workbook.on_formula_update self
  self
end

#formula_valueObject

returns the formula for this Cell if it has one, otherwise nil



65
66
67
# File 'lib/poi/workbook/cell.rb', line 65

def formula_value
  poi_cell.cell_type == CELL_TYPE_FORMULA ? poi_cell.cell_formula : nil
end

#indexObject



98
99
100
# File 'lib/poi/workbook/cell.rb', line 98

def index
  poi_cell.column_index 
end

#poi_cellObject

returns the underlying org.apache.poi.ss.usermodel.Cell



117
118
119
# File 'lib/poi/workbook/cell.rb', line 117

def poi_cell
  @cell
end

#style!(options) ⇒ Object



124
125
126
# File 'lib/poi/workbook/cell.rb', line 124

def style! options
  self.style = @row.worksheet.workbook.create_style(options)
end

#to_s(evaluate_formulas = true) ⇒ Object

Get the String representation of this Cell’s value.

If this Cell is a formula you can pass a false to this method and get the formula instead of the String representation.



106
107
108
109
110
111
112
113
114
# File 'lib/poi/workbook/cell.rb', line 106

def to_s(evaluate_formulas=true)
  return '' if poi_cell.nil?

  if poi_cell.cell_type == CELL_TYPE_FORMULA && evaluate_formulas == false
    formula_value
  else
    value.to_s
  end
end

#valueObject



69
70
71
72
# File 'lib/poi/workbook/cell.rb', line 69

def value
  return nil if poi_cell.nil?
  cast_value
end

#value=(new_value) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/poi/workbook/cell.rb', line 84

def value= new_value
  set_cell_value new_value
  if new_value.nil?
    @row.worksheet.workbook.on_delete self
  else
    @row.worksheet.workbook.on_update self
  end
  self
end