Module: SpreadsheetArchitect::ClassMethods

Included in:
SpreadsheetArchitect
Defined in:
lib/spreadsheet_architect/class_methods/csv.rb,
lib/spreadsheet_architect/class_methods/ods.rb,
lib/spreadsheet_architect/class_methods/xlsx.rb

Instance Method Summary collapse

Instance Method Details

#to_axlsx_package(opts = {}, package = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/spreadsheet_architect/class_methods/xlsx.rb', line 13

def to_axlsx_package(opts={}, package=nil)
  opts = SpreadsheetArchitect::Utils.get_options(opts, self)
  options = SpreadsheetArchitect::Utils.get_cell_data(opts, self)

  if options[:column_types] && !(options[:column_types].compact.collect(&:to_sym) - SpreadsheetArchitect::XLSX_COLUMN_TYPES).empty?
    raise SpreadsheetArchitect::Exceptions::ArgumentError.new("Invalid column type. Valid XLSX values are #{SpreadsheetArchitect::XLSX_COLUMN_TYPES}")
  end

  header_style = SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(options[:header_style])
  row_style = SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(options[:row_style])

  if package.nil?
    package = Axlsx::Package.new
  end

  row_index = -1

  package.workbook.add_worksheet(name: options[:sheet_name]) do |sheet|
    max_row_length = options[:data].empty? ? 0 : options[:data].max_by{|x| x.length}.length

    if options[:headers]
      header_style_index = package.workbook.styles.add_style(header_style)

      options[:headers].each do |header_row|
        row_index += 1

        missing = max_row_length - header_row.count
        if missing > 0
          missing.times do
            header_row.push(nil)
          end
        end

        sheet.add_row header_row, style: header_style_index

        if options[:conditional_row_styles]
          conditional_styles_for_row = SpreadsheetArchitect::Utils::XLSX.conditional_styles_for_row(options[:conditional_row_styles], row_index, header_row)
          
          unless conditional_styles_for_row.empty?
            sheet.add_style(
              "#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES.first}#{row_index+1}:#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES[max_row_length-1]}#{row_index+1}", 
              SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(conditional_styles_for_row)
            )
          end
        end
      end
    end

    if options[:data].empty?
      break
    end

    row_style_index = package.workbook.styles.add_style(row_style)

    default_date_style_index = nil
    default_time_style_index = nil

    options[:data].each do |row_data|
      row_index += 1

      missing = max_row_length - row_data.count
      if missing > 0
        missing.times do
          row_data.push(nil)
        end
      end

      types = []
      styles = []
      row_data.each_with_index do |x,i|
        if (x.respond_to?(:empty) ? x.empty? : x.nil?)
          types[i] = nil
          styles[i] = row_style_index
        else
          if options[:column_types]
            types[i] = options[:column_types][i]
          end

          types[i] ||= SpreadsheetArchitect::Utils::XLSX.get_type(x)

          if [:date, :time].include?(types[i])
            if types[i] == :date
              default_date_style_index ||= package.workbook.styles.add_style(row_style.merge({format_code: 'yyyy-mm-dd'}))
              styles[i] = default_date_style_index
            else
              default_time_style_index ||= package.workbook.styles.add_style(row_style.merge({format_code: 'yyyy-mm-dd h:mm AM/PM'}))
              styles[i] = default_time_style_index
            end
          else
            styles[i] = row_style_index
          end
        end
      end

      sheet.add_row row_data, style: styles, types: types

      if options[:conditional_row_styles]
        options[:conditional_row_styles] = SpreadsheetArchitect::Utils.hash_array_symbolize_keys(options[:conditional_row_styles])

        conditional_styles_for_row = SpreadsheetArchitect::Utils::XLSX.conditional_styles_for_row(options[:conditional_row_styles], row_index, row_data)
        
        unless conditional_styles_for_row.empty?
          sheet.add_style(
            "#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES.first}#{row_index+1}:#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES[max_row_length-1]}#{row_index+1}", 
            SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(conditional_styles_for_row)
          )
        end
      end
    end

    if options[:column_widths]
      sheet.column_widths(*options[:column_widths])
    end

    if options[:borders] || options[:column_styles] || options[:range_styles] || options[:merges]
      num_rows = options[:data].count + (options[:headers] ? options[:headers].count : 0)
    end

    if options[:borders]
      options[:borders] = SpreadsheetArchitect::Utils.hash_array_symbolize_keys(options[:borders])

      options[:borders].each do |x|
        if x[:range].is_a?(Hash)
          x[:range] = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str(x[:range], max_row_length, num_rows)
        else
          SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows)
        end

        sheet.add_border x[:range], (x[:border_styles] || x[:styles])
      end
    end

    if options[:column_styles]
      options[:column_styles] = SpreadsheetArchitect::Utils.hash_array_symbolize_keys(options[:column_styles])

      options[:column_styles].each do |x|
        start_row = (options[:headers] ? options[:headers].count : 0) + 1

        x[:styles] = SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(x[:styles])

        add_column_style = ->(col){
          SpreadsheetArchitect::Utils::XLSX.verify_column(col, max_row_length)

          range_str = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str({rows: (start_row..num_rows), columns: col}, max_row_length, num_rows)
          sheet.add_style range_str, x[:styles]

          if x[:include_header] && start_row > 1
            range_str = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str({rows: (1..start_row-1), columns: col}, max_row_length, num_rows)
            sheet.add_style(range_str, x[:styles])
          end
        }

        case x[:columns]
        when Array, Range
          x[:columns].each do |col|
            add_column_style.call(col)
          end
        when Integer, String
          add_column_style.call(x[:columns])
        else
          SpreadsheetArchitect::Utils::XLSX.verify_column(x[:columns], max_row_length)
        end
      end
    end

    if options[:range_styles]
      options[:range_styles] = SpreadsheetArchitect::Utils.hash_array_symbolize_keys(options[:range_styles])

      options[:range_styles].each do |x|
        styles = SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(x[:styles])

        if x[:range].is_a?(Hash)
          x[:range] = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str(x[:range], max_row_length, num_rows)
        else
          SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows)
        end

        sheet.add_style x[:range], styles
      end
    end

    if options[:merges]
      options[:merges] = SpreadsheetArchitect::Utils.hash_array_symbolize_keys(options[:merges])

      options[:merges].each do |x|
        if x[:range].is_a?(Hash)
          x[:range] = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str(x[:range], max_row_length, num_rows)
        else
          SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows)
        end

        sheet.merge_cells x[:range]
      end
    end

    if options[:freeze_headers]
      sheet.sheet_view.pane do |pane|
        pane.state = :frozen
        pane.y_split = options[:headers].count
      end

    elsif options[:freeze]
      options[:freeze] = SpreadsheetArchitect::Utils.symbolize_keys(options[:freeze])

      sheet.sheet_view.pane do |pane|
        pane.state = :frozen

        ### Currently not working
        #if options[:freeze][:active_pane]
        #  Axlsx.validate_pane_type(options[:freeze][:active_pane])
        #  pane.active_pane = options[:freeze][:active_pane]
        #else
        #  pane.active_pane = :bottom_right
        #end

        if !options[:freeze][:rows]
          raise SpreadsheetArchitect::Exceptions::ArgumentError.new("The :rows key must be specified in the :freeze option hash")
        elsif options[:freeze][:rows].is_a?(Range)
          pane.y_split = options[:freeze][:rows].count
        else
          pane.y_split = 1
        end

        if options[:freeze][:columns] && options[:freeze][:columns] != :all
          if options[:freeze][:columns].is_a?(Range)
            pane.x_split = options[:freeze][:columns].count
          else
            pane.x_split = 1
          end
        end
      end
    end
  end

  return package
