Module: Excel2CSV

Defined in:
lib/excel2csv.rb,
lib/excel2csv/info.rb,
lib/excel2csv/version.rb

Defined Under Namespace

Classes: Info

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.clean_options(options) ⇒ Object



112
113
114
115
116
# File 'lib/excel2csv.rb', line 112

def clean_options options
  options.dup.delete_if do |key, value|
    [:working_folder, :java_options, :preview, :sheet, :path, :rows, :info].include?(key)
  end
end

.convert(path, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/excel2csv.rb', line 26

def convert(path, options = {})
  info = options[:info]
  if info && Dir.exists?(info.working_folder)
    return block_given? ? yield(info) : info
  end
  begin
    info = create_cvs_files(path, options)
    if block_given?
      yield info
    else
      info
    end
  ensure
    info.clean if block_given? && info
  end
end

.create_cvs_files(path, options) ⇒ Object



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
# File 'lib/excel2csv.rb', line 58

def create_cvs_files(path, options)
  tmp_dir = Dir.mktmpdir
  working_folder = options[:working_folder] || tmp_dir
  limit = options[:rows]
  path = File.expand_path(path)
  if path =~ /\.csv$/
    json = {
      'generatePreviews' => !!limit,
      'perSheetRowLimitForPreviews' => limit,
      'sourceFile' => path.to_s,
      'targetDir' => working_folder
    }
    total_rows = 0
    preview_rows = []
    opts = clean_options(options)
    sheetJson = {}
    
    full_output = "1-#{total_rows}.csv"
    # Transcode file to utf-8, count total and gen preview
    CSV.open("#{working_folder}/#{full_output}", "wb") do |csv|
      CSV.foreach(path, opts) do |row| 
        if limit && total_rows <= limit
          preview_rows << row
        end
        total_rows += 1
        csv << row
      end
    end
    sheetJson['fullOutput'] = full_output
    sheetJson['rowCount'] = total_rows
    
    if limit
      preview_output = "1-#{limit}-of-#{total_rows}-preview.csv"
      sheetJson['previewOutput'] = preview_output
      CSV.open("#{working_folder}/#{preview_output}", "wb") do |csv|
        preview_rows.each {|row| csv << row}
      end
    end
    json['sheets'] = [sheetJson]
    File.open "#{working_folder}/info.json", "wb" do |f|
      f.write(JSON.generate(json))
    end
  else
    java_options = options[:java_options] || "-Dfile.encoding=utf8 -Xms512m -Xmx512m -XX:MaxPermSize=256m"
    rows_limit = limit ? "-r #{limit}" : ""
    jar_path = File.join(File.dirname(__FILE__), "excel2csv.jar")
    `java #{java_options} -jar #{jar_path} #{rows_limit} "#{path}" #{working_folder}`
  end
  
  Info.read(working_folder)
end

.foreach(path, options = {}, &block) ⇒ Object



10
11
12
13
14
# File 'lib/excel2csv.rb', line 10

def foreach(path, options = {}, &block) 
  convert(path, options) do |info|
    CSV.foreach(path_to_sheet(info, options), clean_options(options), &block)
  end 
end

.path_to_sheet(info, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/excel2csv.rb', line 45

def path_to_sheet(info, options = {})
  options.delete(:encoding) # all previews are in utf-8
  if options[:preview]
    collection = info.previews
  else
    collection = info.sheets
  end
  index = (idx = options[:sheet]) ? idx : 0
  collection[index][:path]
end

.read(path, options = {}) ⇒ Object



18
19
20
21
22
# File 'lib/excel2csv.rb', line 18

def read(path, options = {})
  convert(path, options) do |info|
    CSV.read(path_to_sheet(info, options), clean_options(options))
  end
end