Class: LiquidXlsx::SharedStrings

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

Overview

Reads and provides access to shared strings from xl/sharedStrings.xml.

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ SharedStrings

Returns a new instance of SharedStrings.

Parameters:

  • xml (String)

    XML content of sharedStrings.xml or nil if not present



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/liquid_xlsx/shared_strings.rb', line 7

def initialize(xml)
  @strings = []
  return unless xml
  return if xml.strip.empty?

  doc = Nokogiri::XML(xml)
  ns = doc.root&.namespace
  return unless ns

  doc.xpath("//xmlns:si", "xmlns" => ns.href).each do |si|
    t = si.at_xpath("xmlns:t")
    @strings << if t
                  t.text
                else
                  # Handle rich text or other cases - extract all text
                  si.xpath(".//xmlns:t").map(&:text).join
                end
  end
end

Instance Method Details

#[](index) ⇒ String?

Get string by index.

Parameters:

  • index (Integer)

Returns:

  • (String, nil)


30
31
32
# File 'lib/liquid_xlsx/shared_strings.rb', line 30

def [](index)
  @strings[index]
end

#countInteger

Get the number of shared strings.

Returns:

  • (Integer)


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

def count
  @strings.length
end

#raw_xmlString?

Get the raw XML (preserved as-is for re-insertion).

Returns:

  • (String, nil)


42
43
44
45
# File 'lib/liquid_xlsx/shared_strings.rb', line 42

def raw_xml
  # We keep the original content — we never modify shared strings.
  nil
end