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)


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

def array_formula_serialization(cell, str = +'')
  str << 't="str">' << '<f t="array" ref="' << cell.r << '">' << cell.clean_value.delete_prefix(ARRAY_FORMULA_PREFIX).delete_suffix(ARRAY_FORMULA_SUFFIX) << '</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)


68
69
70
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 68

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)


52
53
54
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 52

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)


76
77
78
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 76

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)


92
93
94
95
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 92

def formula_serialization(cell, str = +'')
  str << 't="str"><f>' << cell.clean_value.delete_prefix(FORMULA_PREFIX) << '</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)


110
111
112
113
114
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 110

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)


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

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)


44
45
46
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 44

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)


136
137
138
139
140
141
142
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 136

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)


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 26

def run_xml_string(cell, str = +'')
  if cell.is_text_run?
    valid = RichTextRun::INLINE_STYLES - [:value, :type]
    data = Axlsx.instance_values_for(cell).transform_keys(&:to_sym)
    data = data.select { |key, value| !value.nil? && valid.include?(key) }
    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)


120
121
122
123
124
125
126
127
128
129
130
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 120

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)


148
149
150
151
152
153
154
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 148

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)


60
61
62
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 60

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)


12
13
14
15
16
17
18
19
20
21
# File 'lib/axlsx/workbook/worksheet/cell_serializer.rb', line 12

def to_xml_string(row_index, column_index, cell, str = +'')
  str << '<c r="'
  str << Axlsx.col_ref(column_index) << Axlsx.row_ref(row_index)
  str << '" s="' << cell.style_str << '" '
  return str << '/>' if cell.value.nil?

  method = cell.type
  send(method, cell, str)
  str << '</c>'
end