Module: SpreadsheetArchitect::ClassMethods

Defined in:
lib/spreadsheet_architect.rb

Instance Method Summary collapse

Instance Method Details

#to_csv(opts = {}) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/spreadsheet_architect.rb', line 152

def to_csv(opts={})
  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



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
# File 'lib/spreadsheet_architect.rb', line 164

def to_ods(opts={})
  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.bytes
end

#to_xlsx(opts = {}) ⇒ Object



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
264
265
266
267
268
269
270
# File 'lib/spreadsheet_architect.rb', line 222

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

  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?}

  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?}
  
  package = Axlsx::Package.new

  return package if options[:data].empty?

  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|
      sheet.add_row row_data, style: package.workbook.styles.add_style(row_style), types: options[:types]
    end
  end

  return package.to_stream.read
end