Ruby Roo gem: how to extract the hyperlink text from a cell that contains hyperlink?

324 views Asked by At

I have MS Excel spreadsheets, where there are many cells with hyperlinks in them. I do not want the hyperlinks. Instead I want to use the visible texts in the cells only. How to get them?

Here is sample code

require 'roo-xls'
ifl = Roo::Spreadsheet.open(ifile)

sheet = ifl.sheet(0)
i = 0
while sheet.cell(i,0) != nil
  key = sheet.cell(i,0) # these cells contain hyperlinks, not usual strings
  puts key  # url is printed here instead of the visible text of the link
  i = i + 1
end
2

There are 2 answers

0
GrowthCode On

There is a different method to get the hyperlinks:

sheet.hyperlink(row, column)
0
balaji On

sheet.cell(i,0).to_s

It's better to convert it into a string. roo does not have any method to removea hyperlinkfrom a cell.