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

Class Method Details

.convert_styles_to_ods(styles = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/spreadsheet_architect/utils.rb', line 129

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

def self.get_cell_data(options, klass)
  if options[:data] && options[:instances]
    raise SpreadsheetArchitect::Exceptions::MultipleDataSourcesError
  elsif options[:data]
    data = options[:data]
  end

  if !options[:data] && options[:headers] == true
    headers = []
    needs_headers = true
  elsif options[:headers].is_a?(Array)
    headers = options[:headers]
  else
    headers = false
  end

  if options[:column_types]
    column_types = options[:column_types]
  else
    column_types = []
    needs_column_types = true
  end

  if !data
    if !options[:instances]
      if is_ar_model?(klass) 
        options[: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 !options[:spreadsheet_columns] && 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

    data = []
    options[:instances].each do |instance|
      if columns
        data.push columns.map{|col| col.is_a?(Symbol) ? instance.instance_eval(col.to_s) : col}
      else
        row_data = []

        if !options[:spreadsheet_columns]
          if klass == SpreadsheetArchitect && !instance.respond_to?(:spreadsheet_columns)
            raise SpreadsheetArchitect::Exceptions::SpreadsheetColumnsNotDefinedError.new(instance.class)
          else
            instance_cols = instance.spreadsheet_columns
          end
        else
          instance_cols = options[:spreadsheet_columns].call(instance)
        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.instance_eval(x[1].to_s) : 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.instance_eval(x.to_s) : 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 options.merge(headers: headers, data: data, column_types: column_types)
end

.get_options(options, klass) ⇒ Object



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

def self.get_options(options, klass)
  verify_option_types(options)

  if defined?(klass::SPREADSHEET_OPTIONS)
    if klass::SPREADSHEET_OPTIONS.is_a?(Hash)
      options = SpreadsheetArchitect.default_options.merge(
        klass::SPREADSHEET_OPTIONS.merge(options)
      )
    else
      raise SpreadsheetArchitect::Exceptions::InvalidTypeError.new("#{klass}::SPREADSHEET_OPTIONS constant")
    end
  else
    options = SpreadsheetArchitect.default_options.merge(options)
  end

  if !options[:headers]
    options[:header_style] = false
  end

  if !options[:sheet_name]
    if klass == SpreadsheetArchitect
      options[:sheet_name] = 'Sheet1'
    else
      options[:sheet_name] = klass.name

      if options[:sheet_name].respond_to?(:pluralize)
        options[:sheet_name] = options[:sheet_name].pluralize
      end
    end
  end

  return options
end