ruby axlsx gem: 不使用添加行将公式分配给 axlsx 工作表
ruby axlsx gem: assign formula to axlsx worksheet without using add-row
根据官方的例子,可以在add_rowhttps://github.com/randym/axlsx/blob/master/examples/example.rb#L355
期间将公式分配给工作表
我的问题是当 table 填满时是否可以将公式分配给列?
下面是向现有单元格添加公式的示例。
pk = Axlsx::Package.new
wb = pk.workbook
sheet = wb.add_worksheet(name: 'Test')
sheet.add_row(['First', 'Second', 'Third'])
sheet.add_row([1, 1, 1])
sheet.add_row([2, 2, 2])
sheet.add_row([3, 3, 0]) # we will be updating the last cell in this row (C4)
sheet.add_row([4, 4, 4])
cell = sheet['C4']
cell.type = :string # it is important to ensure the type of the cell is set before adding the formula
cell.value = '=A3+A4'
pk.serialize("example.xlsx")
下面是一个动态更新单元格公式的例子:
pk = Axlsx::Package.new
wb = pk.workbook
sheet = wb.add_worksheet(name: 'Test')
sheet.add_row(['First', 'Second', 'Third'])
sheet.add_row([1, 1, 1])
sheet.add_row([2, 2, 2])
sheet.add_row([3, 3, 3])
sheet.add_row([4, 4, 4])
cells = sheet["C2:C5"] # select an array of cells
cells.each do |cell|
row_index = cell.row.index + 1
cell.type = :string
cell.value = "=SUM(A#{row_index}:B#{row_index})"
end
pk.serialize("example2.xlsx")
根据官方的例子,可以在add_rowhttps://github.com/randym/axlsx/blob/master/examples/example.rb#L355
期间将公式分配给工作表我的问题是当 table 填满时是否可以将公式分配给列?
下面是向现有单元格添加公式的示例。
pk = Axlsx::Package.new
wb = pk.workbook
sheet = wb.add_worksheet(name: 'Test')
sheet.add_row(['First', 'Second', 'Third'])
sheet.add_row([1, 1, 1])
sheet.add_row([2, 2, 2])
sheet.add_row([3, 3, 0]) # we will be updating the last cell in this row (C4)
sheet.add_row([4, 4, 4])
cell = sheet['C4']
cell.type = :string # it is important to ensure the type of the cell is set before adding the formula
cell.value = '=A3+A4'
pk.serialize("example.xlsx")
下面是一个动态更新单元格公式的例子:
pk = Axlsx::Package.new
wb = pk.workbook
sheet = wb.add_worksheet(name: 'Test')
sheet.add_row(['First', 'Second', 'Third'])
sheet.add_row([1, 1, 1])
sheet.add_row([2, 2, 2])
sheet.add_row([3, 3, 3])
sheet.add_row([4, 4, 4])
cells = sheet["C2:C5"] # select an array of cells
cells.each do |cell|
row_index = cell.row.index + 1
cell.type = :string
cell.value = "=SUM(A#{row_index}:B#{row_index})"
end
pk.serialize("example2.xlsx")