<commit_before>module ImportableAttachments
  module Importers
    module Excel

      require 'iconv'

      # :call-seq:
      # import_csv
      #
      # imports an Excel (tm) file

      def import_excel
        column_names = self.class.spreadsheet_columns
        assoc = self.class.import_into
        import_method = self.class.import_method
        return unless attachment.present?

        stream = attachment.io_stream
        stream_path = if stream.exists?
          stream.path
        else
          stream.queued_for_write[:original].path
        end
        spreadsheet = Roo::Excel.new stream_path

        spreadsheet.default_sheet = spreadsheet.sheets.first
        headers = (1..column_names.length).map { |n| spreadsheet.cell(1, n).try(:downcase) }
        return unless headers == column_names.map(&:downcase)
        self.send(assoc).destroy_all
        2.upto(spreadsheet.last_row) do |line|
          self.send(import_method, *(1..column_names.length).map { |n| spreadsheet.cell(line, n) })
        end
        spreadsheet.remove_tmp
      end

    end
  end
end
<commit_msg>drop deprecated roo API call<commit_after>module ImportableAttachments
  module Importers
    module Excel

      require 'iconv'

      # :call-seq:
      # import_csv
      #
      # imports an Excel (tm) file

      def import_excel
        column_names = self.class.spreadsheet_columns
        assoc = self.class.import_into
        import_method = self.class.import_method
        return unless attachment.present?

        stream = attachment.io_stream
        stream_path = if stream.exists?
          stream.path
        else
          stream.queued_for_write[:original].path
        end
        spreadsheet = Roo::Excel.new stream_path

        spreadsheet.default_sheet = spreadsheet.sheets.first
        headers = (1..column_names.length).map { |n| spreadsheet.cell(1, n).try(:downcase) }
        return unless headers == column_names.map(&:downcase)
        self.send(assoc).destroy_all
        2.upto(spreadsheet.last_row) do |line|
          self.send(import_method, *(1..column_names.length).map { |n| spreadsheet.cell(line, n) })
        end
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.0908675799086758, 'fraction_numerical': 0.0036529680365296802, 'mean_word_length': 2.304675716440422, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '924895', 'n_tokens_mistral': 665, 'n_tokens_neox': 635, 'n_words': 131}