96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/rbbt/tsv/excel.rb', line 96
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 = Spreadsheet.open Open.open(file)
if sheet && sheet.to_s =~ /^\d+$/
sheet = workbook.worksheets.collect{|s| s.name }[sheet.to_i]
end
sheet_name = sheet
Log.debug "Opening LSX #{file} sheet #{ sheet_name }"
TmpFile.with_file :extension => Misc.sanitize_filename(sheet_name.to_s) do |filename|
sheet = workbook.worksheet sheet
rows = []
sheet.each do |row|
if skip_rows > 0
skip_rows -= 1
next
end
rows << row.values_at(0..(row.size - 1)).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|
values = row.collect{|c| c.respond_to?(:value) ? c.value : c }
values[num_values-1] ||= nil
f.puts values * "\t"
end
end
text ? Open.read(filename) : TSV.open(filename, options)
end
end
|