我有一个axlsx文件,其中有一些创建行的逻辑。我在那里检查,当满足某个条件时,我想合并创建的最后一个单元格。我怎么能这样做?
我见过如何合并单元格的唯一方法是指定要合并的单元格:
sheet.merge_cells "A2:B2"
但如果我有动态工作表,我该如何合并最后一行的单元格?
答案 0 :(得分:6)
Axlsx merge_cells接受一个字符串,如您的示例或单元格数组。 在这种程度上,您可以使用rows,cols及其单元格集合来查找,并传入要合并的单元格数组。
类似的东西:
sheet.merge_cells sheet.rows.last.cells
供参考,这是有关Worksheet#merge_cell
的文档# Creates merge information for this worksheet.
# Cells can be merged by calling the merge_cells method on a worksheet.
# @example This would merge the three cells C1..E1 #
# worksheet.merge_cells "C1:E1"
# # you can also provide an array of cells to be merged
# worksheet.merge_cells worksheet.rows.first.cells[(2..4)]
# #alternatively you can do it from a single cell
# worksheet["C1"].merge worksheet["E1"]
# @param [Array, string] cells
def merge_cells(cells)
merged_cells.add cells
end
答案 1 :(得分:0)
这是一种通用解决方案
current_row = sheet.rows.count + 1
merge_parameter= "A#{current_row}:F#{current_row}"
sheet.merge_cells(merge_parameter)