Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def initialize(filename_or_stream, options = {})
sheet_options[:expand_merged_ranges] = (options[:expand_merged_ranges] || false)
sheet_options[:no_hyperlinks] = (options[:no_hyperlinks] || false)
sheet_options[:empty_cell] = (options[:empty_cell] || false)
sheet_options[:support_excel_sheet_hyperlinks] = (options[:support_excel_sheet_hyperlinks] || false)
shared_options = {}

shared_options[:disable_html_wrapper] = (options[:disable_html_wrapper] || false)
Expand Down
4 changes: 2 additions & 2 deletions lib/roo/excelx/sheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize(name, shared, sheet_index, options = {})
end

def cells
@cells ||= @sheet.cells(@rels)
@cells ||= @sheet.cells
end

def present_cells
Expand Down Expand Up @@ -85,7 +85,7 @@ def excelx_format(key)
end

def hyperlinks
@hyperlinks ||= @sheet.hyperlinks(@rels)
@hyperlinks ||= @sheet.hyperlinks
end

def comments
Expand Down
35 changes: 19 additions & 16 deletions lib/roo/excelx/sheet_doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ def initialize(path, relationships, shared, options = {})
@relationships = relationships
end

def cells(relationships)
@cells ||= extract_cells(relationships)
def cells
@cells ||= extract_cells
end

def hyperlinks(relationships)
def hyperlinks
# If you're sure you're not going to need this hyperlinks you can discard it
@hyperlinks ||= if @options[:no_hyperlinks] || !relationships.include_type?("hyperlink")
@hyperlinks ||= if @options[:no_hyperlinks] || (!@relationships.include_type?("hyperlink") && !@options[:support_excel_sheet_hyperlinks])
{}
else
extract_hyperlinks(relationships)
extract_hyperlinks
end
end

Expand All @@ -47,9 +47,9 @@ def each_cell(row_xml)
return [] unless row_xml
row_xml.children.each do |cell_element|
coordinate = ::Roo::Utils.extract_coordinate(cell_element["r"])
hyperlinks = hyperlinks(@relationships)[coordinate]
row_hyperlink = hyperlinks[coordinate]

yield cell_from_xml(cell_element, hyperlinks, coordinate)
yield cell_from_xml(cell_element, row_hyperlink, coordinate)
end
end

Expand Down Expand Up @@ -173,17 +173,20 @@ def create_cell_from_value(value_type, cell, formula, format, style, hyperlink,
end
end

def extract_hyperlinks(relationships)
def extract_hyperlinks
return {} unless (hyperlinks = doc.xpath('/worksheet/hyperlinks/hyperlink'))

hyperlinks.each_with_object({}) do |hyperlink, hash|
if relationship = relationships[hyperlink['id']]
target_link = relationship['Target']
target_link += "##{hyperlink['location']}" if hyperlink['location']
relationship = @relationships[hyperlink['id']]
# If the relationship is not found, we can still use the location for the excel sheet link
target_link = relationship['Target'] if relationship
target_link += "##{hyperlink['location']}" if hyperlink['location'] && (!@options[:support_excel_sheet_hyperlinks] || relationship)
target_link = "##{hyperlink['location']}" if @options[:support_excel_sheet_hyperlinks] && hyperlink['location'] && !relationship

Roo::Utils.coordinates_in_range(hyperlink["ref"].to_s) do |coord|
hash[coord] = target_link
end
next unless target_link

Roo::Utils.coordinates_in_range(hyperlink["ref"].to_s) do |coord|
hash[coord] = target_link
end
end
end
Expand All @@ -207,7 +210,7 @@ def expand_merged_ranges(cells)
end
end

def extract_cells(relationships)
def extract_cells
extracted_cells = {}
empty_cell = @options[:empty_cell]

Expand All @@ -221,7 +224,7 @@ def extract_cells(relationships)
::Roo::Utils.extract_coordinate(r)
end

cell = cell_from_xml(cell_xml, hyperlinks(relationships)[coordinate], coordinate, empty_cell)
cell = cell_from_xml(cell_xml, hyperlinks[coordinate], coordinate, empty_cell)
extracted_cells[coordinate] = cell if cell
end
end
Expand Down
18 changes: 15 additions & 3 deletions spec/lib/roo/excelx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@
it 'returns the expected result' do
expect(subject.hyperlink?(1, 1)).to eq true
expect(subject.hyperlink?(1, 2)).to eq false
expect(subject.hyperlink?(3, 1)).to eq false
end

context 'defined on cell range' do
Expand Down Expand Up @@ -660,13 +661,14 @@

describe 'Roo::Excelx with options set' do
subject(:xlsx) do
Roo::Excelx.new(path, disable_html_wrapper: true)
Roo::Excelx.new(path, options)
end

describe '#html_strings' do
describe "HTML Parsing Disabled" do
let(:path) { 'test/files/html_strings_formatting.xlsx' }
let(:path) { 'test/files/html_strings_formatting.xlsx' }
let(:options) { {disable_html_wrapper: true} }

describe "HTML Parsing Disabled" do
it 'returns the expected result' do
expect(subject.excelx_value(1, 1, "Sheet1")).to eq("This has no formatting.")
expect(subject.excelx_value(2, 1, "Sheet1")).to eq("This has bold formatting.")
Expand Down Expand Up @@ -696,4 +698,14 @@
end
end
end

describe '#match_relationships_with_hyperlinks' do
let(:path) { 'test/files/sheet_link.xlsx' }
let(:options) { { support_excel_sheet_hyperlinks: true } }

it 'should display excel links' do
expect(subject.hyperlink(1, 1)).to eq "http://www.google.com/"
expect(subject.hyperlink(1, 2)).to eq "#Sheet2!A1"
end
end
end
Binary file added test/files/sheet_link.xlsx
Binary file not shown.