Class: Axlsx::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/axlsx/workbook/worksheet/cell.rb

Overview

Note:

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.

Examples:

Manually creating and manipulating Cell objects

ws = Workbook.new.add_worksheet 
# This is the simple, and recommended way to create cells. Data types will automatically be determined for you.
ws.add_row :values => [1,"fish",Time.now]

# but you can also do this
r = ws.add_row
r.add_cell 1

# or even this
r = ws.add_row
c = Cell.new row, 1, :value=>integer

# cells can also be accessed via Row#cells. The example here changes the cells type, which will automatically updated the value from 1 to 1.0
r.cells.last.type = :float

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, value = "", options = {}) ⇒ Cell

Returns a new instance of Cell.

Parameters:

  • row (Row)

    The row this cell belongs to.

  • value (Any) (defaults to: "")

    The value associated with this cell.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • type (Symbol)

    The intended data type for this cell. If not specified the data type will be determined internally based on the vlue provided.

  • style (Integer)

    The index of the cellXfs item to be applied to this cell. If not specified, the default style (0) will be applied.

  • font_name (String)
  • charset (Integer)
  • family (String)
  • b (Boolean)
  • i (Boolean)
  • strike (Boolean)
  • outline (Boolean)
  • shadow (Boolean)
  • condense (Boolean)
  • extend (Boolean)
  • u (Boolean)
  • vertAlign (Symbol)

    must be one of :baseline, :subscript, :superscript

  • sz (Integer)
  • color (String)

    an 8 letter rgb specification

  • scheme (Symbol)

    must be one of :none, major, :minor



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 177

def initialize(row, value="", options={})
  self.row=row
  @styles = row.worksheet.workbook.styles
  @style = 0 
  @type = cell_type_from_value(value)
  @row.cells << self      
  options.each do |o|
    self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
  end
  @value = cast_value(value)
end

Instance Attribute Details

#bBoolean

The inline bold property for the cell

Returns:

  • (Boolean)


84
85
86
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 84

def b
  @b
end

#charsetString

The inline charset property for the cell

Returns:

  • (String)


72
73
74
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 72

def charset
  @charset
end

#colorColor

The inline color property for the cell

Returns:



132
133
134
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 132

def color
  @color
end

#condenseBoolean

The inline condense property for the cell

Returns:

  • (Boolean)


114
115
116
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 114

def condense
  @condense
end

#extendBoolean

The inline extend property for the cell

Returns:

  • (Boolean)


120
121
122
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 120

def extend
  @extend
end

#familyString

The inline family property for the cell

Returns:

  • (String)


78
79
80
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 78

def family
  @family
end

#font_nameString

The inline font_name property for the cell

Returns:

  • (String)


66
67
68
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 66

def font_name
  @font_name
end

#iBoolean

The inline italic property for the cell

Returns:

  • (Boolean)


90
91
92
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 90

def i
  @i
end

#outlineBoolean

The inline outline property for the cell

Returns:

  • (Boolean)


102
103
104
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 102

def outline
  @outline
end

#rowRow

The row this cell belongs to.

Returns:



33
34
35
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 33

def row
  @row
end

#schemeSymbol

The inline scheme property for the cell this must be one of [:none, major, minor]

Returns:

  • (Symbol)


154
155
156
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 154

def scheme
  @scheme
end

#shadowBoolean

The inline shadow property for the cell

Returns:

  • (Boolean)


108
109
110
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 108

def shadow
  @shadow
end

#strikeBoolean

The inline strike property for the cell

Returns:

  • (Boolean)


96
97
98
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 96

def strike
  @strike
end

#styleInteger

The index of the cellXfs item to be applied to this cell.

Returns:

  • (Integer)

See Also:



29
30
31
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 29

def style
  @style
end

#szBoolean

The inline sz property for the cell

Returns:

  • (Boolean)


140
141
142
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 140

def sz
  @sz
end

