blob: dac8221f0978302e829e6d624b9555c18e6f52ae (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# encoding: UTF-8
module Axlsx
# 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.
# @note Serialization performance is affected by using this serialization method so if you do not need interoperability
# it is recomended that you use the default inline string method of serialization.
class SharedStringsTable
# The total number of strings in the workbook including duplicates
# Empty cells are treated as blank strings
# @return [Integer]
attr_reader :count
# The total number of unique strings in the workbook.
# @return [Integer]
def unique_count
@unique_cells.size
end
# 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 Cell#sharable
attr_reader :unique_cells
# Creates a new Shared Strings Table agains an array of cells
# @param [Array] cells This is an array of all of the cells in the workbook
def initialize(cells)
cells = cells.flatten.reject { |c| c.type != :string || c.value.start_with?('=') }
@count = cells.size
@unique_cells = []
@shared_xml_string = ""
resolve(cells)
end
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
# Generate the xml document for the Shared Strings Table
# @return [String]
def to_xml
builder = Nokogiri::XML::Builder.new(:encoding => ENCODING) do |xml|
xml.sst(:xmlns => Axlsx::XML_NS, :count => count, :uniqueCount => unique_count) {
@unique_cells.each do |cell|
xml.si { cell.run_xml(xml) }
end
}
end
builder.to_xml(:save_with => 0)
end
private
# Interate over all of the cells in the array.
# if our unique cells array does not contain a sharable cell,
# add the cell to our unique cells array and set the ssti attribute on the index of this cell in the shared strings table
# if a sharable cell already exists in our unique_cells array, set the ssti attribute of the cell and move on.
# @return [Array] unique cells
def resolve(cells)
cells.each do |cell|
cell_hash = cell.shareable_hash
index = @unique_cells.index do |item|
item == cell_hash
end
if index == nil
cell.send :ssti=, @unique_cells.size
@shared_xml_string << '<si>' << cell.run_xml_string << '</si>'
@unique_cells << cell_hash
else
cell.send :ssti=, index
end
end
end
end
end
|