Class: ActiveAdmin::Axlsx::Builder
- Inherits:
-
CSVBuilder
- Object
- CSVBuilder
- ActiveAdmin::Axlsx::Builder
- Includes:
- MethodOrProcHelper
- Defined in:
- lib/active_admin/axlsx/builder.rb
Overview
XlsxBuilder extends CSVBuilder adding in xlsx specific options
Usage example
xlsx_builder = XlsxBuilder.new
xlsx_builder.column :id
xlsx_builder.column('Name') { |resource| resource.full_name }
xlsx_builder = XlsxBuilder.new :shared_strings => true
xlsx_builder.column :id
xlsx_builder = XlsxBuiler.new :header_style => { :bg_color => '00', :fg_color => 'FF', :sz => 14, :alignment => { :horizontal => :center } }
xlsx_buider.i18n_scope [:active_record, :models, :posts]
Defined Under Namespace
Classes: Column
Constant Summary collapse
- @@default_header_style =
{ :bg_color => '00', :fg_color => 'FF', :sz => 12, :alignment => { :horizontal => :center } }
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#header_style ⇒ Hash
This has can be used to override the default header style for your sheet.
-
#i18n_scope ⇒ Object
This is the I18n scope that will be used when looking up your colum names in the current I18n locale.
-
#shared_strings ⇒ Object
when this is set to true the xlsx file will be generated with shared strings and will inter-operate with Numbers for Mac This is true by default, but you can set it to false to minimize the generation time.
Class Method Summary collapse
-
.default_for_resource(resource) ⇒ Object
Return a default XlsxBuilder for a resource The XlsxBuilder columns will be id, follwed by this resource’s content columns The default header_style is applied.
Instance Method Summary collapse
-
#column(name, &block) ⇒ Object
Add a column.
-
#header_row ⇒ Array
tranform column names into array of localized strings.
-
#initialize(options = {}, &block) ⇒ Builder
constructor
shared strings or not when parsing out the package.
-
#package ⇒ Object
the Axlsx::Package.
-
#serialize(collection) ⇒ Axlsx::Package
Serializes the collection provided.
Constructor Details
#initialize(options = {}, &block) ⇒ Builder
shared strings are an optional part of the ECMA-376 spec,and
shared strings or not when parsing out the package. are only required when you need to support Numbers.
64 65 66 67 68 69 70 |
# File 'lib/active_admin/axlsx/builder.rb', line 64 def initialize(={}, &block) super @header_style = .delete(:header_style) || @@default_header_style @i18n_scope = .delete(:i18n_scope) @shared_strings = .delete(:shared_strings) || true instance_eval &block if block_given? end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
33 34 35 |
# File 'lib/active_admin/axlsx/builder.rb', line 33 def columns @columns end |
#header_style ⇒ Hash
This has can be used to override the default header style for your sheet. create and apply style.
45 46 47 |
# File 'lib/active_admin/axlsx/builder.rb', line 45 def header_style @header_style end |
#i18n_scope ⇒ Object
If you do not set this, the column name will be titleized.
This is the I18n scope that will be used when looking up your colum names in the current I18n locale. If you set it to [:active_admin, :resources, :posts] the serializer will render the value at active_admin.resources.posts.title in the current translations
53 54 55 |
# File 'lib/active_admin/axlsx/builder.rb', line 53 def i18n_scope @i18n_scope end |
#shared_strings ⇒ Object
when this is set to true the xlsx file will be generated with shared strings and will inter-operate with Numbers for Mac This is true by default, but you can set it to false to minimize the generation time.
38 39 40 |
# File 'lib/active_admin/axlsx/builder.rb', line 38 def shared_strings @shared_strings end |
Class Method Details
.default_for_resource(resource) ⇒ Object
Return a default XlsxBuilder for a resource The XlsxBuilder columns will be id, follwed by this resource’s content columns The default header_style is applied.
27 28 29 30 31 |
# File 'lib/active_admin/axlsx/builder.rb', line 27 def self.default_for_resource(resource) xlsx_builder = super xlsx_builder.header_style = @@default_header_style xlsx_builder end |
Instance Method Details
#column(name, &block) ⇒ Object
Add a column
97 98 99 |
# File 'lib/active_admin/axlsx/builder.rb', line 97 def column(name, &block) @columns << Column.new(name, block) end |
#header_row ⇒ Array
tranform column names into array of localized strings
92 93 94 |
# File 'lib/active_admin/axlsx/builder.rb', line 92 def header_row columns.map { |column| column.localized_name(i18n_scope) } end |
#package ⇒ Object
the Axlsx::Package
73 74 75 |
# File 'lib/active_admin/axlsx/builder.rb', line 73 def package @package ||= ::Axlsx::Package.new(:use_shared_strings => shared_strings) end |
#serialize(collection) ⇒ Axlsx::Package
Serializes the collection provided
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/active_admin/axlsx/builder.rb', line 79 def serialize(collection) header_style_id = package.workbook.styles.add_style header_style package.workbook.add_worksheet do |sheet| sheet.add_row header_row, :style => header_style_id collection.each do |resource| sheet.add_row columns.map { |column| call_method_or_proc_on resource, column.data } end end package end |