Module: SpreadsheetArchitect::Utils
- Defined in:
- lib/spreadsheet_architect/utils.rb,
lib/spreadsheet_architect/utils/xlsx.rb
Defined Under Namespace
Modules: XLSX
Class Method Summary collapse
- .convert_styles_to_ods(styles = {}) ⇒ Object
- .get_cell_data(options, klass) ⇒ Object
- .get_options(options, klass) ⇒ Object
Class Method Details
.convert_styles_to_ods(styles = {}) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/spreadsheet_architect/utils.rb', line 151 def self.convert_styles_to_ods(styles={}) styles = {} unless styles.is_a?(Hash) styles = stringify_keys(styles) property_styles = {} text_styles = {} text_styles['font-weight'] = styles.delete('bold') ? 'bold' : styles.delete('font-weight') text_styles['font-size'] = styles.delete('font_size') || styles.delete('font-size') text_styles['font-style'] = styles.delete('italic') ? 'italic' : styles.delete('font-style') if styles['underline'] styles.delete('underline') text_styles['text-underline-style'] = 'solid' text_styles['text-underline-type'] = 'single' end if styles['align'] text_styles['align'] = true end if styles['color'].respond_to?(:sub) && !styles['color'].empty? text_styles['color'] = "##{styles.delete('color').sub('#','')}" end text_styles.delete_if{|_,v| v.nil?} property_styles['text'] = text_styles cell_styles = {} styles['background_color'] ||= styles.delete('background-color') if styles['background_color'].respond_to?(:sub) && !styles['background_color'].empty? cell_styles['background-color'] = "##{styles.delete('background_color').sub('#','')}" end cell_styles.delete_if{|_,v| v.nil?} property_styles['cell'] = cell_styles return property_styles end |
.get_cell_data(options, klass) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/spreadsheet_architect/utils.rb', line 3 def self.get_cell_data(, klass) if [:data] && [:instances] raise SpreadsheetArchitect::Exceptions::MultipleDataSourcesError elsif [:data] data = [:data] end if ![:data] && [:headers] == true headers = [] needs_headers = true elsif [:headers].is_a?(Array) headers = [:headers] else headers = [] end if [:column_types] column_types = [:column_types] else column_types = [] needs_column_types = true end if !data if ![:instances] if is_ar_model?(klass) [:instances] = klass.where(nil).to_a # triggers the relation call, not sure how this works but it does else raise SpreadsheetArchitect::Exceptions::NoDataError end end if [:spreadsheet_columns] if [String, Symbol].any?{|x| [:spreadsheet_columns].is_a?(x)} cols_method_name = [:spreadsheet_columns] if klass != SpreadsheetArchitect && !klass.instance_methods.include?(cols_method_name) raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(klass, cols_method_name) end end else if klass != SpreadsheetArchitect && !klass.instance_methods.include?(:spreadsheet_columns) if is_ar_model?(klass) the_column_names = klass.column_names headers = the_column_names.map{|x| str_titleize(x)} if needs_headers columns = the_column_names.map{|x| x.to_sym} else raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(klass) end end end data = [] [:instances].each do |instance| if columns data.push columns.map{|col| col.is_a?(Symbol) ? instance.send(col) : col} else row_data = [] if [:spreadsheet_columns] if cols_method_name instance_cols = instance.send(cols_method_name) else instance_cols = [:spreadsheet_columns].call(instance) end else if klass == SpreadsheetArchitect && !instance.respond_to?(:spreadsheet_columns) raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(instance.class) else instance_cols = instance.spreadsheet_columns end end instance_cols.each_with_index do |x,i| if x.is_a?(Array) headers.push(x[0].to_s) if needs_headers row_data.push(x[1].is_a?(Symbol) ? instance.send(x[1]) : x[1]) if needs_column_types column_types[i] = x[2] end else headers.push(str_titleize(x.to_s)) if needs_headers row_data.push(x.is_a?(Symbol) ? instance.send(x) : x) end end data.push row_data needs_headers = false needs_column_types = false end end end if headers && !headers[0].is_a?(Array) headers = [headers] end if column_types.compact.empty? column_types = nil end return .merge(headers: headers, data: data, column_types: column_types) end |
.get_options(options, klass) ⇒ Object
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 |
# File 'lib/spreadsheet_architect/utils.rb', line 109 def self.(, klass) verify_option_types() if [:freeze] && [:freeze_headers] raise SpreadsheetArchitect::Exceptions::ArgumentError.new('Cannot use both :freeze and :freeze_headers options at the same time') end if defined?(klass::SPREADSHEET_OPTIONS) if klass::SPREADSHEET_OPTIONS.is_a?(Hash) = SpreadsheetArchitect..merge( klass::SPREADSHEET_OPTIONS.merge() ) else raise SpreadsheetArchitect::Exceptions::OptionTypeError.new("#{klass}::SPREADSHEET_OPTIONS constant") end else = SpreadsheetArchitect..merge() end if ![:headers] [:header_style] = false end if ![:sheet_name] if klass == SpreadsheetArchitect [:sheet_name] = 'Sheet1' else [:sheet_name] = klass.name if [:sheet_name].respond_to?(:pluralize) [:sheet_name] = [:sheet_name].pluralize end end end if [:freeze] && [:freeze].is_a?(Hash) && ![:freeze][:rows] raise SpreadsheetArchitect::Exceptions::ArgumentError.new('Must provide a :rows key when passing a hash to the :freeze option') end return end |