5

如何将整个单元格数组添加到一行?我有这样的东西->

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"]

有没有类似的

test_sheet.add_row(a)

我可以在哪里将数组 arr 的所有内容放在测试工作表中的一行中,并将 a 的每个元素放在一个单独的单元格中?

4

3 回答 3

1

看起来没有官方的方法可以做到这一点。我们在项目中使用此代码:

row_index = sheet.sheet_data.rows.size
arr.each_with_index do |value, index|
  sheet.add_cell(row_index, index, value)
end
于 2020-08-05T16:49:59.730 回答
0

从我的代码库中添加多行数据而不是 add_row 的示例继续将单元格添加到行中希望这可能会有所帮助,尽管还有许多其他方法可以在 excel 中编写,但旧版 rails 应用程序仍然使用这个库:

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)
于 2021-11-03T09:45:05.647 回答
0

您可以使用:

worksheet.insert_row(1)

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

于 2019-06-03T21:32:32.200 回答