<commit_before>require 'rubygems'
require 'nokogiri'
require 'zip/zip' #rubyzip
require File.expand_path(File.join(File.dirname(__FILE__),'Hash'))

module RubyXL

  class Parser
    attr_reader :data_only, :num_sheets

    # converts cell string (such as "AA1") to matrix indices
    def Parser.convert_to_index(cell_string)
      index = Array.new(2)
      index[0]=-1
      index[1]=-1
      if(cell_string =~ /^([A-Z]+)(\d+)$/)
        one = $1.to_s
        row = Integer($2) - 1 #-1 for 0 indexing
        col = 0
        i = 0
        one = one.reverse #because of 26^i calculation
        one.each_byte do |c|
          int_val = c - 64 #converts A to 1
          col += int_val * 26**(i)
          i=i+1
        end
        col -= 1 #zer0 index
        index[0] = row
        index[1] = col
      end
      return index
    end


    # data_only allows only the sheet data to be parsed, so as to speed up parsing
    def Parser.parse(file_path, data_only=false)
      @data_only = data_only
      files = Parser.decompress(file_path)
      wb = Parser.fill_workbook(file_path, files)

      if(files['sharedString'] != nil)
        wb.num_strings = Integer(files['sharedString'].css('sst').attribute('count').value())
        wb.size = Integer(files['sharedString'].css('sst').attribute('uniqueCount').value())

        shared_strings = files['sharedString'].css('si t').children
        wb.shared_strings = {}
        shared_strings.each_with_index do |string,i|
          wb.shared_strings[i] = string.to_s
          wb.shared_strings[string.to_s] = i
        end
      end

      unless @data_only
        styles = files['styles'].css('cellXfs xf')
        style_hash = Hash.xml_node_to_hash(files['styles'].root)
        fill_styles(wb,style_hash)
<commit_msg>Fixed bug with parsing shared strings with styles<commit_after>require 'rubygems'
require 'nokogiri'
require 'zip/zip' #rubyzip
require File.expand_path(File.join(File.dirname(__FILE__),'Hash'))

module RubyXL

  class Parser
    attr_reader :data_only, :num_sheets

    # converts cell string (such as "AA1") to matrix indices
    def Parser.convert_to_index(cell_string)
      index = Array.new(2)
      index[0]=-1
      index[1]=-1
      if(cell_string =~ /^([A-Z]+)(\d+)$/)
        one = $1.to_s
        row = Integer($2) - 1 #-1 for 0 indexing
        col = 0
        i = 0
        one = one.reverse #because of 26^i calculation
        one.each_byte do |c|
          int_val = c - 64 #converts A to 1
          col += int_val * 26**(i)
          i=i+1
        end
        col -= 1 #zer0 index
        index[0] = row
        index[1] = col
      end
      return index
    end


    # data_only allows only the sheet data to be parsed, so as to speed up parsing
    def Parser.parse(file_path, data_only=false)
      @data_only = data_only
      files = Parser.decompress(file_path)
      wb = Parser.fill_workbook(file_path, files)

      if(files['sharedString'] != nil)
        wb.num_strings = Integer(files['sharedString'].css('sst').attribute('count').value())
        wb.size = Integer(files['sharedString'].css('sst').attribute('uniqueCount').value())

        files['sharedString'].css('si').each do |node|
          unless node.css('r').empty?
            text = node.css('r t').children.to_a.join
            node.children.remove
            node << "<t xml:space=\"preserve\">#{text}</t>"
          end
        end
        

        shared_strings = files['sharedString'].css('si t').children
        wb.shared_strings = {}
        shared_strings.each_with_index do |string,i|
          wb.shared_strings[i] = string.to_s
          wb.shared_strings[string.to_s] = i
        end
      end

      unless @data_only
        styles = files['styles'].css('cellXfs xf')
        style_hash = Hash.xml_node_to_hash(files['styles'].root)
        fill_styles(wb,style_hash)

{'fraction_non_alphanumeric': 0.12869745718733783, 'fraction_numerical': 0.012973533990659055, 'mean_word_length': 2.3845478489903424, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6666242', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1236, 'n_words': 283}