Class: Axlsx::SharedStringsTable

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

Overview

Note:

Serialization performance is affected by using this serialization method so if you do not need interoperability

The Shared String Table class is responsible for managing and serializing common strings in a workbook. While the ECMA-376 spec allows for both inline and shared strings it seems that at least some applications like iWorks Numbers and Google Docs require that the shared string table is populated in order to interoperate properly. As a developer, you should never need to directly work against this class. Simply set 'use_shared_strings' on the package or workbook to generate a package that uses the shared strings table instead of inline strings. it is recomended that you use the default inline string method of serialization.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cells) ⇒ SharedStringsTable

Creates a new Shared Strings Table agains an array of cells

Parameters:

  • cells (Array)

    This is an array of all of the cells in the workbook



31
32
33
34
35
36
37
38
# File 'lib/axlsx/workbook/shared_strings_table.rb', line 31

def initialize(cells)
  @index = 0
  @unique_cells = {}
  @shared_xml_string = ""
  shareable_cells = cells.flatten.select{ |cell| cell.plain_string? }
  @count = shareable_cells.size
  resolve(shareable_cells)
end

Instance Attribute Details

#countInteger (readonly)

The total number of strings in the workbook including duplicates Empty cells are treated as blank strings

Returns:

  • (Integer)


16
17
18
# File 'lib/axlsx/workbook/shared_strings_table.rb', line 16

def count
  @count
end

#unique_cellsObject (readonly)

An array of unique cells. Multiple attributes of the cell are used in comparison each of these unique cells is parsed into the shared string table.

See Also:

  • Cell#sharable


27
28
29
# File 'lib/axlsx/workbook/shared_strings_table.rb', line 27

def unique_cells
  @unique_cells
end

Instance Method Details

#to_xml_stringString

Serializes the object

Parameters:

  • str (String)

Returns:

  • (String)


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

def to_xml_string
  '<?xml version="1.0" encoding="UTF-8"?><sst xmlns="' << XML_NS << '" count="' << @count.to_s << '" uniqueCount="' << unique_count.to_s << '">' << @shared_xml_string << '</sst>'
end

#unique_countInteger

The total number of unique strings in the workbook.

Returns:

  • (Integer)


20
21
22
# File 'lib/axlsx/workbook/shared_strings_table.rb', line 20

def unique_count
  @unique_cells.size
end