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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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[:fg_color].nil?
    if styles[:color].respond_to?(:sub) && !styles[:color].empty?
      styles[:fg_color] = styles.delete(:color).sub('#','')
    end
  end

  if styles[:bg_color].nil?
    if styles[:background_color].respond_to?(:sub) && !styles[:background_color].empty?
      styles[:bg_color] = styles.delete(:background_color).sub('#','')
    end
  end
  
  if styles[:alignment].nil? && styles[:align]
    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)}
    end

    styles.delete(:align)
  end

  if styles[:b].nil?
    styles[:b] = styles.delete(:bold)
  end

  if styles[:sz].nil?
    styles[:sz] = styles.delete(:font_size)
  end

  if styles[:i].nil?
    styles[:i] = styles.delete(:italic)
  end

  if styles[:u].nil?
    styles[:u] = styles.delete(:underline)
  end
  
  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



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

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/spreadsheet_architect/utils/xlsx.rb', line 106

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