lib/rbbt/tsv/excel.rb in rbbt-util-5.32.25 vs lib/rbbt/tsv/excel.rb in rbbt-util-5.32.26

- old
+ new

@@ -175,20 +175,22 @@ header = true unless header == false sheet ||= "0" workbook = RubyXL::Parser.parse file - if sheet && sheet =~ /^\d+$/ + 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.to_s) do |filename| - sheet = sheet ? workbook[sheet] : workbook.worksheets.first + sheet = sheet_name ? workbook[sheet_name] : workbook.worksheets.first + raise "No sheet #{sheet_name} found" if sheet.nil? + rows = [] sheet.each do |row| next if row.nil? if skip_rows > 0 @@ -215,24 +217,30 @@ text ? Open.read(filename) : TSV.open(filename, options) end end def self.write(tsv, file, options = {}) - sheet = Misc.process_options options, :sheet + sheet, add_sheet = Misc.process_options options, :sheet, :add_sheet fields, rows = TSV._excel_data(tsv, options) - book = RubyXL::Workbook.new - sheet1 = book.worksheets.first - sheet1.sheet_name = sheet if sheet + if Open.exists?(file) && add_sheet + book = RubyXL::Parser.parse file + sheet1 = book.add_worksheet(sheet) + else + book = RubyXL::Workbook.new + sheet1 = book.worksheets.first + sheet1.sheet_name = sheet if sheet + end fields.each_with_index do |e,i| sheet1.add_cell(0, i, e) - end + end if fields rows.each_with_index do |cells,i| + i += 1 if fields cells.each_with_index do |e,j| - sheet1.add_cell(i+1, j, e) + sheet1.add_cell(i, j, e) end end book.write file end