Class: OoxmlParser::ExcelComments

Inherits:
Object
  • Object
show all
Defined in:
lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/excel_comments.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authors = [], comments = []) ⇒ ExcelComments

Returns a new instance of ExcelComments.



7
8
9
10
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/excel_comments.rb', line 7

def initialize(authors = [], comments = [])
  @authors = authors
  @comments = comments
end

Instance Attribute Details

#authorsObject

Returns the value of attribute authors.



5
6
7
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/excel_comments.rb', line 5

def authors
  @authors
end

#commentsObject

Returns the value of attribute comments.



5
6
7
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/excel_comments.rb', line 5

def comments
  @comments
end

Class Method Details

.parse(file) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/excel_comments.rb', line 12

def self.parse(file)
  doc = Nokogiri::XML(File.open(file))
  excel_comments = ExcelComments.new
  comments_node = doc.search('//xmlns:comments').first
  comments_node.xpath('*').each do |comments_node_child|
    case comments_node_child.name
    when 'authors'
      comments_node_child.xpath('xmlns:author').each do |author_node|
        excel_comments.authors << author_node.text
      end
    when 'commentList'
      comments_node_child.xpath('xmlns:comment').each do |comment_node|
        excel_comments.comments << ExcelComment.parse(comment_node)
      end
    end
  end
  excel_comments
end

.parse_file(file_name, path_to_folder) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/excel_comments.rb', line 31

def self.parse_file(file_name, path_to_folder)
  path_to_comments_xml = ''
  return nil unless File.exist?(path_to_folder + "xl/worksheets/_rels/#{file_name}.rels")
  relationships = XmlSimple.xml_in(File.open(path_to_folder + "xl/worksheets/_rels/#{file_name}.rels"))
  if relationships['Relationship']
    relationships['Relationship'].each do |relationship|
      path_to_comments_xml = path_to_folder + 'xl/' + relationship['Target'].gsub('..', '') if File.basename(relationship['Target']).include?('comment')
    end
  end
  ExcelComments.parse(path_to_comments_xml) unless path_to_comments_xml == ''
end