Module: SpreadsheetArchitect::ClassMethods
- Defined in:
- lib/spreadsheet_architect.rb
Instance Method Summary collapse
- #sa_get_options(options = {}) ⇒ Object
- #sa_get_row_data(the_columns = [], instance) ⇒ Object
- #sa_str_humanize(str, capitalize = true) ⇒ Object
- #to_csv(opts = {}) ⇒ Object
- #to_ods(opts = {}) ⇒ Object
- #to_xlsx(opts = {}) ⇒ Object
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 (={}) if self.ancestors.include?(ActiveRecord::Base) && !self.respond_to?(:spreadsheet_columns) && ![: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 [:spreadsheet_columns] || self.respond_to?(:spreadsheet_columns) headers = [] columns = [] array = [: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 = ([: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 [:header_style] header_style.merge!([:header_style]) elsif [: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 [:row_style] row_style.merge!([:row_style]) end sheet_name = [:sheet_name] || self.name if [:data] data = [:data].to_a elsif self.ancestors.include?(ActiveRecord::Base) data = where([:where]).order([:order]).to_a else # object must have a to_a method data = self.to_a end types = ([: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={}) = (opts) CSV.generate do |csv| csv << [:headers] if [:headers] [:data].each do |x| csv << sa_get_row_data([: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={}) = (opts) spreadsheet = ODF::Spreadsheet.new spreadsheet.office_style :header_style, family: :cell do if [:header_style] unless opts[:header_style] && opts[:header_style][:bold] == false #uses opts, temporary property :text, 'font-weight': :bold end if [:header_style][:align] property :text, 'align': [:header_style][:align] end if [:header_style][:size] property :text, 'font-size': [:header_style][:size] end if [:header_style][:color] && opts[:header_style] && opts[:header_style][:color] #temporary property :text, 'color': "##{[:header_style][:color]}" end end end spreadsheet.office_style :row_style, family: :cell do if [:row_style] if [:row_style][:bold] property :text, 'font-weight': :bold end if [:row_style][:align] property :text, 'align': [:row_style][:align] end if [:row_style][:size] property :text, 'font-size': [:row_style][:size] end if opts[:row_style] && opts[:row_style][:color] #uses opts, temporary property :text, 'color': "##{[:row_style][:color]}" end end end this_class = self spreadsheet.table [:sheet_name] do if [:headers] row do [:headers].each do |header| cell header, style: :header_style end end end [:data].each do |x| row do this_class.sa_get_row_data([: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={}) = (opts) header_style = {} header_style[:fg_color] = [:header_style].delete(:color) header_style[:bg_color] = [:header_style].delete(:background_color) if header_style[:align] header_style[:alignment] = {} header_style[:alignment][:horizontal] = [:header_style].delete(:align) end header_style[:b] = [:header_style].delete(:bold) header_style[:sz] = [:header_style].delete(:font_size) header_style[:i] = [:header_style].delete(:italic) header_style[:u] = [:header_style].delete(:underline) header_style.delete_if{|x| x.nil?} row_style = {} row_style[:fg_color] = [:row_style].delete(:color) row_style[:bg_color] = [:row_style].delete(:background_color) if row_style[:align] row_style[:alignment] = {} row_style[:alignment][:horizontal] = [:row_style][:align] end row_style[:b] = [:row_style].delete(:bold) row_style[:sz] = [:row_style].delete(:font_size) row_style[:i] = [:row_style].delete(:italic) row_style[:u] = [:row_style].delete(:underline) row_style.delete_if{|x| x.nil?} package = Axlsx::Package.new return package if [:data].empty? package.workbook.add_worksheet(name: [:sheet_name]) do |sheet| if [:headers] sheet.add_row [:headers], style: package.workbook.styles.add_style(header_style) end [:data].each do |x| sheet.add_row sa_get_row_data([:columns], x), style: package.workbook.styles.add_style(row_style), types: [:types] end end return package.to_stream.read end |