Module: SpreadsheetArchitect::ClassMethods

Defined in:
lib/spreadsheet_architect.rb

Instance Method Summary collapse

Instance Method Details

#sa_get_options(options = {}) ⇒ Object



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

def sa_get_options(options={})
  if self.ancestors.include?(ActiveRecord::Base) && !self.respond_to?(:spreadsheet_columns) && !options[:spreadsheet_columns]
    the_column_names = (self.column_names - ["id","created_at","updated_at","deleted_at"])
    headers = the_column_names.map{|x| sa_str_humanize(x)}
    columns = the_column_names.map{|x| x.to_s}
  elsif options[:spreadsheet_columns] || self.respond_to?(:spreadsheet_columns)
    headers = []
    columns = []

    array = options[:spreadsheet_columns] || self.spreadsheet_columns || []
    array.each do |x|
      if x.is_a?(Array)
        headers.push x[0].to_s
        columns.push x[1].to_s
      else
        headers.push sa_str_humanize(x.to_s)
        columns.push x.to_s
      end
    end
  else
    headers = []
    columns = []
  end

  headers = (options[:headers] == false ? false : headers)

  header_style = {background_color: "AAAAAA", color: "FFFFFF", align: :center, bold: false, font_name: 'Arial', font_size: 10, italic: false, underline: false}
  
  if options[:header_style]
    header_style.merge!(options[:header_style])
  elsif options[:header_style] == false
    header_style = false
  end

  row_style = {background_color: nil, color: "000000", align: :left, bold: false, font_name: 'Arial', font_size: 10, italic: false, underline: false}
  if options[:row_style]
    row_style.merge!(options[:row_style])
  end

  sheet_name = options[:sheet_name] || self.name
  
  if options[:data]
    data = options[:data].to_a
  elsif self.ancestors.include?(ActiveRecord::Base)
    data = where(options[:where]).order(options[:order]).to_a
  else
    # object must have a to_a method 
    data = self.to_a
  end

  types = (options[:types] || []).flatten

  return {headers: headers, columns: columns, header_style: header_style, row_style: row_style, types: types, sheet_name: sheet_name, data: data}
end

#sa_get_row_data(the_columns = [], instance) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/spreadsheet_architect.rb', line 77

def sa_get_row_data(the_columns=[], instance)
  row_data = []
  the_columns.each do |col|
    col.split('.').each_with_index do |x,i| 
      if i == 0
        col = instance.instance_eval(x)
      else
        col = col.instance_eval(x)
      end
    end
    row_data.push col.to_s
  end
  return row_data
end

#sa_str_humanize(str, capitalize = true) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/spreadsheet_architect.rb', line 14

def sa_str_humanize(str, capitalize = true)
  str = str.sub(/\A_+/, '').gsub(/[_\.]/,' ').sub(' rescue nil','')
  if capitalize
    str = str.gsub(/(\A|\ )\w/){|x| x.upcase}
  end
  str
end

#to_csv(opts = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/spreadsheet_architect.rb', line 92

def to_csv(opts={})
  options = sa_get_options(opts)

  CSV.generate do |csv|
    csv << options[:headers] if options[:headers]
    
    options[:data].each do |x|
      csv << sa_get_row_data(options[:columns], x)
    end
  end
end

#to_ods(opts = {}) ⇒ Object



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

def to_ods(opts={})
  options = sa_get_options(opts)

  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

  this_class = self
  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 |x|
      row do 
        this_class.sa_get_row_data(options[:columns], x).each do |y|
          cell y, style: :row_style
        end
      end
    end
  end

  return spreadsheet.bytes
end

#to_xlsx(opts = {}) ⇒ Object



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

def to_xlsx(opts={})
  options = sa_get_options(opts)

  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)
  header_style[:u] = options[:header_style].delete(:underline)
  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)
  row_style[:u] = options[:row_style].delete(:underline)
  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 |x|
      sheet.add_row sa_get_row_data(options[:columns], x), style: package.workbook.styles.add_style(row_style), types: options[:types]
    end
  end

  return package.to_stream.read
end