Module: SpreadsheetArchitect::ClassMethods

Included in:
SpreadsheetArchitect
Defined in:
lib/spreadsheet_architect.rb

Instance Method Summary collapse

Instance Method Details

#to_axlsx(which = 'sheet', opts = {}) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/spreadsheet_architect.rb', line 269

def to_axlsx(which='sheet', opts={})
  opts = SpreadsheetArchitect::Helpers.get_cell_data(opts, self)
  options = SpreadsheetArchitect::Helpers.get_options(opts, self)

  header_style = {}
  if options[:header_style]
    header_style[:fg_color] = options[:header_style].delete(:color)
    header_style[:bg_color] = options[:header_style].delete(:background_color)
    if header_style[:align]
      header_style[:alignment] = {}
      header_style[:alignment][:horizontal] = options[:header_style].delete(:align)
    end
    header_style[:b] = options[:header_style].delete(:bold)
    header_style[:sz] = options[:header_style].delete(:font_size)
    header_style[:i] = options[:header_style].delete(:italic)
    if options[:header_style][:underline]
      header_style[:u] = options[:header_style].delete(:underline)
    end
    header_style.delete_if{|x| x.nil?}
  end

  row_style = {}
  if options[:row_style]
    row_style[:fg_color] = options[:row_style].delete(:color)
    row_style[:bg_color] = options[:row_style].delete(:background_color)
    if row_style[:align]
      row_style[:alignment] = {}
      row_style[:alignment][:horizontal] = options[:row_style][:align]
    end
    row_style[:b] = options[:row_style].delete(:bold)
    row_style[:sz] = options[:row_style].delete(:font_size)
    row_style[:i] = options[:row_style].delete(:italic)
    if options[:row_style][:underline]
      row_style[:u] = options[:row_style].delete(:underline)
    end
    row_style.delete_if{|x| x.nil?}
  end
  
  package = Axlsx::Package.new

  return package if options[:data].empty?

  the_sheet = package.workbook.add_worksheet(name: options[:sheet_name]) do |sheet|
    if options[:headers]
      sheet.add_row options[:headers], style: package.workbook.styles.add_style(header_style)
    end
    
    options[:data].each do |row_data|
      row_style.merge!(format_code: opts[:row_style][:number_format_code]) if opts[:row_style] && opts[:row_style][:number_format_code]
      sheet.add_row row_data, style: package.workbook.styles.add_style(row_style), types: options[:types]
    end
  end

  if which.to_sym == :sheet
    return the_sheet
  else
    return package
  end
end

#to_csv(opts = {}) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/spreadsheet_architect.rb', line 193

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

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

#to_ods(opts = {}) ⇒ Object



265
266
267
# File 'lib/spreadsheet_architect.rb', line 265

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

#to_rodf_spreadsheet(opts = {}) ⇒ Object



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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/spreadsheet_architect.rb', line 206

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

  spreadsheet = ODF::Spreadsheet.new

  spreadsheet.office_style :header_style, family: :cell do
    if options[:header_style]
      unless opts[:header_style] && opts[:header_style][:bold] == false #uses opts, temporary
        property :text, 'font-weight' => :bold
      end
      if options[:header_style][:align]
        property :text, align: options[:header_style][:align]
      end
      if options[:header_style][:size]
        property :text, 'font-size' => options[:header_style][:size]
      end
      if options[:header_style][:color] && opts[:header_style] && opts[:header_style][:color] #temporary
        property :text, color: "##{options[:header_style][:color]}"
      end
    end
  end
  spreadsheet.office_style :row_style, family: :cell do
    if options[:row_style]
      if options[:row_style][:bold]
        property :text, 'font-weight' => :bold
      end
      if options[:row_style][:align]
        property :text, align: options[:row_style][:align]
      end
      if options[:row_style][:size]
        property :text, 'font-size' => options[:row_style][:size]
      end
      if opts[:row_style] && opts[:row_style][:color] #uses opts, temporary
        property :text, color: "##{options[:row_style][:color]}"
      end
    end
  end

  spreadsheet.table options[:sheet_name] do 
    if options[:headers]
      row do
        options[:headers].each do |header|
          cell header, style: :header_style
        end
      end
    end
    options[:data].each do |row_data|
      row do 
        row_data.each_with_index do |y,i|
          cell y, style: :row_style, type: options[:types][i]
        end
      end
    end
  end

  return spreadsheet
end

#to_xlsx(opts = {}) ⇒ Object



329
330
331
# File 'lib/spreadsheet_architect.rb', line 329

def to_xlsx(opts={})
  return to_axlsx('package', opts).to_stream.read
end