Class: LiquidXlsx::SharedStrings
- Inherits:
-
Object
- Object
- LiquidXlsx::SharedStrings
- Defined in:
- lib/liquid_xlsx/shared_strings.rb
Overview
Reads and provides access to shared strings from xl/sharedStrings.xml.
Instance Method Summary collapse
-
#[](index) ⇒ String?
Get string by index.
-
#count ⇒ Integer
Get the number of shared strings.
-
#initialize(xml) ⇒ SharedStrings
constructor
A new instance of SharedStrings.
-
#raw_xml ⇒ String?
Get the raw XML (preserved as-is for re-insertion).
Constructor Details
#initialize(xml) ⇒ SharedStrings
Returns a new instance of SharedStrings.
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.
30 31 32 |
# File 'lib/liquid_xlsx/shared_strings.rb', line 30 def [](index) @strings[index] end |
#count ⇒ Integer
Get the number of shared strings.
36 37 38 |
# File 'lib/liquid_xlsx/shared_strings.rb', line 36 def count @strings.length end |
#raw_xml ⇒ String?
Get the raw XML (preserved as-is for re-insertion).
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 |