Class: RubyXL::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyXL/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Parser

keep_tempfiles_on_error prevents temp files to be erased if exception was thrown during parsing. :data_only allows only the sheet data to be parsed, so as to speed up parsing. However, using this option will result in date-formatted cells being interpreted as numbers.



16
17
18
19
20
# File 'lib/rubyXL/parser.rb', line 16

def initialize(opts = {})
  @data_only = opts.is_a?(TrueClass) || opts[:data_only]
  @skip_filename_check = opts[:skip_filename_check]
  @delete_tempfiles_on_error = !opts[:keep_tempfiles_on_error]
end

Class Method Details

.parse(file_path, opts = {}) ⇒ Object



9
10
11
# File 'lib/rubyXL/parser.rb', line 9

def self.parse(file_path, opts = {})
  self.new(opts).parse(file_path)
end

Instance Method Details

#data_onlyObject



22
23
24
25
# File 'lib/rubyXL/parser.rb', line 22

def data_only
  @data_only = true
  self
end

#parse(xl_file_path) ⇒ Object



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rubyXL/parser.rb', line 27

def parse(xl_file_path)
  raise 'Not .xlsx or .xlsm excel file' unless @skip_filename_check ||
                                          %w{.xlsx .xlsm}.include?(File.extname(xl_file_path))

  dir_path = File.join(File.dirname(xl_file_path), Dir::Tmpname.make_tmpname(['rubyXL', '.tmp'], nil))
  parse_success = false

  begin
    ::Zip::File.open(xl_file_path) { |zip_file|
      zip_file.each { |f|
        fpath = File.join(dir_path, f.name)
        FileUtils.mkdir_p(File.dirname(fpath))
        zip_file.extract(f, fpath) unless File.exist?(fpath)
      }
    }

    wb = RubyXL::Workbook.parse_file(dir_path)
    wb.filepath = xl_file_path

    wb.content_types = RubyXL::ContentTypes.parse_file(dir_path)
    wb.relationship_container = RubyXL::WorkbookRelationships.parse_file(dir_path)
    wb.root_relationship_container = RubyXL::RootRelationships.parse_file(dir_path)
    wb.shared_strings_container = RubyXL::SharedStringsTable.parse_file(dir_path)

    # We must always load the stylesheet because it tells us which values are actually dates/times.
    wb.stylesheet = RubyXL::Stylesheet.parse_file(dir_path)

    unless @data_only
      wb.media.load_dir(dir_path)
      wb.external_links.load_dir(dir_path)
      wb.external_links_rels.load_dir(dir_path)
      wb.drawings.load_dir(dir_path)
      wb.drawings_rels.load_dir(dir_path)
      wb.charts.load_dir(dir_path)
      wb.chart_rels.load_dir(dir_path)
      wb.printer_settings.load_dir(dir_path)
      wb.worksheet_rels.load_dir(dir_path)
      wb.chartsheet_rels.load_dir(dir_path)
      wb.macros.load_file(dir_path, 'vbaProject.bin')
      wb.thumbnail.load_file(dir_path, 'thumbnail.jpeg')

      wb.theme = RubyXL::Theme.parse_file(dir_path)
      wb.core_properties = RubyXL::CoreProperties.parse_file(dir_path)
      wb.document_properties = RubyXL::DocumentProperties.parse_file(dir_path)
      wb.calculation_chain = RubyXL::CalculationChain.parse_file(dir_path)
    end

    #fills out count information for each font, fill, and border
    wb.cell_xfs.each { |style|
      id = style.font_id
      wb.fonts[id].count += 1 #unless id.nil?

      id = style.fill_id
      wb.fills[id].count += 1 #unless id.nil?

      id = style.border_id
      wb.borders[id].count += 1 #unless id.nil?
    }

    wb.sheets.each_with_index { |sheet, i|
      sheet_rel = wb.relationship_container.find_by_rid(sheet.r_id)

      # Making sure that the file will be automatically closed immediately after it has been read
      File.open(File.join(dir_path, 'xl', sheet_rel.target)) { |sheet_file|
        case File::basename(sheet_rel.type)
        when 'worksheet' then
          sheet_obj = RubyXL::Worksheet.parse(sheet_file)
          sheet_obj.sheet_data.rows.each { |r|
            next if r.nil?
            r.worksheet = sheet
            r.cells.each { |c| c.worksheet = sheet_obj unless c.nil? }
          }
        when 'chartsheet' then
          sheet_obj = RubyXL::Chartsheet.parse(sheet_file)
        end

        sheet_obj.workbook = wb
        sheet_obj.sheet_name = sheet.name
        sheet_obj.sheet_id = sheet.sheet_id
        sheet_obj.state = sheet.state

        wb.worksheets[i] = sheet_obj
      }
    }

    parse_success = true
  ensure
    FileUtils.remove_entry_secure(dir_path) if parse_success || @delete_tempfiles_on_error
  end

  return wb
end