end

#to_csv(opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/spreadsheet_architect/class_methods/csv.rb', line 6

def to_csv(opts={})
  opts = SpreadsheetArchitect::Utils.get_options(opts, self)
  options = SpreadsheetArchitect::Utils.get_cell_data(opts, self)

  CSV.generate do |csv|
    if options[:headers]
      options[:headers].each do |header_row|
        csv << header_row
      end
    end
    
    options[:data].each do |row_data|
      csv << row_data
    end
  end
end

#to_ods(opts = {}) ⇒ Object



6
7
8
# File 'lib/spreadsheet_architect/class_methods/ods.rb', line 6

def to_ods(opts={})
  return to_rodf_spreadsheet(opts).bytes
end

#to_rodf_spreadsheet(opts = {}, spreadsheet = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/spreadsheet_architect/class_methods/ods.rb', line 10

def to_rodf_spreadsheet(opts={}, spreadsheet=nil)
  opts = SpreadsheetArchitect::Utils.get_options(opts, self)
  options = SpreadsheetArchitect::Utils.get_cell_data(opts, self)

  if options[:column_types] && !(options[:column_types].compact.collect(&:to_sym) - SpreadsheetArchitect::ODS_COLUMN_TYPES).empty?
    raise SpreadsheetArchitect::Exceptions::ArgumentError.new("Invalid column type. Valid ODS values are #{SpreadsheetArchitect::ODS_COLUMN_TYPES}")
  end

  if !spreadsheet
    spreadsheet = RODF::Spreadsheet.new
  end

  spreadsheet.office_style :header_style, family: :cell do
    if options[:header_style]
      SpreadsheetArchitect::Utils.convert_styles_to_ods(options[:header_style]).each do |prop, styles|
        styles.each do |k,v|
          property prop.to_sym, k => v
        end
      end
    end
  end

  spreadsheet.office_style :row_style, family: :cell do
    if options[:row_style]
      SpreadsheetArchitect::Utils.convert_styles_to_ods(options[:row_style]).each do |prop, styles|
        styles.each do |k,v|
          property prop.to_sym, k => v
        end
      end
    end
  end

  spreadsheet.table options[:sheet_name] do 
    if options[:headers]
      options[:headers].each do |header_row|
        row do
          header_row.each_with_index do |header, i|
            cell header, style: :header_style
          end
        end
      end
    end

    options[:data].each_with_index do |row_data, index|
      row do 
        row_data.each_with_index do |val, i|
          if options[:column_types]
            type = options[:column_types][i]
          end

          if (type && [:date, :time].include?(type)) || val.respond_to?(:strftime)
            type = :string
            val = val.to_s 
          end

          cell val, style: :row_style, type: type
        end
      end
    end
  end

  return spreadsheet
end

#to_xlsx(opts = {}) ⇒ Object



9
10
11
# File 'lib/spreadsheet_architect/class_methods/xlsx.rb', line 9

def to_xlsx(opts={})
  return to_axlsx_package(opts).to_stream.read
end