Class: SimpleXlsxReader::Loader::SheetParser::HyperlinksParser

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/simple_xlsx_reader/loader/sheet_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_io, xrels:) ⇒ HyperlinksParser

Returns a new instance of HyperlinksParser.



203
204
205
206
# File 'lib/simple_xlsx_reader/loader/sheet_parser.rb', line 203

def initialize(file_io, xrels:)
  @file_io = file_io
  @xrels = xrels
end

Class Method Details

.parse(file_io, xrels:) ⇒ Object



208
209
210
# File 'lib/simple_xlsx_reader/loader/sheet_parser.rb', line 208

def self.parse(file_io, xrels:)
  new(file_io, xrels: xrels).parse
end

Instance Method Details

#parseObject



212
213
214
215
216
# File 'lib/simple_xlsx_reader/loader/sheet_parser.rb', line 212

def parse
  @hyperlinks_by_cell = {}
  Nokogiri::XML::SAX::Parser.new(self).parse(@file_io)
  @hyperlinks_by_cell
end

#start_element_namespace(name, attrs, _prefix, _uri, _ns) ⇒ Object



218
219
220
221
222
223
224
225
226
227
# File 'lib/simple_xlsx_reader/loader/sheet_parser.rb', line 218

def start_element_namespace(name, attrs, _prefix, _uri, _ns)
  case name
  when 'hyperlink'
    attrs = attrs.inject({}) {|acc, attr| acc[attr.localname] = attr.value; acc}
    id = attrs['id'] || attrs['r:id']

    @hyperlinks_by_cell[attrs['ref']] =
      @xrels.at_xpath(%(//*[@Id="#{id}"])).attr('Target')
  end
end