|
| 1 | +module MergeExcel |
| 2 | + class WBook |
| 3 | + attr_reader :sheets, :hash, :format, :filepath, :filename |
| 4 | + |
| 5 | + def initialize(filepath, hash) |
| 6 | + @sheets = [] |
| 7 | + @hash = hash |
| 8 | + @format = detect_format(filepath) # :xls or :xlsx |
| 9 | + @filepath = filepath |
| 10 | + @filename = File.basename(filepath) |
| 11 | + |
| 12 | + book = SpreadsheetParser.open(filepath, @format) |
| 13 | + book.worksheets.each_with_index do |sheet, idx| |
| 14 | + i = 0 |
| 15 | + if @hash[:sheets_idxs]==:all || @hash[:sheets_idxs].include?(idx) |
| 16 | + raise InvalidOptions if !@hash[:header_rows].keys.include?(idx) |
| 17 | + s = @hash[:header_rows][idx] # settings |
| 18 | + sh = Sheet.new(idx, i, sheet.sheet_name, self) |
| 19 | + arr = sheet.row_values(s[:row_idx], s[:from_col_idx], s[:to_col_idx]) |
| 20 | + sh.add_header(arr, @hash[:extra_data][idx]) |
| 21 | + @sheets << sh |
| 22 | + i+=1 |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + |
| 28 | + def sheet(original_idx: ) |
| 29 | + @sheets.find{|s| s.original_idx==original_idx} |
| 30 | + end |
| 31 | + |
| 32 | + |
| 33 | + def import_data(filepath) |
| 34 | + book = SpreadsheetParser.open(filepath, @format) |
| 35 | + filename = File.basename(filepath) |
| 36 | + @hash[:sheets_idxs].each do |idx| |
| 37 | + s = sheet(original_idx: idx) |
| 38 | + sheet_to_read = book.worksheets[idx] |
| 39 | + if row_settings = @hash[:data_rows][idx] # is an hash |
| 40 | + row_idx = row_settings[:first_row_idx] |
| 41 | + loop do |
| 42 | + break unless cells = sheet_to_read.row_values(row_idx, row_settings[:from_col_idx], row_settings[:to_col_idx]) |
| 43 | + s.add_data_row(cells, @hash[:extra_data][idx], filename, book) |
| 44 | + row_idx+=1 |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + |
| 51 | + def export(filepath) |
| 52 | + print "Exporting data..." |
| 53 | + SpreadsheetWriter.create(filepath, @format) do |wb| |
| 54 | + @sheets.each_with_index do |sheet, sheet_idx| |
| 55 | + s = wb.add_worksheet_at(sheet_idx, sheet.name) |
| 56 | + s.add_row_at(0, sheet.header) |
| 57 | + sheet.data_rows.each_with_index do |data_row, row_idx| |
| 58 | + s.add_row_at(row_idx+1, data_row) |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + # if xls? |
| 64 | + # merged_book = Spreadsheet::Workbook.new |
| 65 | + # @sheets.each do |sheet| |
| 66 | + # s = merged_book.create_worksheet name: sheet.name |
| 67 | + # s.row(0).concat sheet.header.to_a |
| 68 | + # sheet.data_rows.each_with_index do |data_row, i| |
| 69 | + # s.row(i+1).concat data_row.to_a |
| 70 | + # end |
| 71 | + # end |
| 72 | + # merged_book.write filename |
| 73 | + # |
| 74 | + # else |
| 75 | + # merged_book = RubyXL::Workbook.new |
| 76 | + # @sheets.each_with_index do |sheet, i| |
| 77 | + # if i==0 |
| 78 | + # s = merged_book.worksheets[0] |
| 79 | + # s.sheet_name = sheet.name |
| 80 | + # else |
| 81 | + # s = merged_book.add_worksheet sheet.name |
| 82 | + # end |
| 83 | + # |
| 84 | + # sheet.header.to_a.each_with_index do |header_cell, idx| |
| 85 | + # s.add_cell(0, idx, header_cell) |
| 86 | + # end |
| 87 | + # sheet.data_rows.each_with_index do |data_row, row_idx| |
| 88 | + # data_row.to_a.each_with_index do |data_cell, col_idx| |
| 89 | + # # puts "#{data_cell} -> #{data_cell.class}" |
| 90 | + # # exit if row_idx>8 |
| 91 | + # if data_cell.is_a? DateTime |
| 92 | + # c = s.add_cell(row_idx+1, col_idx) |
| 93 | + # c.set_number_format('yyyy-mm-dd') |
| 94 | + # c.change_contents(data_cell) |
| 95 | + # else |
| 96 | + # s.add_cell(row_idx+1, col_idx, data_cell) |
| 97 | + # end |
| 98 | + # |
| 99 | + # end |
| 100 | + # end |
| 101 | + # end |
| 102 | + # merged_book.write filename |
| 103 | + # end |
| 104 | + puts "finished!" |
| 105 | + end |
| 106 | + |
| 107 | + private |
| 108 | + def detect_format(filepath) |
| 109 | + case File.extname(filepath) |
| 110 | + when ".xls" |
| 111 | + :xls |
| 112 | + when ".xlsx" |
| 113 | + :xlsx |
| 114 | + else |
| 115 | + raise "Invalid format" |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments