Class: Axlsx::CellSerializer

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

Overview

The Cell Serializer class contains the logic for serializing cells based on their type.

Class Method Summary collapse

Class Method Details

.array_formula_serialization(cell, str = '') ⇒ String

Serializes cells that are type array formula

Parameters:

  • cell (Cell)

    The cell that is being serialized

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

    The string the serialized content will be appended to.

Returns:

  • (String)

97
98
99
100
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 97

def array_formula_serialization(cell, str='')
  str << ('t="str">' << '<f t="array" ref="' << cell.r << '">' << cell.clean_value.to_s.sub('{=', '').sub(/}$/, '') << '</f>')
  str << ('<v>' << cell.formula_value.to_s << '</v>') unless cell.formula_value.nil?
end

.boolean(cell, str = '') ⇒ String

Serializes cells that are type boolean

Parameters:

  • cell (Cell)

    The cell that is being serialized

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

    The string the serialized content will be appended to.

Returns:

  • (String)

64
65
66
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 64

def boolean(cell, str='')
  value_serialization 'b', cell.value.to_s, str
end

.date(cell, str = '') ⇒ String

serializes cells that are type date

Parameters:

  • cell (Cell)

    The cell that is being serialized

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

    The string the serialized content will be appended to.

Returns:

  • (String)

48
49
50
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 48

def date(cell, str='')
  value_serialization false, DateTimeConverter::date_to_serial(cell.value).to_s, str
end

.float(cell, str = '') ⇒ String

Serializes cells that are type float

Parameters:

  • cell (Cell)

    The cell that is being serialized

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

    The string the serialized content will be appended to.

Returns:

  • (String)

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

def float(cell, str='')
  numeric cell, str
end

.formula_serialization(cell, str = '') ⇒ String

Serializes cells that are type formula

Parameters:

  • cell (Cell)

    The cell that is being serialized

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

    The string the serialized content will be appended to.

Returns:

  • (String)

88
89
90
91
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 88

def formula_serialization(cell, str='')
  str << ('t="str"><f>' << cell.clean_value.to_s.sub('=', '') << '</f>')
  str << ('<v>' << cell.formula_value.to_s << '</v>') unless cell.formula_value.nil?
end

.inline_string_serialization(cell, str = '') ⇒ String

Serializes cells that are type inline_string

Parameters:

  • cell (Cell)

    The cell that is being serialized

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

    The string the serialized content will be appended to.

Returns:

  • (String)

106
107
108
109
110
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 106

def inline_string_serialization(cell, str = '')
  str << 't="inlineStr"><is>'
  run_xml_string cell, str
  str << '</is>'
end

.integer(cell, str = '') ⇒ String

Serializes cells that are type integer

Parameters:

  • cell (Cell)

    The cell that is being serialized

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

    The string the serialized content will be appended to.

Returns:

  • (String)

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

def integer(cell, str = '')
  numeric cell, str
end

.iso_8601(cell, str = '') ⇒ String

serializes cells that are type iso_8601

Parameters:

  • cell (Cell)

    The cell that is being serialized

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

    The string the serialized content will be appended to.

Returns:

  • (String)

40
41
42
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 40

def iso_8601(cell, str='')
  value_serialization 'd', cell.value, str
end

.richtext(cell, str) ⇒ String

Serializes cells that are of the type richtext

Parameters:

  • cell (Cell)

    The cell that is being serialized

  • str (String)

    The string the serialized content will be appended to.

Returns:

  • (String)

132
133
134
135
136
137
138
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 132

def richtext(cell, str)
  if cell.ssti.nil?
    inline_string_serialization cell, str
  else
    value_serialization 's', cell.ssti, str
  end
end

.run_xml_string(cell, str = '') ⇒ String

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)

22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 22

def run_xml_string(cell, str = '')
  if cell.is_text_run?
    valid = RichTextRun::INLINE_STYLES - [:value, :type]
    data = Hash[cell.instance_values.map{ |k, v| [k.to_sym, v] }]
    data = data.select { |key, value| valid.include?(key) && !value.nil? }
    RichText.new(cell.value.to_s, data).to_xml_string(str)
  elsif cell.contains_rich_text?
    cell.value.to_xml_string(str)
  else
    str << ('<t>' << cell.clean_value << '</t>')
  end
  str
end

.string(cell, str = '') ⇒ String

Serializes cells that are type string

Parameters:

  • cell (Cell)

    The cell that is being serialized

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

    The string the serialized content will be appended to.

Returns:

  • (String)

116
117
118
119
120
121
122
123
124
125
126
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 116

def string(cell, str='')
  if cell.is_array_formula?
    array_formula_serialization cell, str
  elsif cell.is_formula?
    formula_serialization cell, str
  elsif !cell.ssti.nil?
    value_serialization 's', cell.ssti, str
  else
    inline_string_serialization cell, str
  end
end

.text(cell, str) ⇒ String

Serializes cells that are of the type text

Parameters:

  • cell (Cell)

    The cell that is being serialized

  • str (String)

    The string the serialized content will be appended to.

Returns:

  • (String)

144
145
146
147
148
149
150
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 144

def text(cell, str)
  if cell.ssti.nil?
    inline_string_serialization cell, str
  else
    value_serialization 's', cell.ssti, str
  end
end

.time(cell, str = '') ⇒ String

Serializes cells that are type time

Parameters:

  • cell (Cell)

    The cell that is being serialized

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

    The string the serialized content will be appended to.

Returns:

  • (String)

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

def time(cell, str='')
  value_serialization false, DateTimeConverter::time_to_serial(cell.value).to_s, str
end

.to_xml_string(row_index, column_index, cell, str = '') ⇒ String

Calls the proper serialization method based on type.

Parameters:

  • row_index (Integer)

    The index of the cell's row

  • column_index (Integer)

    The index of the cell's column

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

    The string to apend serialization to.

Returns:

  • (String)

11
12
13
14
15
16
17
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 11

def to_xml_string(row_index, column_index, cell, str='')
  str << ('<c r="' << Axlsx::cell_r(column_index, row_index) << '" s="' << cell.style.to_s << '" ')
  return str << '/>' if cell.value.nil?
  method = cell.type
  self.send(method, cell, str)
  str << '</c>'
end