[英]RubyXL: How to add multiple cells of data to a worksheet?
如何將整個單元格數組添加到一行? 我有這樣的東西->
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 的每個元素放在一個單獨的單元格中?
看起來沒有官方的方法來做到這一點。 我們在我們的項目中使用此代碼:
row_index = sheet.sheet_data.rows.size
arr.each_with_index do |value, index|
sheet.add_cell(row_index, index, value)
end
從我的代碼庫添加多行數據而不是 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)
您可以簡單地使用換行符加入數組,然后在適當的情況下應用換行文本
@worksheet.add_cell(row, column, array_value.join("\n"))
@worksheet.sheet_data[row][column].change_text_wrap(true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.