Class: Axlsx::Cell

Inherits:
Object
  • Object
show all
Includes:
OptionsParser
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 collapse

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].freeze
CELL_TYPES =

An array of valid cell types

[:date, :time, :float, :integer, :richtext,
:string, :boolean, :iso_8601, :text].freeze
FORMULA_PREFIXES =

Leading characters that indicate a formula. See: https://owasp.org/www-community/attacks/CSV_Injection

['-', '=', '+', '@', '%', '|', "\r", "\t"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsParser

#parse_options

Constructor Details

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

Returns a new instance of Cell.

Parameters:

  • row (Row)

    The row this cell belongs to.

  • value (Any) (defaults to: nil)

    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 value 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

  • formula_value (Number)

    The value to cache for a formula cell.

  • scheme (Symbol)

    must be one of :none, major, :minor

  • escape_formulas (Boolean)

    Whether to treat values starting with an equals sign as formulas or as literal strings. Allowing user-generated data to be interpreted as formulas is a security risk. See https://www.owasp.org/index.php/CSV_Injection for details.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 34

def initialize(row, value = nil, options = {})
  @row = row
  # Do not use instance vars if not needed to use less RAM
  # And do not call parse_options on frequently used options
  # to get less GC cycles
  type = options.delete(:type) || cell_type_from_value(value)
  self.type = type unless type == :string

  val = options.delete(:style)
  self.style = val unless val.nil? || val == 0
  val = options.delete(:formula_value)
  self.formula_value = val unless val.nil?

  parse_options(options)
  self.escape_formulas = row.worksheet.escape_formulas if escape_formulas.nil?

  self.value = value
  value.cell = self if contains_rich_text?
end

Instance Attribute Details

#bBoolean

The inline bold property for the cell

Returns:

  • (Boolean)


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

def b
  @b
end

#charsetString

The inline charset property for the cell As far as I can tell, this is pretty much ignored. However, based on the spec it should be one of the following: 0  ANSI_CHARSET 1 DEFAULT_CHARSET 2 SYMBOL_CHARSET 77 MAC_CHARSET 128 SHIFTJIS_CHARSET 129  HANGUL_CHARSET 130  JOHAB_CHARSET 134  GB2312_CHARSET 136  CHINESEBIG5_CHARSET 161  GREEK_CHARSET 162  TURKISH_CHARSET 163  VIETNAMESE_CHARSET 177  HEBREW_CHARSET 178  ARABIC_CHARSET 186  BALTIC_CHARSET 204  RUSSIAN_CHARSET 222  THAI_CHARSET 238  EASTEUROPE_CHARSET 255  OEM_CHARSET

Returns:

  • (String)


208
209
210
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 208

def charset
  @charset
end

#colorColor

The inline color property for the cell

Returns:



291
292
293
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 291

def color
  @color
end

#condenseBoolean

The inline condense property for the cell

Returns:

  • (Boolean)


264
265
266
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 264

def condense
  @condense
end

#escape_formulasBoolean

Whether to treat values starting with an equals sign as formulas or as literal strings. Allowing user-generated data to be interpreted as formulas is a security risk. See https://www.owasp.org/index.php/CSV_Injection for details.

Returns:

  • (Boolean)


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

def escape_formulas
  @escape_formulas
end

#extendBoolean

The inline extend property for the cell

Returns:

  • (Boolean)


271
272
273
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 271

def extend
  @extend
end

#familyInteger

The inline family property for the cell 1 Roman 2 Swiss 3 Modern 4 Script 5 Decorative

Returns:

  • (Integer)


220
221
222
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 220

def family
  @family
end

#font_nameString

The inline font_name property for the cell

Returns:

  • (String)


181
182
183
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 181

def font_name
  @font_name
end

#formula_valueObject

this is the cached value for formula cells. If you want the values to render in iOS/Mac OSX preview you need to set this.



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

def formula_value
  @formula_value
end

#iBoolean

The inline italic property for the cell

Returns:

  • (Boolean)


236
237
238
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 236

def i
  @i
end

#nameObject

returns the name of the cell



415
416
417
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 415

def name
  @name
end

#outlineBoolean

The inline outline property for the cell

Returns:

  • (Boolean)


250
251
252
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 250

def outline
  @outline
end

#raw_styleObject

Returns the value of attribute raw_style.



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

def raw_style
  @raw_style
end

#rowRow (readonly)

The row this cell belongs to.

Returns:



112
113
114
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 112

def row
  @row
end

#schemeSymbol

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

Returns:

  • (Symbol)


320
321
322
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 320

def scheme
  @scheme
end

#shadowBoolean

The inline shadow property for the cell

Returns:

  • (Boolean)


257
258
259
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 257

def shadow
  @shadow
end

#sstiInteger

The Shared Strings Table index for this cell

Returns:

  • (Integer)


330
331
332
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 330

def ssti
  @ssti
end

#strikeBoolean

The inline strike property for the cell

Returns:

  • (Boolean)


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

def strike
  @strike
end

#szInteter

The inline sz property for the cell

Returns:

  • (Inteter)


301
302
303
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 301

def sz
  @sz
end

#uBoolean, String

Note:

true is for backwards compatability and is reassigned to :single

The inline underline property for the cell. It must be one of :none, :single, :double, :singleAccounting, :doubleAccounting, true

Returns:

  • (Boolean)
  • (String)


281
282
283
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 281

def u
  @u
end

#valueString, ...

The value of this cell.

Returns:

  • (String, Integer, Float, Time, Boolean)

    casted value based on cell's type attribute.



151
152
153
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 151

def value
  @value
end

#vertAlignSymbol

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

Returns:

  • (Symbol)


309
310
311
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 309

def vertAlign
  @vertAlign
end

Instance Method Details

#add_style(style) ⇒ Object

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

Parameters:

  • styles (Hash)

See Also:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 89

def add_style(style)
  self.raw_style ||= {}

  new_style = Axlsx.hash_deep_merge(raw_style, style)

  all_edges = [:top, :right, :bottom, :left]

  if !raw_style[:border].nil? && !style[:border].nil?
    border_at = (raw_style[:border][:edges] || all_edges) + (style[:border][:edges] || all_edges)
    new_style[:border][:edges] = border_at.uniq.sort
  elsif !style[:border].nil?
    new_style[:border] = style[:border]
  end

  self.raw_style = new_style

  wb = row.worksheet.workbook

  wb.styled_cells << self
end

#autowidthFloat

Attempts to determine the correct width for this cell's content

Returns:

  • (Float)


419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 419

def autowidth
  return if is_formula? || value.nil?

  if contains_rich_text?
    string_width('', font_size) + value.autowidth
  elsif styles.cellXfs[style].alignment && styles.cellXfs[style].alignment.wrap_text
    max_width = 0
    value.to_s.split(/\r?\n/).each do |line|
      width = string_width(line, font_size)
      max_width = width if width > max_width
    end
    max_width
  else
    string_width(value, font_size)
  end
end

#clean_valueObject

Returns the sanatized value TODO find a better way to do this as it accounts for 30% of processing time in benchmarking...



439
440
441
442
443
444
445
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 439

def clean_value
  if (type == :string || type == :text) && !Axlsx::trust_input
    Axlsx::sanitize(::CGI.escapeHTML(@value.to_s))
  else
    @value.to_s
  end
end

#contains_rich_text?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 165

def contains_rich_text?
  type == :richtext
end

#indexInteger

Returns The index of the cell in the containing row.

Returns:

  • (Integer)

    The index of the cell in the containing row.



333
334
335
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 333

def index
  @row.index(self)
end

#is_array_formula?Boolean

Returns:

  • (Boolean)


393
394
395
396
397
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 393

def is_array_formula?
  return false if escape_formulas

  type == :string && @value.to_s.start_with?('{=') && @value.to_s.end_with?('}')
end

#is_formula?Boolean

Returns:

  • (Boolean)


387
388
389
390
391
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 387

def is_formula?
  return false if escape_formulas

  type == :string && @value.to_s.start_with?(*FORMULA_PREFIXES)
end

#is_text_run?Boolean

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

Returns:

  • (Boolean)


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

def is_text_run?
  defined?(@is_text_run) && @is_text_run && !contains_rich_text?
end

#merge(target) ⇒ Object

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


369
370
371
372
373
374
375
376
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 369

def merge(target)
  start, stop = if target.is_a?(String)
                  [self.r, target]
                elsif target.is_a?(Cell)
                  Axlsx.sort_cells([self, target]).map { |c| c.r }
                end
  self.row.worksheet.merge_cells "#{start}:#{stop}" unless stop.nil?
end

#plain_string?Boolean

Indicates if the cell is good for shared string table

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 170

def plain_string?
  (type == :string || type == :text) &&         # String typed
    !is_text_run? &&          # No inline styles
    !@value.nil? &&           # Not nil
    !@value.empty? &&         # Not empty
    !is_formula? &&           # Not a formula
    !is_array_formula?        # Not an array formula
end

#posArray

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

Returns:

  • (Array)

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



362
363
364
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 362

def pos
  [index, row.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.



340
341
342
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 340

def r
  Axlsx::cell_r index, @row.row_index
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.



347
348
349
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 347

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

#reference(absolute = true) ⇒ String

returns the absolute or relative string style reference for this cell. returned.

Parameters:

  • absolute (Boolean) (defaults to: true)

    -when false a relative reference will be

Returns:

  • (String)


404
405
406
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 404

def reference(absolute = true)
  absolute ? r_abs : r
end

#styleInteger

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

Returns:

  • (Integer)

See Also:



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

def style
  defined?(@style) ? @style : 0
end

#style=(v) ⇒ Integer

Returns The cellXfs item index applied to this cell.

Returns:

  • (Integer)

    The cellXfs item index applied to this cell.

Raises:

  • (ArgumentError)

    Invalid cellXfs id if the value provided is not within cellXfs items range.



353
354
355
356
357
358
359
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 353

def style=(v)
  Axlsx::validate_unsigned_int(v)
  count = styles.cellXfs.size
  raise ArgumentError, "Invalid cellXfs id" unless v < count

  @style = v
end

#to_xml_string(r_index, c_index, str = '') ⇒ String

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



383
384
385
# File 'lib/axlsx/workbook/worksheet/cell.rb', line 383

def to_xml_string(r_index, c_index, str = '')
  CellSerializer.to_xml_string r_index, c_index, self, str
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 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. 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


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

def type
  defined?(@type) ? @type : :string
end

#type=(v) ⇒ Object

See Also:



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

def type=(v)
  RestrictionValidator.validate :cell_type, CELL_TYPES, v
  @type = v
  self.value = @value unless !defined?(@value) || @value.nil?
end