#typeSymbol

Note:

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.

Returns:

  • (Symbol)

    The type of data this cell’s value is cast to.

Raises:

  • (ArgumentExeption)

    Cell.type must be one of [:time, :float, :integer, :string]

See Also:

  • #cell_type_from_value


46
47
48
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 46

def type
  @type
end

#uBoolean

The inline underline property for the cell

Returns:

  • (Boolean)


126
127
128
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 126

def u
  @u
end

#valueString, ...

The value of this cell.

Returns:

  • (String, Integer, Float, Time)

    casted value based on cell’s type attribute.



57
58
59
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 57

def value
  @value
end

#vertAlignSymbol

The inline vertical alignment property for the cell this must be one of [:baseline, :subscript, :superscript]

Returns:

  • (Symbol)


147
148
149
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 147

def vertAlign
  @vertAlign
end

Instance Method Details

#indexInteger

Returns The index of the cell in the containing row.

Returns:

  • (Integer)

    The index of the cell in the containing row.



190
191
192
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 190

def index
  @row.cells.index(self)
end

#posArray

Returns of x/y coordinates in the cheet for this cell.

Returns:

  • (Array)

    of x/y coordinates in the cheet for this cell.



218
219
220
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 218

def pos
  [index, row.index]
end

#rString

Returns The alpha(column)numeric(row) reference for this sell.

Examples:

Relative Cell Reference

ws.rows.first.cells.first.r #=> "A1" 

Returns:

  • (String)

    The alpha(column)numeric(row) reference for this sell.



197
198
199
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 197

def r
  "#{col_ref}#{@row.index+1}"      
end

#r_absString

Returns The absolute alpha(column)numeric(row) reference for this sell.

Examples:

Absolute Cell Reference

ws.rows.first.cells.first.r #=> "$A$1" 

Returns:

  • (String)

    The absolute alpha(column)numeric(row) reference for this sell.



204
205
206
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 204

def r_abs
  "$#{r.split('').join('$')}"
end

#to_xml(xml) ⇒ String

Note:

Shared Strings are not used in this library. All values are set directly in the each sheet.

Serializes the cell

Parameters:

  • xml (Nokogiri::XML::Builder)

    The document builder instance this objects xml will be added to.

Returns:

  • (String)

    xml text for the cell



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 227

def to_xml(xml)
  
  # however nokogiri does a nice 'force_encoding' which we shall remove!
  if @type == :string 
    #parse formula
    if @value.start_with?('=')
      xml.c(:r => r, :t=>:str, :s=>style) {
        xml.f @value.to_s.gsub('=', '')
      }
    else
      #parse standard string
      #xml.c(:r => r, :t=>:inlineStr, :s=>style) {
      #  xml.is { xml.t @value.to_s } 
      #}
      #parse styled string
      xml.c(:r => r, :s=>style, :t => :inlineStr) {
        xml.is {
          xml.r {
            xml.rPr {
              xml.rFont(:val=>@font_name) if @font_name
              xml.charset(:val=>@charset) if @charset
              xml.family(:val=>@family) if @family
              xml.b(:val=>@b) if @b
              xml.i(:val=>@i) if @i
              xml.strike(:val=>@strike) if @strike
              xml.outline(:val=>@outline) if @outline
              xml.shadow(:val=>@shadow) if @shadow
              xml.condense(:val=>@condense) if @condense
              xml.extend(:val=>@extend) if @extend
              @color.to_xml(xml) if @color
              xml.sz(:val=>@sz) if @sz
              xml.u(:val=>@u) if @u
              # :baseline, :subscript, :superscript
              xml.vertAlign(:val=>@vertAlign) if @verAlign
              # :none, major, :minor
              xml.scheme(:val=>@scheme) if @scheme
            }
            xml.t @value.to_s
          }
        }
      }
    end
  else
    xml.c(:r => r, :s => style) { xml.v value }
  end
end