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.

See Also:

Constant Summary

INLINE_STYLES =

An array of available inline styes. TODO change this to a hash where each key defines attr name and validator (and any info the validator requires) then move it out to a module so we can re-use in in other classes. needs to define bla=(v) and bla methods on the class that hook into a set_attr method that kicks the suplied validator and updates the instance_variable for the key

['value', 'type', 'font_name', 'charset',
'family', 'b', 'i', 'strike','outline',
'shadow', 'condense', 'extend', 'u',
'vertAlign', 'sz', 'color', 'scheme']

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

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



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 196

def initialize(row, value="", options={})
  self.row=row
  @font_name = @charset = @family = @b = @i = @strike = @outline = @shadow = nil
  @condense = @u = @vertAlign = @sz = @color = @scheme = @extend = @ssti = nil
  @styles = row.worksheet.workbook.styles
  @row.cells << self
  options.each do |o|
    self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
  end
  @style ||= 0
  @type ||= cell_type_from_value(value)
  @value = cast_value(value)
end

Instance Attribute Details

- (Boolean) b

The inline bold property for the cell

Returns:

  • (Boolean)


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

def b
  @b
end

- (String) charset

The inline charset property for the cell

Returns:

  • (String)


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

def charset
  @charset
end

- (Color) color

The inline color property for the cell

Returns:



144
145
146
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 144

def color
  @color
end

- (Boolean) condense

The inline condense property for the cell

Returns:

  • (Boolean)


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

def condense
  @condense
end

- (Boolean) extend

The inline extend property for the cell

Returns:

  • (Boolean)


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

def extend
  @extend
end

- (String) family

The inline family property for the cell

Returns:

  • (String)


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

def family
  @family
end

- (String) font_name

The inline font_name property for the cell

Returns:

  • (String)


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

def font_name
  @font_name
end

- (Boolean) i

The inline italic property for the cell

Returns:

  • (Boolean)


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

def i
  @i
end

- (Boolean) outline

The inline outline property for the cell

Returns:

  • (Boolean)


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

def outline
  @outline
end

- (Row) row

The row this cell belongs to.

Returns:



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

def row
  @row
end

- (Symbol) scheme

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

Returns:

  • (Symbol)


170
171
172
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 170

def scheme
  @scheme
end

- (Boolean) shadow

The inline shadow property for the cell

Returns:

  • (Boolean)


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

def shadow
  @shadow
end

- (Integer) ssti

The Shared Strings Table index for this cell

Returns:

  • (Integer)


212
213
214
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 212

def ssti
  @ssti
end

- (Boolean) strike

The inline strike property for the cell

Returns:

  • (Boolean)


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

def strike
  @strike
end

- (Integer) style

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

Returns:

  • (Integer)

See Also:



26
27
28
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 26

def style
  @style
end

- (Boolean) sz

The inline sz property for the cell

Returns:

  • (Boolean)


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

def sz
  @sz
end

- (Symbol) type

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 conversions 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 six types are supported, :date, :time, :float, :integer, :string and :boolean. 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 [:date, time, :float, :integer, :string, :boolean]

See Also:

  • #cell_type_from_value


43
44
45
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 43

def type
  @type
end

- (Boolean) u

The inline underline property for the cell

Returns:

  • (Boolean)


138
139
140
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 138

def u
  @u
end

- (String, ...) value

The value of this cell.

Returns:

  • (String, Integer, Float, Time)

    casted value based on cell's type attribute.



54
55
56
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 54

def value
  @value
end

- (Symbol) vertAlign

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

Returns:

  • (Symbol)


160
161
162
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 160

def vertAlign
  @vertAlign
end

Instance Method Details

- (Integer) index

The index of the cell in the containing row.

Returns:

  • (Integer)

    The index of the cell in the containing row.



215
216
217
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 215

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

- (Boolean) is_text_run?

Indicates that the cell has one or more of the custom cell styles applied.

Returns:

  • (Boolean)


63
64
65
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 63

def is_text_run?
  @is_text_run ||= false
end

- (Object) merge(target)

Merges all the cells in a range created between this cell and the cell or string name for a cell provided

Parameters:

  • target (Cell, String)

    The last cell, or str ref for the cell in the merge range

See Also:

  • Axlsx::Cell.worksheetworksheet.merge_cells


250
251
252
253
254
255
256
257
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 250

def merge(target)
  range_end = if target.is_a?(String)
                target
              elsif(target.is_a?(Cell))
                target.r
              end
  self.row.worksheet.merge_cells "#{self.r}:#{range_end}" unless range_end.nil?
end

- (Boolean) plain_string?

Indicates if the cell is good for shared string table

Returns:

  • (Boolean)


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

def plain_string?
  @type == :string &&         # String typed
    !is_text_run? &&          # No inline styles
    !@value.nil? &&           # Not nil
    !@value.empty? &&         # Not empty
    !@value.start_with?('=')  # Not a formula
end

- (Array) pos

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

Returns:

  • (Array)

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



243
244
245
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 243

def pos
  [index, row.index]
end

- (String) r

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.



222
223
224
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 222

def r
  Axlsx::cell_r index, @row.index
end

- (String) r_abs

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.



229
230
231
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 229

def r_abs
  "$#{r.match(%r{([A-Z]+)([0-9]+)})[1,2].join('$')}"
end

- (String) run_xml_string(str = '')

builds an xml text run based on this cells attributes.

Parameters:

  • str (String) (defaults to: '')

    The string instance this run will be concated to.

Returns:

  • (String)


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 262

def run_xml_string(str = '')
  if is_text_run?
    data = self.instance_values.reject{|key, value| value == nil }
    keys = data.keys & INLINE_STYLES
    keys.delete ['value', 'type']
    str << "<r><rPr>"
    keys.each do |key|
      case key
      when 'font_name'
        str << "<rFont val='"<< @font_name << "'/>"
      when 'color'
        str << data[key].to_xml_string
      else
        "<" << key.to_s << " val='" << data[key].to_s << "'/>"
      end
    end
    str << "</rPr>" << "<t>" << value.to_s << "</t></r>"
  else
    str << "<t>" << value.to_s << "</t>"
  end
  str
end

- (String) to_xml_string(r_index, c_index, str = '')

Serializes the cell

Parameters:

  • r_index (Integer)

    The row index for the cell

  • c_index (Integer)

    The cell index in the row.

  • str (String) (defaults to: '')

    The string index the cell content will be appended to. Defaults to empty string.

Returns:

  • (String)

    xml text for the cell



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 290

def to_xml_string(r_index, c_index, str = '')
  str << '<c r="' << Axlsx::cell_r(c_index, r_index) << '" s="' << @style.to_s << '" '
  return str << '/>' if @value.nil?

  case @type

  when :string
    #parse formula
    if @value.start_with?('=')
      str  << 't="str"><f>' << @value.to_s.sub('=', '') << '</f>'
    else
      #parse shared
      if @ssti
        str << 't="s"><v>' << @ssti.to_s << '</v>'
      else
        str << 't="inlineStr"><is>' << run_xml_string << '</is>'
      end
    end
  when :date
    # TODO: See if this is subject to the same restriction as Time below
    str << '><v>' << DateTimeConverter::date_to_serial(@value).to_s << '</v>'
  when :time
    str << '><v>' << DateTimeConverter::time_to_serial(@value).to_s << '</v>'
  when :boolean
    str << 't="b"><v>' << @value.to_s << '</v>'
  else
    str << '><v>' << @value.to_s << '</v>'
  end
  str << '</c>'
end