Class: XlsxWriter::SharedStrings

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsx_writer/shared_strings.rb

Constant Summary collapse

BUFSIZE =

128kb

131072

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ SharedStrings

Returns a new instance of SharedStrings.



11
12
13
14
15
16
17
18
# File 'lib/xlsx_writer/shared_strings.rb', line 11

def initialize(document)
  @mutex = Mutex.new
  @document = document
  @indexes = {}
  @path = File.join document.staging_dir, relative_path
  FileUtils.mkdir_p File.dirname(path)
  @strings_tmp_file_writer = File.open(strings_tmp_file_path, 'wb')
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



7
8
9
# File 'lib/xlsx_writer/shared_strings.rb', line 7

def document
  @document
end

#indexesObject (readonly)

Returns the value of attribute indexes.



9
10
11
# File 'lib/xlsx_writer/shared_strings.rb', line 9

def indexes
  @indexes
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/xlsx_writer/shared_strings.rb', line 8

def path
  @path
end

Instance Method Details

#generateObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/xlsx_writer/shared_strings.rb', line 40

def generate
  return if generated?
  @mutex.synchronize do
    return if generated?
    @generated = true
    @strings_tmp_file_writer.close
    File.open(path, 'wb') do |f|
      f.write <<-EOS
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="#{indexes.length}" uniqueCount="#{indexes.length}">
EOS
      File.open(strings_tmp_file_path, 'rb') do |strings_tmp_file_reader|
        buffer = ''
        while strings_tmp_file_reader.read(BUFSIZE, buffer)
          f.write buffer
        end
      end
      f.write %{</sst>}
    end
    File.unlink strings_tmp_file_path
  end
end

#generated?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/xlsx_writer/shared_strings.rb', line 36

def generated?
  @generated == true
end

#ndx(str) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/xlsx_writer/shared_strings.rb', line 24

def ndx(str)
  @mutex.synchronize do
    digest = MurmurHash3::V128.str_digest str
    unless ndx = indexes[digest]
      ndx = indexes.length
      indexes[digest] = ndx
      @strings_tmp_file_writer.write %{<si><t>#{str.fast_xs}</t></si>}
    end
    ndx
  end
end

#relative_pathObject



20
21
22
# File 'lib/xlsx_writer/shared_strings.rb', line 20

def relative_path
  'xl/sharedStrings.xml'
end