Class: Axlsx::Cell
- Inherits:
-
Object
- Object
- Axlsx::Cell
- Defined in:
- lib/axlsx/workbook/worksheet/cell.rb
Overview
The recommended way to generate cells is via Worksheet#add_row
A cell in a worksheet. Cell stores inforamation requried to serialize a single worksheet cell to xml. You must provde the Row that the cell belongs to and the cells value. The data type will automatically be determed if you do not specify the :type option. The default style will be applied if you do not supply the :style option. Changing the cell’s type will recast the value to the type specified. Altering the cell’s value via the property accessor will also automatically cast the provided value to the cell’s type.
Instance Attribute Summary collapse
-
#row ⇒ Row
readonly
The row this cell belongs to.
-
#style ⇒ Integer
The index of the cellXfs item to be applied to this cell.
-
#type ⇒ Symbol
The cell’s data type.
-
#value ⇒ Object
The value of this cell.
Instance Method Summary collapse
-
#index ⇒ Integer
The index of the cell in the containing row.
-
#initialize(row, value = "", options = {}) ⇒ Cell
constructor
A new instance of Cell.
-
#r ⇒ String
The alpha(column)numeric(row) reference for this sell.
-
#r_abs ⇒ String
The absolute alpha(column)numeric(row) reference for this sell.
-
#to_xml(xml) ⇒ String
Serializes the cell.
Constructor Details
#initialize(row, value = "", options = {}) ⇒ Cell
Returns a new instance of Cell.
56 57 58 59 60 61 62 63 64 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 56 def initialize(row, value="", ={}) self.row=row #reference for validation @styles = row.worksheet.workbook.styles @type= [:type] || cell_type_from_value(value) self.style = [:style] || 0 @value = cast_value(value) @row.cells << self end |
Instance Attribute Details
#row ⇒ Row
The row this cell belongs to.
32 33 34 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 32 def row @row end |
#style ⇒ Integer
The index of the cellXfs item to be applied to this cell.
28 29 30 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 28 def style @style end |
#type ⇒ Symbol
If the value provided cannot be cast into the type specified, type is changed to :string and the following logic is applied.
:string to :integer or :float, type coversions always return 0 or 0.0
:string, :integer, or :float to :time conversions always return the original value as a string and set the cells type to :string.
No support is currently implemented for parsing time strings.
The cell’s data type. Currently only four types are supported, :time, :float, :integer and :string. Changing the type for a cell will recast the value into that type. If no type option is specified in the constructor, the type is automatically determed.
46 47 48 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 46 def type @type end |
#value ⇒ Object
The value of this cell.
50 51 52 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 50 def value @value end |
Instance Method Details
#index ⇒ Integer
Returns The index of the cell in the containing row.
67 68 69 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 67 def index @row.cells.index(self) end |
#r ⇒ String
Returns The alpha(column)numeric(row) reference for this sell.
74 75 76 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 74 def r "#{col_ref}#{@row.index+1}" end |
#r_abs ⇒ String
Returns The absolute alpha(column)numeric(row) reference for this sell.
81 82 83 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 81 def r_abs "$#{r.split('').join('$')}" end |
#to_xml(xml) ⇒ String
Shared Strings are not used in this library. All values are set directly in the each sheet.
Serializes the cell
110 111 112 113 114 115 116 117 |
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 110 def to_xml(xml) if @type == :string #NOTE not sure why, but xml.t @v renders the text as html entities of unicode data xml.c(:r => r, :t=>:inlineStr, :s=>style) { xml.is { xml << "<t>#{value}</t>" } } else xml.c(:r => r, :s => style) { xml.v value } end end |