How to I add a hyperlink to a cell in axlsx?
Asked Answered
D

1

7

With the spreadsheet gem, you can run Spreadsheet::Link.new('http://hyperlinkhere.com', 'Some words') to make a spreadsheet with a cell containing the string "Some words" with a hyperlink leading to "http://hyperlinkhere.com."

What's the axlsx equivalent?

EDIT: What if I want to write a row with more than one cell?

With spreadsheet, you can do this:

        newSheetRow[13] = Spreadsheet::Link.new('url.com','text')
        newSheetRow[14] = 'some text'

How do I do that with axlsx's .add_row method?

Dustup answered 23/4, 2015 at 19:39 Comment(1)
My case is a bit different, but since this answers is well linked already: I wanted to add a hyperlink to only a portion of text within a cell, but after manual testing in Excel, found that is not possible: superuser.com/questions/428299/…Reckless
H
12

You can add both links within workbook and URLs.

p = Axlsx::Package.new
book = p.workbook
book.add_worksheet(:name => 'hyperlinks') do |sheet|
  # external references
  sheet.add_row ['axlsx']
  sheet.add_hyperlink :location => 'https://github.com/randym/axlsx', :ref => sheet.rows.first.cells.first
  # internal references
  sheet.add_hyperlink :location => "'Next Sheet'!A1", :ref => 'A2', :target => :sheet
  sheet.add_row ['next sheet']
end
Hoes answered 2/5, 2015 at 16:55 Comment(1)
if i add integer is it not working 'sheet.add_row ['123']'Fairleigh

© 2022 - 2024 — McMap. All rights reserved.