Sha256: 6019e691d6af3d6193fe99dbf2438ecb6d349bcd42a834b392628dab844e07d5

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module ExportTo
  module Exporter
    class Xlsx < Struct.new(:rows)

      def export
        rows.each! do |columns, model, x|
          worksheet.set_row(x, height, nil) if x > 0
          worksheet.write_row(x, columns)
        end

        workbook.read_string
      end

      protected

      def workbook
        @workbook ||= FastExcel.open(constant_memory: true)
      end

      def worksheet
        @worksheet ||= begin
          ws = workbook.add_worksheet(options.fetch(:worksheet) { "Default" })

          # 表身樣式
          column_options.each_with_index do |column_options, i|
            next if column_options.blank?

            width = column_options.fetch(:width) { FastExcel::DEF_COL_WIDTH }
            format = column_options.fetch(:format) { {} }

            ws.set_column(i, i, width, workbook.add_format(format))
          end

          # 設定表頭樣式
          ws.set_row(0, height, head_format)

          ws
        end
      end

      def head_format
        workbook.add_format(bold: true, bg_color: "#e5dbad", align: { h: :center })
      end

      def options
        @options ||= (rows.class.options || {})
      end

      def worksheet_name
        options.fetch(:worksheet) { "Default" }
      end

      def height
        options.fetch(:height) { 20 }
      end

      def column_options
        @column_options ||= rows.class.column_options
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
export_to-1.1.0 lib/export_to/exporter/xlsx.rb