RubyXL: How to add multiple cells of data to a worksheet?

2.3k Views Asked by At

How can I add an entire array of cells to a row? I have something like this->

w = RubyXL::Workbook.new
w.add_worksheet("Test")
test_sheet = w["Test"]
test_sheet.add_cell(0, 0, "abc") #this adds abc to row 0 and column 0

arr = ["hello","again","hi","there"]

Is there anything like

test_sheet.add_row(a)

where I can all all contents of array arr to one row in the Test worksheet with each element of a in a separate cell?

4

There are 4 best solutions below

0
On

You can simply join the array using a newline and then apply wrap text if appropriate

@worksheet.add_cell(row, column, array_value.join("\n"))
@worksheet.sheet_data[row][column].change_text_wrap(true)
0
On

Example to add multiple rows data from my code base instead of add_row keep adding cells to the row hope this might help, Although there are many other ways to write in excel but legacy rails app still use this library :

workbook = RubyXL::Workbook.new
## First workbook
worksheet = workbook[0]
## Rename first workbook
worksheet.sheet_name = 'delayed_jobs_data'
header = ['JOB ID', 'PARENT ID', 'STATUS', 'PRIORITY', 'ATTEMPTS', 'JOB TYPE', 'HOST', 'USER', 'PROCESS', 'QUEUE', 'ITEM NUMBER', 'RUN AT', 'LOCKED AT', 'CREATED AT', 'COMPLETED AT']
header.each_with_index do |h, idx|
  worksheet.add_cell(0, idx, h)
end
worksheet.change_column_width(0, 10)
worksheet.change_column_width(1, 10)
worksheet.change_column_width(2, 10)
worksheet.change_column_width(3, 10)
worksheet.change_column_width(4, 10)
worksheet.change_column_width(5, 30)
worksheet.change_column_width(6, 20)
worksheet.change_column_width(7, 10)
worksheet.change_column_width(8, 30)
worksheet.change_column_width(9, 20)
worksheet.change_column_width(10, 10)
worksheet.change_column_width(11, 20)
worksheet.change_column_width(12, 20)
worksheet.change_column_width(13, 20)
worksheet.change_column_width(14, 20)
worksheet.change_row_fill(0, '808080')

## Now write the data
# check if the from data and to date present else archive the data of delayed jobs of the Time.now - 30 days current month
from = params[:from_date]
to = params[:to_date]
if !from.blank? && !to.blank?
  data = Model.where("created_at between ? and ?", from, to)
  file_name = "archiving_persited_jobs_from_#{from}_to_#{to}"

end

dest_file_path = Rails.root.join("public", "persisted_delayed_jobs", "#{file_name}.xlsm")

## After the data os written delete the data from database
data.each_with_index do |row, index|
  new_index = index += 1
  row_data = [row.id,
              (row.parent.id unless row.parent.blank?),
              row.status, row.priority, row.attempts, row.job_type, row.locked_by, row.user.full_name,
              (row.parent.job_type unless row.parent.blank?),
              row.queue, row&.item&.item_number,
              (row.run_at.to_datetime.in_time_zone(current_user.time_zone).strftime('%m/%d/%Y %H:%M') rescue row.run_at.strftime('%m/%d/%Y %H:%M')),
              (row.locked_at.to_datetime.in_time_zone(current_user.time_zone).strftime('%m/%d/%Y %H:%M') rescue row.locked_at.strftime('%m/%d/%Y %H:%M')),
              (row.created_at.to_datetime.in_time_zone(current_user.time_zone).strftime('%m/%d/%Y %H:%M') rescue row.created_at.strftime('%m/%d/%Y %H:%M')),
              (row&.completed_at&.to_datetime&.in_time_zone(current_user.time_zone)&.strftime('%m/%d/%Y %H:%M') rescue row&.completed_at&.strftime('%m/%d/%Y %H:%M')),
              (row&.failed_at&.to_datetime&.in_time_zone(current_user.time_zone)&.strftime('%m/%d/%Y %H:%M') rescue row&.failed_at&.strftime('%m/%d/%Y %H:%M')),
              (row.completed_at - row.created_at unless !row.created_at.blank? & !row.completed_at)
  ]
  # worksheet.insert_row(row_data)
  worksheet.add_cell(new_index, 0, row_data[0])
  worksheet.add_cell(new_index, 1, row_data[1])
  worksheet.add_cell(new_index, 2, row_data[2])
  worksheet.add_cell(new_index, 3, row_data[3])
  worksheet.add_cell(new_index, 4, row_data[4])
  worksheet.add_cell(new_index, 5, row_data[5])
  worksheet.add_cell(new_index, 6, row_data[6])
  worksheet.add_cell(new_index, 7, row_data[7])
  worksheet.add_cell(new_index, 8, row_data[8])
  worksheet.add_cell(new_index, 9, row_data[9])
  worksheet.add_cell(new_index, 10, row_data[10])
  worksheet.add_cell(new_index, 11, row_data[11])
  worksheet.add_cell(new_index, 12, row_data[12])
  worksheet.add_cell(new_index, 13, row_data[13])
  worksheet.add_cell(new_index, 14, row_data[14])
end

workbook.write(dest_file_path)
1
On

You can use:

worksheet.insert_row(1)

Reference: https://www.rubydoc.info/gems/rubyXL/3.3.21#label-Insert+Row

0
On

Looks like there is no official way to do it. We use this code in our project:

row_index = sheet.sheet_data.rows.size
arr.each_with_index do |value, index|
  sheet.add_cell(row_index, index, value)
end