From 75bc50a38035ed3ca741164982aea320234a42c2 Mon Sep 17 00:00:00 2001 From: Rui Castro Date: Thu, 27 Mar 2014 23:18:43 +0000 Subject: [PATCH 1/2] Added test for nil in Roo::Excelx#read_hyperlinks to avoid error "NoMethodError (undefined method `text' for nil:NilClass)" --- .gitignore | 1 + lib/roo/excelx.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1ec2ce82..e086f14f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /pkg/ /log/ .ruby-version +.project diff --git a/lib/roo/excelx.rb b/lib/roo/excelx.rb index a6d04c2e..d5177d02 100644 --- a/lib/roo/excelx.rb +++ b/lib/roo/excelx.rb @@ -539,7 +539,7 @@ def read_hyperlinks(sheet=nil) [r.attribute('Id').text, r] end] @sheet_doc[n].xpath("/xmlns:worksheet/xmlns:hyperlinks/xmlns:hyperlink").each do |h| - if rel_element = rels[h.attribute('id').text] + if h.attribute('id') && rel_element = rels[h.attribute('id').text] row,col = Roo::Base.split_coordinate(h.attributes['ref'].to_s) @hyperlink[sheet] ||= {} @hyperlink[sheet][[row,col]] = rel_element.attribute('Target').text From d69d198510e186ab3d4b36800925879683c20f74 Mon Sep 17 00:00:00 2001 From: Rui Castro Date: Sun, 30 Mar 2014 17:37:39 +0100 Subject: [PATCH 2/2] Updated the XPATH query to "/xmlns:worksheet/xmlns:hyperlinks/xmlns:hyperlink[id]" to avoid the previous error "NoMethodError (undefined method `text' for nil:NilClass)" --- lib/roo/excelx.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/roo/excelx.rb b/lib/roo/excelx.rb index d5177d02..80f897c7 100644 --- a/lib/roo/excelx.rb +++ b/lib/roo/excelx.rb @@ -538,8 +538,8 @@ def read_hyperlinks(sheet=nil) rels = Hash[rels_doc.xpath("/xmlns:Relationships/xmlns:Relationship").map do |r| [r.attribute('Id').text, r] end] - @sheet_doc[n].xpath("/xmlns:worksheet/xmlns:hyperlinks/xmlns:hyperlink").each do |h| - if h.attribute('id') && rel_element = rels[h.attribute('id').text] + @sheet_doc[n].xpath("/xmlns:worksheet/xmlns:hyperlinks/xmlns:hyperlink[id]").each do |h| + if rel_element = rels[h.attribute('id').text] row,col = Roo::Base.split_coordinate(h.attributes['ref'].to_s) @hyperlink[sheet] ||= {} @hyperlink[sheet][[row,col]] = rel_element.attribute('Target').text