Ruby on rails Rails AXSLX sort.cells

Ruby on rails Rails AXSLX sort.cells,ruby-on-rails,axlsx,Ruby On Rails,Axlsx,我正在rails应用程序中使用gem'axlsx\u rails' 我想将单元格排序(A列的第2行到第23行)作为创建选项卡的最后一步 以下是我的代码: wb.add_worksheet(:name => "Cost") do |sheet| sheet.page_setup.set :orientation => :portrait sheet.add_row ['Seq', 'Category', 'Remarks', 'Amount', 'Notes'], :style

我正在rails应用程序中使用
gem'axlsx\u rails'

我想将单元格排序(A列的第2行到第23行)作为创建选项卡的最后一步

以下是我的代码:

wb.add_worksheet(:name => "Cost") do |sheet|
  sheet.page_setup.set :orientation => :portrait
  sheet.add_row ['Seq', 'Category', 'Remarks', 'Amount', 'Notes'], :style => [header_cell, header_cell, header_cell, header_cell, header_cell]
    @costproject.costestimates.each do |costestimate|
      sheet.add_row [costestimate.costcat.position, costestimate.costcat.category_name, costestimate.costcat.categorydesc, number_with_precision(costestimate.amount, :precision => 2), costestimate.notes], :style=> [intgr,nil,nil,money]
    end
  sheet.add_row [nil, 'TOTAL', nil, "=SUM(D2:D23)"]
  sheet.column_widths 5, 35, 25, 25
  cells.sort ?????
end
我想这是可以做到的。是这样吗? 如果是,如何进行?用什么替换
cells.sort?????

谢谢你的帮助

更新1:

多亏了emcanes,我在添加行中对记录进行了排序:

       sheet.add_row ['Seq', 'Category', 'Remarks', 'Amount', 'Notes'], :style => [header_cell, header_cell, header_cell, header_cell, header_cell]
    @costproject.costestimates.includes(:costcat).order("costcats.position").each do |ce|
      sheet.add_row [ce.costcat.position, ce.costcat.category_name,  ce.costcat.categorydesc, number_with_precision(ce.amount, :precision => 2), ce.notes], :style=> [intgr,border,border,money,border]
    end

我仍然想知道AXSLX是否可以使用
单元格。sort

工作表中提供了
SimpleTypedList,尽管没有
工作表。rows=
方法,但您可以使用
排序依据要就地修改它,请执行以下操作:

wb.add_worksheet(:name => "Cost") do |sheet|
  # your other code
  first_row = sheet.rows.first
  last_row  = sheet.rows.last
  # get a total position higher than all others
  tpos = sheet.rows.map {|row| row.cells[0].value.to_i}.max + 1
  sheet.rows.sort_by! do |row|
    (row == first_row ? -1 : (row == last_row ? tpos : row.cells[0].value.to_i))
  end
  # now do styling
end
一行没有内部索引,所以不能询问它是什么索引。如果您询问,它只会找出它在行列表中的位置。因此,您必须提前保存标题/总行数

我还没有测试过这个样式,但我完全不确定它是否会遵循这个排序,因为这会弄乱Axlsx的内部结构。它可能需要在之后发生,除非它是通用的

顺便说一句,使用
Axlsx::cell\r
函数获取总范围:

"=SUM(D2:#{Axlsx::cell_r(3,@costproject.costestimates.length)}"

因为它需要从零开始的索引,所以实际长度将计算标题行。22估计值将为您提供“D23”。

为什么不在插入之前对数据进行排序?排序字段来自插入的引用表/字段。我试着把它分类,但是,也许我需要回去,让它那样工作。我试试看。我仍然想知道
cells.sort
是否有效。谢谢