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 |
# File 'lib/spreadsheet_architect.rb', line 22 def (={}) if self.ancestors.include?(ActiveRecord::Base) && !self.respond_to?(:spreadsheet_columns) && ![:spreadsheet_columns] headers = self.column_names.map{|x| x.humanize} columns = self.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) if [:header_style] header_style = [:header_style] elsif [:header_style] == false header_style = false elsif [:row_style] header_style = [:row_style] else header_style = {bg_color: "AAAAAA", fg_color: "FFFFFF", alignment: { horizontal: :center }, bold: true} end row_style = [:row_style] 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
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={}) = (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
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={}) = (opts) spreadsheet = ODF::Spreadsheet.new spreadsheet.office_style :header_style, family: :cell do if [:header_style] if [:header_style][:bold] property :text, 'font-weight': :bold property :text, 'align': :center end if [:header_style][:fg_color] && opts[:header_style] && opts[:header_style][:fg_color] #temporary property :text, 'color': "##{[:header_style][:fg_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][:fg_color] property :text, 'color': "##{[:header_style][:fg_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 if [: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 if [: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={}) = (opts) package = opts[: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]) if [:header_style]) end [:data].each do |x| sheet.add_row sa_get_row_data([:columns], x), style: (package.workbook.styles.add_style([:row_style]) if [:row_style]), types: [:types] end end return package.to_stream.read end |