lib/worksheet.rb in rxl-0.2.1 vs lib/worksheet.rb in rxl-0.3.0

- old
+ new

@@ -32,10 +32,33 @@ Cell.hash_cell_to_rubyxl_cell(hash_cell, rubyxl_worksheet, row_index, column_index) end end + ################################################ + ### GET RUBYXL WORKSHEET FROM HASHES ### + ################################################ + + def self.hashes_to_hash_worksheet(hashes, order, write_headers: true) + rows = hashes.map do |hash| + order.map { |item| hash[item] } + end + rows.unshift(order.map { |item| "#{item}" }) if write_headers + rows_to_hash_worksheet(rows) + end + + def self.rows_to_hash_worksheet(rows) + rxl_worksheet = {} + rows.count.times do |i| + rows[i].each_with_index do |cell_value, index| + rxl_worksheet["#{column_name(index)}#{i + 1}"] = { value: cell_value } + end + end + rxl_worksheet + end + + ############################## ### SHARED METHODS ### ############################## def self.process_sheet_to_populated_block(hash_worksheet) @@ -85,8 +108,14 @@ raise(%(worksheet value at path ["#{hash_worksheet_name}"] must be a Hash)) end hash_worksheet.each do |hash_cell_key, hash_cell| Cell.validate_hash_cell(hash_cell_key, hash_cell, [hash_worksheet_name]) end + end + + def self.column_name(int) + name = 'A' + int.times { name.succ! } + name end end \ No newline at end of file