Module: Docx::Utils

Defined in:
lib/docx/utils.rb

Overview

Module containing some useful methods to help using this gem

Class Method Summary collapse

Class Method Details

.build_table_config_from_array(values, options = {}) ⇒ Object

Build a table config object from an array of hashes



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/docx/utils.rb', line 7

def self.build_table_config_from_array(values, options = {})
  options[:table_width] ||= 670
  options[:font_family] ||= 'Arial'
  options[:font_size] ||= 12

  return nil unless values&.any?

  properties = values.flat_map(&:keys).uniq
  average_column_width = 100.0 / properties.count
  {
    columns: properties.map do |p|
      {
        name: p,
        description: I18n.t(p, scope: 'docx_builder.tables.columns', default: p.to_s),
        width: average_column_width
      }
    end,
    rows: values
  }.merge(options)
end