@@ -65,25 +65,47 @@ def to_type(format)
65
65
end
66
66
67
67
class Sheet
68
- def initialize ( name , rels_doc , sheet_doc )
68
+ def initialize ( name , rels_doc , sheet_doc , comments_doc )
69
69
@name = name
70
70
@rels_doc = rels_doc
71
71
@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
72
88
end
73
89
74
90
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 ||=
76
102
Hash [ @sheet_doc . xpath ( "/worksheet/hyperlinks/hyperlink" ) . map do |hyperlink |
77
103
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 ]
80
105
end
81
106
end . compact ]
82
- @hyperlink [ key ]
83
107
end
84
108
85
- private
86
-
87
109
def relationships
88
110
@relationships ||=
89
111
if @rels_doc
@@ -116,8 +138,6 @@ def initialize(filename, options = {})
116
138
@excelx_type = { }
117
139
@excelx_value = { }
118
140
@style = { }
119
- @comment = { }
120
- @comments_read = { }
121
141
end
122
142
123
143
def method_missing ( m , *args )
@@ -139,7 +159,7 @@ def sheet_for(sheet)
139
159
validate_sheet! ( sheet )
140
160
n = self . sheets . index ( sheet )
141
161
142
- Sheet . new ( sheet , @rels_doc [ n ] , @sheet_doc [ n ] )
162
+ Sheet . new ( sheet , @rels_doc [ n ] , @sheet_doc [ n ] , @comments_doc [ n ] )
143
163
end
144
164
145
165
# Returns the content of a spreadsheet-cell.
@@ -321,29 +341,18 @@ def hyperlink(row,col,sheet=nil)
321
341
# returns the comment at (row/col)
322
342
# nil if there is no comment
323
343
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 )
329
346
end
330
347
331
348
# true, if there is a comment
332
349
def comment? ( row , col , sheet = nil )
333
350
comment ( row , col , sheet ) != nil
334
351
end
335
352
336
- # returns each comment in the selected sheet as an array of elements
337
- # [row, col, comment]
338
353
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 ]
347
356
end
348
357
end
349
358
@@ -534,23 +543,6 @@ def read_cells(sheet=nil)
534
543
#end comments
535
544
end
536
545
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
-
554
546
def read_labels
555
547
@label ||= Hash [ workbook_doc . xpath ( "//definedName" ) . map do |defined_name |
556
548
# "Sheet1!$C$5"
0 commit comments