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
# 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]
    headers = self.column_names.map{|x| x.humanize}
    columns = self.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)

  if options[:header_style]
    header_style = options[:header_style]
  elsif options[:header_style] == false
    header_style = false
  elsif options[:row_style]
    header_style = options[:row_style]
  else
    header_style = {bg_color: "AAAAAA", fg_color: "FFFFFF", alignment: { horizontal: :center }, bold: true}
  end

  row_style = options[:row_style]

  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



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

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
  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_+/, '').sub(/_id\z/, '').gsub(/[_\.]/,' ').sub(' rescue nil','')
  if capitalize
    str = str.gsub(/(\A|\ )\w/){|x| x.upcase}
  end
  str
end

#to_csv(opts = {}) ⇒ Object



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

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



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

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]
      if options[:header_style][:bold]
        property :text, 'font-weight': :bold
        property :text, 'align': :center
      end
      if options[:header_style][:fg_color] && opts[:header_style] && opts[:header_style][:fg_color] #temporary
        property :text, 'color': "##{options[:header_style][:fg_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][:fg_color]
        property :text, 'color': "##{options[:header_style][:fg_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 if options[: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 if options[:row_style])
        end
      end
    end
  end

  return spreadsheet.bytes
end

#to_xlsx(opts = {}) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/spreadsheet_architect.rb', line 150

def to_xlsx(opts={})
  options = sa_get_options(opts)
  
  package = opts[: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(options[:header_style]) if options[:header_style])
    end
    
    options[:data].each do |x|
      sheet.add_row sa_get_row_data(options[:columns], x), style: (package.workbook.styles.add_style(options[:row_style]) if options[:row_style]), types: options[:types]
    end
  end

  return package.to_stream.read
end