aboutsummaryrefslogtreecommitdiffstats
path: root/lib/roo/excelx/comments.rb
blob: 65044a94bb380d7ddcd700f2497821ad7ffc5797 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'roo/excelx/extractor'

module Roo
  class Excelx
    class Comments < Excelx::Extractor
      def comments
        @comments ||= extract_comments
      end

      private

      def extract_comments
        return {} unless doc_exists?

        doc.xpath('//comments/commentList/comment').each_with_object({}) do |comment, hash|
          value = (comment.at_xpath('./text/r/t') || comment.at_xpath('./text/t')).text
          hash[::Roo::Utils.ref_to_key(comment['ref'].to_s)] = value
        end
      end
    end
  end
end
# xl/comments1.xml
#   <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
#   <comments xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
#     <authors>
#       <author />
#     </authors>
#     <commentList>
#       <comment ref="B4" authorId="0">
#         <text>
#           <r>
#             <rPr>
#               <sz val="10" />
#               <rFont val="Arial" />
#               <family val="2" />
#             </rPr>
#             <t>Comment for B4</t>
#           </r>
#         </text>
#       </comment>
#       <comment ref="B5" authorId="0">
#         <text>
#           <r>
#             <rPr>
#             <sz val="10" />
#             <rFont val="Arial" />
#             <family val="2" />
#           </rPr>
#             <t>Comment for B5</t>
#           </r>
#         </text>
#       </comment>
#     </commentList>
#   </comments>