Module: TSV::XLSX
- Defined in:
- lib/rbbt/tsv/excel.rb
Class Method Summary collapse
Class Method Details
.read(file, options = {}) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/rbbt/tsv/excel.rb', line 151 def self.read(file, = {}) = Misc.add_defaults , :sep2 => /[,|]\s?/ sheet = Misc. , :sheet header = Misc. , :header header = true unless header == false TmpFile.with_file do |filename| workbook = RubyXL::Parser.parse file sheet = sheet ? workbook[sheet] : workbook.worksheets.first rows = [] sheet.each do |row| next if row.nil? 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 header header = rows.shift f.puts "#" + header * "\t" end rows.each do |row| row[num_values-1] ||= nil f.puts row * "\t" end end TSV.open(filename, ) end end |
.write(tsv, file, options = {}) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rbbt/tsv/excel.rb', line 185 def self.write(tsv, file, = {}) sheet = Misc. , :sheet fields, rows = TSV._excel_data(tsv, ) book = RubyXL::Workbook.new sheet1 = book.worksheets.first sheet1.sheet_name = sheet if sheet fields.each_with_index do |e,i| sheet1.add_cell(0, i, e) end rows.each_with_index do |cells,i| cells.each_with_index do |e,j| sheet1.add_cell(i+1, j, e) end end book.write file end |