使用rails Axlsx合并多行的多个单元格

时间:2018-06-12 15:52:11

标签: ruby ruby-on-rails-5 spreadsheet axlsx

我有一个rails应用程序,我试图以excel格式导出一些数据。在我的Excel工作表中,我想合并动态行的多个单元格。我正在尝试按this回答,但它给了我一个错误。根据该解决方案中给出的答案,我在excel.rb目录中创建了一个config/initializers文件并粘贴了此代码

module Axlsx
  class Worksheet
    def merge_last_row(options={})
       last_row = rows.last.index + 1
       first_col,last_col = options[:columns]
       if first_col && last_col
         merge_cells "#{first_col}#{last_row}:#{last_col}#{last_row}"
       else
         merge_cells sheet.rows.last
       end
       rows.last.style = style if options[:style]
    end
    def fill_columns(column_count,options={})
      row_data = options[:row_data] || []
      (column_count - row.count).times do 
        row_data << nil
      end
      add_row row_data
    end
  end
end

然后在我的report.xls.axlsx文件中,我有以下代码

wb = xlsx_package.workbook
xlsx_package.use_autowidth = true

wb.add_worksheet(name: "gold_sheet") do |sheet|
    @gold_live_console.each do |item|
        section = item.room.section.try(:name)
         time = item.bingning_inserted_at.strftime("%Y-%m-%d - %l:%M %p")
        if section.present?
            sheet.add_row %w(Date)
            sheet.merge_last_row columns:["A","E"]
            sheet.add_row [time]
            sheet.add_row %w(  Section  )
            sheet.add_row [item.room.section.try(:name)]
            sheet.add_row %w(  Gra_Name  )
            sheet.add_row [item.room.gra_employee.try(:full_name)]
            sheet.add_row %w(Room# Credit Beginning_Status PM_Status Notes)
            sheet.add_row [item.room_location, item.room_credit, item.beginning_status]
            sheet.add_row()
        end
    end
end

当我运行它时,它会给我ActionView::Template::Error (undefined method merge_last_row'错误

我想将具有文本日期

的行的A中的所有列合并到E中

我应该如何在axlsx文件中使用上述方法?我是ruby的新手很抱歉,如果我的错误太傻了,但有人可以指出我的错误吗?

0 个答案:

没有答案