4

我有一个 axlsx 文件,其中包含一些创建行的逻辑。我在那里进行了检查,当满足某个条件时,我想合并最后创建的单元格。我怎样才能做到这一点?

我见过如何合并单元格的唯一方法是指定要合并的单元格,如下所示:

sheet.merge_cells "A2:B2"

但是如果我有一个动态工作表,我如何合并创建的最后一行的单元格?

4

2 回答 2

7

Axlsx merge_cells 接受一个字符串,如您的示例所示,或者一个单元格数组。在这种情况下,您可以使用行、列及其单元格集合来查找并传入要合并的单元格数组。

就像是:

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
于 2013-06-24T21:01:09.773 回答
1

这是一种通用解决方案

 current_row = sheet.rows.count + 1
 merge_parameter= "A#{current_row}:F#{current_row}"
 sheet.merge_cells(merge_parameter)
于 2020-03-17T16:42:17.507 回答