Class: Axlsx::Worksheet

Inherits:
Object
  • Object
show all
Defined in:
lib/barkest_core/extensions/axlsx_extenstions.rb

Overview

The Worksheet class is used to manage the Excel file in memory.

Instance Method Summary collapse

Instance Method Details

#add_combined_row(row_data, keys = [ :value, :style, :type ]) ⇒ Object

Adds a row to the worksheet with combined data.

Currently we support specifying the values, styles, and types using this method.

The row_data value should be an array of arrays. Each subarray represents a value in the row with up to three values specifying the value, style, and type. Value is the only item required.

[['Value 1', :bold, :string], ['Value 2'], ['Value 3', nil, :string]]

In fact, if a subarray is replaced by a value, it is treated the same as an array only containing that value.

[['Value 1', :bold, :string], 'Value 2', ['Value 3', nil, :string]]

The keys parameter defines the location of the data elements within the sub arrays. The default would be [ :value, :style, :type ]. If your array happens to have additional data or data arranged in a different format, you can set this to anything you like to get the method to process your data appropriately.

keys = [ :ignored, :value, :ignored, :ignored, :style, :ignored, :type ]

Styles can be specified as a symbol to use the predefined styles, or as a style object you created.

Raises:

  • (ArgumentError)


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/barkest_core/extensions/axlsx_extenstions.rb', line 135

def add_combined_row(row_data, keys = [ :value, :style, :type ])
  val_index = keys.index(:value) || keys.index('value')
  style_index = keys.index(:style) || keys.index('style')
  type_index = keys.index(:type) || keys.index('type')

  raise ArgumentError.new('Missing :value key') unless val_index
  values = row_data.map{|v| v.is_a?(Array) ? v[val_index] : v }
  styles = style_index ? row_data.map{ |v| v.is_a?(Array) ? v[style_index] : nil } : []
  types = type_index ? row_data.map{ |v| v.is_a?(Array) ? v[type_index] : nil } : []

  # allows specifying the style as just a symbol.
  styles.each_with_index do |style,index|
    if style.is_a?(String) || style.is_a?(Symbol)
      styles[index] = workbook.predefined_styles[style.to_sym]
    end
  end

  add_row values, style: styles, types: types
end