Module: SpreadsheetArchitect::Utils::XLSX
- Defined in:
- lib/spreadsheet_architect/utils/xlsx.rb
Class Method Summary collapse
- .conditional_styles_for_row(conditional_row_styles, row_index, row_data) ⇒ Object
- .convert_styles_to_axlsx(styles = {}) ⇒ Object
- .get_type(value, type = nil) ⇒ Object
- .range_hash_to_str(hash, num_columns, num_rows, use_zero_based_row_index: false) ⇒ Object
- .verify_column(col, num_columns) ⇒ Object
- .verify_range(range, num_rows) ⇒ Object
Class Method Details
.conditional_styles_for_row(conditional_row_styles, row_index, row_data) ⇒ Object
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 |
# File 'lib/spreadsheet_architect/utils/xlsx.rb', line 181 def self.conditional_styles_for_row(conditional_row_styles, row_index, row_data) merged_conditional_styles = {} conditional_row_styles.each do |x| if x[:if] && x[:unless] raise SpreadsheetArchitect::Exceptions::ArgumentError.new('Cannot pass both :if and :unless within the same :conditional_row_styles entry') elsif !x[:if] && !x[:unless] raise SpreadsheetArchitect::Exceptions::ArgumentError.new('Must pass either :if or :unless within the each :conditional_row_styles entry') elsif !x[:styles] raise SpreadsheetArchitect::Exceptions::ArgumentError.new('Must pass the :styles option within a :conditional_row_styles entry') end conditions_met = (x[:if] || x[:unless]).call(row_data, row_index) if x[:unless] conditions_met = !conditions_met end if conditions_met merged_conditional_styles.merge!(x[:styles]) end end return merged_conditional_styles end |
.convert_styles_to_axlsx(styles = {}) ⇒ Object
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 |
# File 'lib/spreadsheet_architect/utils/xlsx.rb', line 34 def self.convert_styles_to_axlsx(styles={}) if [nil, false, true].include?(styles) return {} end styles = SpreadsheetArchitect::Utils.symbolize_keys(styles) ### BOOLEAN VALUES if styles[:b].nil? && styles.has_key?(:bold) styles[:b] = !!styles.delete(:bold) end if styles[:i].nil? && styles.has_key?(:italic) styles[:i] = !!styles.delete(:italic) end if styles[:u].nil? && styles.has_key?(:underline) styles[:u] = !!styles.delete(:underline) end ### OTHER VALUES if styles[:sz].nil? && !styles[:font_size].nil? styles[:sz] = styles.delete(:font_size) end if styles[:fg_color].nil? && styles[:color] && styles[:color].respond_to?(:sub) && !styles[:color].empty? styles[:fg_color] = styles.delete(:color).sub('#','') end if styles[:bg_color].nil? && styles[:background_color] && styles[:background_color].respond_to?(:sub) && !styles[:background_color].empty? styles[:bg_color] = styles.delete(:background_color).sub('#','') end if styles[:alignment].nil? && [:align, :valign, :wrap_text].any?{|k| styles.has_key?(k) } if styles[:align].is_a?(Hash) styles[:alignment] = { horizontal: styles[:align][:horizontal], vertical: styles[:align][:vertical], wrap_text: styles[:align][:wrap_text] } else styles[:alignment] = {horizontal: styles.delete(:align), vertical: styles.delete(:valign), wrap_text: styles.delete(:wrap_text) }.compact end end ### COMMENT SEEMS WRONG, TO BE RECONFIRMED ### If `:u` is false instead of nil, it may be incorrectly rendered as true in Excel #if styles[:u] == false # styles[:u] = nil #end ### ENSURE CLEANUP OF ALL ALIAS KEYS [:bold, :font_size, :italic, :underline, :align, :valign, :wrap_text, :color, :background_color].each do |k| styles.delete(k) end return styles end |
.get_type(value, type = nil) ⇒ Object
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 |
# File 'lib/spreadsheet_architect/utils/xlsx.rb', line 5 def self.get_type(value, type=nil) if type && !type.empty? case type when :hyperlink return :string end return type unless (type.respond_to?(:empty?) ? type.empty? : type.nil?) end if value.is_a?(Numeric) if value.is_a?(Float) || value.is_a?(BigDecimal) type = :float else type = :integer end elsif value.respond_to?(:strftime) if value.is_a?(DateTime) || value.is_a?(Time) type = :time else type = :date end else type = :string end return type end |
.range_hash_to_str(hash, num_columns, num_rows, use_zero_based_row_index: false) ⇒ Object
93 94 95 96 97 98 99 100 101 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 149 150 151 |
# File 'lib/spreadsheet_architect/utils/xlsx.rb', line 93 def self.range_hash_to_str(hash, num_columns, num_rows, use_zero_based_row_index: false) if !hash.has_key?(:rows) && !hash.has_key?(:columns) raise SpreadsheetArchitect::Exceptions::InvalidRangeError.new(:missing_range_keys, hash) end case hash[:columns] when Integer start_col = end_col = COL_NAMES[hash[:columns]] when String start_col = hash[:columns].first end_col = hash[:columns].last when Range start_col = hash[:columns].first unless start_col.is_a?(String) start_col = COL_NAMES[start_col] end end_col = hash[:columns].last unless end_col.is_a?(String) end_col = COL_NAMES[end_col] end when :all, nil start_col = 'A' end_col = COL_NAMES[num_columns-1] else raise SpreadsheetArchitect::Exceptions::InvalidRangeOptionError.new(:columns, hash) end case hash[:rows] when Integer start_row = end_row = hash[:rows] if use_zero_based_row_index start_row += 1 end_row += 1 end when Range start_row = hash[:rows].first end_row = hash[:rows].last if use_zero_based_row_index start_row += 1 end_row += 1 end when :all, nil start_row = 1 end_row = num_rows else raise SpreadsheetArchitect::Exceptions::InvalidRangeOptionError.new(:rows, hash) end range_str = "#{start_col}#{start_row}:#{end_col}#{end_row}" unless hash[:columns] == :all && hash[:rows] == :all verify_range(range_str, num_rows) end return range_str end |
.verify_column(col, num_columns) ⇒ Object
175 176 177 178 179 |
# File 'lib/spreadsheet_architect/utils/xlsx.rb', line 175 def self.verify_column(col, num_columns) unless (col.is_a?(String) && COL_NAMES.include?(col)) || (col.is_a?(Integer) && col >= 0 && col < num_columns) raise SpreadsheetArchitect::Exceptions::InvalidColumnError.new(col) end end |
.verify_range(range, num_rows) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/spreadsheet_architect/utils/xlsx.rb', line 153 def self.verify_range(range, num_rows) if range.is_a?(String) if range.include?(':') front, back = range.split(':') start_col, start_row = front.scan(/\d+|\D+/) end_col, end_row = back.scan(/\d+|\D+/) unless COL_NAMES.include?(start_col) && COL_NAMES.include?(end_col) raise SpreadsheetArchitect::Exceptions::InvalidRangeValue.new(:columns, range) end unless start_row.to_i <= num_rows && end_row.to_i <= num_rows raise SpreadsheetArchitect::Exceptions::InvalidRangeValue.new(:rows, range) end else raise SpreadsheetArchitect::Exceptions::InvalidRangeError.new(:format, range) end else raise SpreadsheetArchitect::Exceptions::InvalidRangeError.new(:type, range) end end |