Skip to content

Commit e49a1da

Browse files
committed
Move comments handling into Excelx::Sheet
1 parent 4834e20 commit e49a1da

File tree

1 file changed

+34
-42
lines changed

1 file changed

+34
-42
lines changed

lib/roo/excelx.rb

+34-42
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,47 @@ def to_type(format)
6565
end
6666

6767
class Sheet
68-
def initialize(name, rels_doc, sheet_doc)
68+
def initialize(name, rels_doc, sheet_doc, comments_doc)
6969
@name = name
7070
@rels_doc = rels_doc
7171
@sheet_doc = sheet_doc
72+
@comments_doc = comments_doc
73+
end
74+
75+
def comment(key)
76+
comments[key]
77+
end
78+
79+
def comments
80+
@comments ||=
81+
if @comments_doc
82+
Hash[@comments_doc.xpath("//comments/commentList/comment").map do |comment|
83+
[ref_to_key(comment), comment.at_xpath('./text/r/t').text ]
84+
end]
85+
else
86+
{}
87+
end
7288
end
7389

7490
def hyperlink(key)
75-
@hyperlink ||=
91+
hyperlinks[key]
92+
end
93+
94+
private
95+
96+
def ref_to_key(element)
97+
Roo::Base.split_coordinate(element.attributes['ref'].to_s)
98+
end
99+
100+
def hyperlinks
101+
@hyperlinks ||=
76102
Hash[@sheet_doc.xpath("/worksheet/hyperlinks/hyperlink").map do |hyperlink|
77103
if hyperlink.attribute('id') && relationship = relationships[hyperlink.attribute('id').text]
78-
key = Roo::Base.split_coordinate(hyperlink.attributes['ref'].to_s)
79-
[key, relationship.attribute('Target').text]
104+
[ref_to_key(hyperlink), relationship.attribute('Target').text]
80105
end
81106
end.compact]
82-
@hyperlink[key]
83107
end
84108

85-
private
86-
87109
def relationships
88110
@relationships ||=
89111
if @rels_doc
@@ -116,8 +138,6 @@ def initialize(filename, options = {})
116138
@excelx_type = {}
117139
@excelx_value = {}
118140
@style = {}
119-
@comment = {}
120-
@comments_read = {}
121141
end
122142

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

142-
Sheet.new(sheet, @rels_doc[n], @sheet_doc[n])
162+
Sheet.new(sheet, @rels_doc[n], @sheet_doc[n], @comments_doc[n])
143163
end
144164

145165
# Returns the content of a spreadsheet-cell.
@@ -321,29 +341,18 @@ def hyperlink(row,col,sheet=nil)
321341
# returns the comment at (row/col)
322342
# nil if there is no comment
323343
def comment(row,col,sheet=nil)
324-
sheet ||= @default_sheet
325-
#read_cells(sheet)
326-
read_comments(sheet) unless @comments_read[sheet]
327-
row,col = normalize(row,col)
328-
@comment[sheet] && @comment[sheet][[row,col]]
344+
key = normalize(row,col)
345+
sheet_for(sheet).comment(key)
329346
end
330347

331348
# true, if there is a comment
332349
def comment?(row,col,sheet=nil)
333350
comment(row,col,sheet) != nil
334351
end
335352

336-
# returns each comment in the selected sheet as an array of elements
337-
# [row, col, comment]
338353
def comments(sheet=nil)
339-
sheet ||= @default_sheet
340-
read_comments(sheet) unless @comments_read[sheet]
341-
if @comment[sheet]
342-
@comment[sheet].each.collect do |elem|
343-
[elem[0][0],elem[0][1],elem[1]]
344-
end
345-
else
346-
[]
354+
sheet_for(sheet).comments.map do |(x, y), comment|
355+
[x, y, comment]
347356
end
348357
end
349358

@@ -534,23 +543,6 @@ def read_cells(sheet=nil)
534543
#end comments
535544
end
536545

537-
# Reads all comments from a sheet
538-
def read_comments(sheet=nil)
539-
sheet ||= @default_sheet
540-
validate_sheet!(sheet)
541-
n = self.sheets.index(sheet)
542-
return unless @comments_doc[n]
543-
@comments_doc[n].xpath("//comments/commentList/comment").each do |comment|
544-
ref = comment.attributes['ref'].to_s
545-
row,col = self.class.split_coordinate(ref)
546-
comment.xpath('./text/r/t').each do |text|
547-
@comment[sheet] ||= {}
548-
@comment[sheet][[row,col]] = text.text
549-
end
550-
end
551-
@comments_read[sheet] = true
552-
end
553-
554546
def read_labels
555547
@label ||= Hash[workbook_doc.xpath("//definedName").map do |defined_name|
556548
# "Sheet1!$C$5"

0 commit comments

Comments
 (0)