2

I'm trying to generate a sheet which has a column of URLs which I would like to present as hyperlinks, not just text. All the examples I've found, show adding a hyperlink this way:

sheet.add_hyperlink :location => 'https://github.com/randym/axlsx', :ref => sheet.rows.first.cells.first

This works if you're just trying to put one hyperlink somewhere on the page, but I'm iterating over a collection and each row will have a link in a certain column, depending on which columns are present.

I attempted the following:

@items.each do |item|
  row = []

  idx = 0
  ...
  idx += 1
  row << item.address
  url_idx = idx
  ...
  r = sheet.add_row row
  sheet.add_hyperlink :location => item.address, :ref => r[url_idx]
end

but that just resulted in an error:

undefined method `[]' for #<Axlsx::Row:0x007fa0024fb600>

This really seems more complicated than it should be so I think I'm missing something obvious.

Ref: How to I add a hyperlink to a cell in axlsx?

Community
  • 1
  • 1
Chip Roberson
  • 532
  • 3
  • 12

1 Answers1

0

I'm guessing you figured it out by now, but for anyone else who's looking: 'r' as defined here would be an object rather than an array, so calling 'r[url_idx]' would try to access a method which, of course, doesn't exist. You could try something like 'sheet.rows[url_idx]' to get the row but you'd still need to then specify which cell to put the hyperlink on.

Hope that helps somebody!

Sam House
  • 16
  • 2