首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails AXSLX sort.cells

Rails AXSLX sort.cells
EN

Stack Overflow用户
提问于 2014-08-04 18:38:06
回答 1查看 1.3K关注 0票数 2

我在Rails应用程序中使用gem 'axlsx_rails'

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

下面是我的代码:

代码语言:javascript
复制
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,我在add_row期间对记录进行了排序:

代码语言:javascript
复制
       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是否可以使用cells.sort??

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-05 20:40:16

rows SimpleTypedList可以通过工作表获得,尽管没有sheet.rows=方法,但可以使用sort_by!对其进行适当的修改:

代码语言:javascript
复制
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函数获得您的总范围:

代码语言:javascript
复制
"=SUM(D2:#{Axlsx::cell_r(3,@costproject.costestimates.length)}"

因为它期望基于零的索引,所以实际长度将计算标题行。22估计会给你"D23“。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25125340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档