Skip to content

Commit

Permalink
Move comments handling into Excelx::Sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Empact committed Nov 22, 2014
1 parent 4834e20 commit e49a1da
Showing 1 changed file with 34 additions and 42 deletions.
76 changes: 34 additions & 42 deletions lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,47 @@ def to_type(format)
end

class Sheet
def initialize(name, rels_doc, sheet_doc)
def initialize(name, rels_doc, sheet_doc, comments_doc)
@name = name
@rels_doc = rels_doc
@sheet_doc = sheet_doc
@comments_doc = comments_doc
end

def comment(key)
comments[key]
end

def comments
@comments ||=
if @comments_doc
Hash[@comments_doc.xpath("//comments/commentList/comment").map do |comment|
[ref_to_key(comment), comment.at_xpath('./text/r/t').text ]
end]
else
{}
end
end

def hyperlink(key)
@hyperlink ||=
hyperlinks[key]
end

private

def ref_to_key(element)
Roo::Base.split_coordinate(element.attributes['ref'].to_s)
end

def hyperlinks
@hyperlinks ||=
Hash[@sheet_doc.xpath("/worksheet/hyperlinks/hyperlink").map do |hyperlink|
if hyperlink.attribute('id') && relationship = relationships[hyperlink.attribute('id').text]
key = Roo::Base.split_coordinate(hyperlink.attributes['ref'].to_s)
[key, relationship.attribute('Target').text]
[ref_to_key(hyperlink), relationship.attribute('Target').text]
end
end.compact]
@hyperlink[key]
end

private

def relationships
@relationships ||=
if @rels_doc
Expand Down Expand Up @@ -116,8 +138,6 @@ def initialize(filename, options = {})
@excelx_type = {}
@excelx_value = {}
@style = {}
@comment = {}
@comments_read = {}
end

def method_missing(m,*args)
Expand All @@ -139,7 +159,7 @@ def sheet_for(sheet)
validate_sheet!(sheet)
n = self.sheets.index(sheet)

Sheet.new(sheet, @rels_doc[n], @sheet_doc[n])
Sheet.new(sheet, @rels_doc[n], @sheet_doc[n], @comments_doc[n])
end

# Returns the content of a spreadsheet-cell.
Expand Down Expand Up @@ -321,29 +341,18 @@ def hyperlink(row,col,sheet=nil)
# returns the comment at (row/col)
# nil if there is no comment
def comment(row,col,sheet=nil)
sheet ||= @default_sheet
#read_cells(sheet)
read_comments(sheet) unless @comments_read[sheet]
row,col = normalize(row,col)
@comment[sheet] && @comment[sheet][[row,col]]
key = normalize(row,col)
sheet_for(sheet).comment(key)
end

# true, if there is a comment
def comment?(row,col,sheet=nil)
comment(row,col,sheet) != nil
end

# returns each comment in the selected sheet as an array of elements
# [row, col, comment]
def comments(sheet=nil)
sheet ||= @default_sheet
read_comments(sheet) unless @comments_read[sheet]
if @comment[sheet]
@comment[sheet].each.collect do |elem|
[elem[0][0],elem[0][1],elem[1]]
end
else
[]
sheet_for(sheet).comments.map do |(x, y), comment|
[x, y, comment]
end
end

Expand Down Expand Up @@ -534,23 +543,6 @@ def read_cells(sheet=nil)
#end comments
end

# Reads all comments from a sheet
def read_comments(sheet=nil)
sheet ||= @default_sheet
validate_sheet!(sheet)
n = self.sheets.index(sheet)
return unless @comments_doc[n]
@comments_doc[n].xpath("//comments/commentList/comment").each do |comment|
ref = comment.attributes['ref'].to_s
row,col = self.class.split_coordinate(ref)
comment.xpath('./text/r/t').each do |text|
@comment[sheet] ||= {}
@comment[sheet][[row,col]] = text.text
end
end
@comments_read[sheet] = true
end

def read_labels
@label ||= Hash[workbook_doc.xpath("//definedName").map do |defined_name|
# "Sheet1!$C$5"
Expand Down

0 comments on commit e49a1da

Please sign in to comment.