Sha256: 9004b4a0ff8fa219a0f3b5cd35c40d53eebcdc9ead70dfaa9385fbcf446e8343

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require_relative 'excel_comments/excel_comment'
# All Comments of single XLSX
module OoxmlParser
  class ExcelComments
    attr_accessor :authors, :comments

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

    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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ooxml_parser-0.1.2 lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/excel_comments.rb
ooxml_parser-0.1.1 lib/ooxml_parser/xlsx_parser/xlsx_data/view_model/workbook/worksheet/excel_comments.rb