168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/rbbt/tsv/excel.rb', line 168
def self.read(file, options = {})
options = Misc.add_defaults options, :sep2 => /[,|]\s?/, :skip_rows => 0
sheet = Misc.process_options options, :sheet
= Misc.process_options options, :header
text = Misc.process_options options, :text
skip_rows = Misc.process_options options, :skip_rows
skip_rows = skip_rows.to_i
= true unless == false
sheet ||= "0"
workbook = RubyXL::Parser.parse file
if sheet && sheet =~ /^\d+$/
sheet = workbook.worksheets.collect{|s| s.sheet_name }[sheet.to_i]
end
sheet_name = sheet
Log.debug "Opening XLSX #{file} sheet #{ sheet_name }"
TmpFile.with_file :extension => Misc.sanitize_filename(sheet_name) do |filename|
sheet = sheet ? workbook[sheet] : workbook.worksheets.first
rows = []
sheet.each do |row|
next if row.nil?
if skip_rows > 0
skip_rows -= 1
next
end
rows << row.cells.collect{|c| c.nil? ? nil : c.value}.collect{|c| String === c ? c.gsub("\n", ' ') : c }
end
num_values = rows.first.length
File.open(filename, 'w') do |f|
if
= rows.shift
f.puts "#" + * "\t"
end
rows.each do |row|
row[num_values-1] ||= nil
f.puts row * "\t"
end
end
text ? Open.read(filename) : TSV.open(filename, options)
end
end
|