Module: SpreadsheetArchitect::Utils::XLSX

Defined in:
lib/spreadsheet_architect/utils/xlsx.rb

Class Method Summary collapse

Class Method Details

.convert_styles_to_axlsx(styles = {}) ⇒ Object



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

def self.convert_styles_to_axlsx(styles={})
  styles = {} unless styles.is_a?(Hash)
  styles = self.symbolize_keys(styles)

  if styles[:color].respond_to?(:sub) && !styles[:color].empty?
    styles[:fg_color] = styles.delete(:color).sub('#','')
  end

  if styles[:background_color].respond_to?(:sub) && !styles[:background_color].empty?
    styles[:bg_color] = styles.delete(:background_color).sub('#','')
  end

  if styles[:align]
    if styles[:align].is_a?(Hash)
      styles[:alignment] = {horizontal: styles[:align][:horizontal], vertical: styles[:align][:vertical]}
      styles.delete(:align)
    else
      styles[:alignment] = {horizontal: styles.delete(:align)}
    end
  end
  styles[:b] = styles.delete(:bold) || styles[:b]
  styles[:sz] = styles.delete(:font_size) || styles[:sz]
  styles[:i] = styles.delete(:italic) || styles[:i]
  styles[:u] = styles.delete(:underline) || styles[:u]

  styles.delete_if{|k,v| v.nil?}
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
# File 'lib/spreadsheet_architect/utils/xlsx.rb', line 5

def self.get_type(value, type=nil)
  return type unless (type.respond_to?(:empty?) ? type.empty? : type.nil?)
  
  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, col_names = Array('A'..'ZZZ')) ⇒ Object



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

def self.range_hash_to_str(hash, num_columns, num_rows, col_names=Array('A'..'ZZZ'))
  case hash[:columns]
  when Integer
    start_col = end_col = col_names[hash[:columns]]
  when Range
    start_col = col_names[hash[:columns].first]
    end_col = col_names[hash[:columns].last]
  when :all
    start_col = 'A'
    end_col = col_names[num_columns-1]
  else
    raise SpreadsheetArchitect::Exceptions::InvalidRangeStylesOptionError.new(:columns, hash)
  end

  case hash[:rows]
  when Integer
    start_row = end_row = hash[:rows]
  when Range
    start_row = hash[:rows].first
    end_row = hash[:rows].last
  when :all
    start_row = 1
    end_row = num_rows
  else
    raise SpreadsheetArchitect::Exceptions::InvalidRangeStylesOptionError.new(:rows, hash)
  end

  return "#{start_col}#{start_row}:#{end_col}#{end_row}"
end

.verify_range(range, num_rows, col_names = Array('A'..'ZZZ')) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/spreadsheet_architect/utils/xlsx.rb', line 85

def self.verify_range(range, num_rows, col_names=Array('A'..'ZZZ'))
  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::BadRangeError.new(:columns, range)
      end
      
      unless start_row.to_i <= num_rows && end_row.to_i <= num_rows
        raise SpreadsheetArchitect::Exceptions::BadRangeError.new(:rows, range)
      end
    else
      raise SpreadsheetArchitect::Exceptions::BadRangeError.new(:format, range)
    end
  else
    raise SpreadsheetArchitect::Exceptions::BadRangeError.new(:type, range)
  end
end