Sha256: 2ed5b4898e8c855c5d1c78e2527568fddb2b0a83c8db75e58b72dad5cb6a270f

Contents?: true

Size: 1.52 KB

Versions: 34

Compression:

Stored size: 1.52 KB

Contents

class Hash
  def write_excel(filename)
    generate_excel(filename) do |workbook|
      each do |sheet_name, sheet_data|
        workbook.add_worksheet(name: sheet_name) do |sheet|
          if sheet_data.is_a?(Hash)
            fields = sheet_data[:fields].map(&:to_s)
            sheet.add_row(fields, types: [:string] * fields.size)
            sheet_data[:data].each do |row|
              sheet.add_row(row.slice(*fields).values.map(&:to_s), types: [:string] * fields.size)
            end
          end

          if sheet_data.is_a?(Array)
            if sheet_data.size > 0 && sheet_data.first.is_a?(ActiveModel::Base)
              fields = sheet_data.first.attributes.keys
              sheet.add_row(fields, types: [:string] * fields.size)
              sheet_data.each do |row|
                sheet.add_row(row.slice(*fields).values.map(&:to_s), types: [:string] * fields.size)
              end
            end

            if sheet_data.size > 0 && sheet_data.first.is_a?(Hash)
              fields = sheet_data.first.keys
              sheet.add_row(fields, types: [:string] * fields.size)
              sheet_data.each do |row|
                sheet.add_row(row.slice(*fields).values.map(&:to_s), types: [:string] * fields.size)
              end
            end

            if sheet_data.size > 0 && sheet_data.first.is_a?(Array)
              sheet_data.each do |row|
                sheet.add_row(row.map(&:to_s), types: [:string] * fields.size)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 3 rubygems

Version Path
crudboy-0.1.5 lib/crudboy/ext/hash.rb
crudboy-0.1.4 lib/crudboy/ext/hash.rb
arql-0.3.21 lib/arql/ext/hash.rb
arql-0.3.20 lib/arql/ext/hash.rb
arql-0.3.19 lib/arql/ext/hash.rb
pry-helper-0.1.3 lib/pry-helper/ext/hash.rb
pry-helper-0.1.2 lib/pry-helper/ext/hash.rb
arql-0.3.18 lib/arql/ext/hash.rb
arql-0.3.17 lib/arql/ext/hash.rb
arql-0.3.16 lib/arql/ext/hash.rb
arql-0.3.15 lib/arql/ext/hash.rb
arql-0.3.14 lib/arql/ext/hash.rb
pry-helper-0.1.1 lib/pry-helper/ext/hash.rb
pry-helper-0.1.0 lib/pry-helper/ext/hash.rb
arql-0.3.13 lib/arql/ext/hash.rb
arql-0.3.12 lib/arql/ext/hash.rb
arql-0.3.11 lib/arql/ext/hash.rb
arql-0.3.10 lib/arql/ext/hash.rb
arql-0.3.9 lib/arql/ext/hash.rb
arql-0.3.8 lib/arql/ext/hash.rb