Module: SpreadsheetArchitect::ClassMethods

Included in:
SpreadsheetArchitect
Defined in:
lib/spreadsheet_architect/class_methods/csv.rb,
lib/spreadsheet_architect/class_methods/ods.rb,
lib/spreadsheet_architect/class_methods/xlsx.rb

Instance Method Summary collapse

Instance Method Details

#to_axlsx_package(opts = {}, package = nil) ⇒ Object



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
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
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
186
187
188
189
190
191
192
193
# File 'lib/spreadsheet_architect/class_methods/xlsx.rb', line 12

def to_axlsx_package(opts={}, package=nil)
  opts = SpreadsheetArchitect::Utils.get_options(opts, self)
  options = SpreadsheetArchitect::Utils.get_cell_data(opts, self)

  header_style = SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(options[:header_style])
  row_style = SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(options[:row_style])

  if package.nil?
    package = Axlsx::Package.new
  end

  row_index = -1

  package.workbook.add_worksheet(name: options[:sheet_name]) do |sheet|
    max_row_length = options[:data].empty? ? 0 : options[:data].max_by{|x| x.length}.length

    if options[:headers]
      header_style_index = package.workbook.styles.add_style(header_style)

      options[:headers].each do |header_row|
        row_index += 1

        missing = max_row_length - header_row.count
        if missing > 0
          missing.times do
            header_row.push(nil)
          end
        end

        sheet.add_row header_row, style: header_style_index

        if options[:conditional_row_styles]
          conditional_styles_for_row = SpreadsheetArchitect::Utils::XLSX.conditional_styles_for_row(options[:conditional_row_styles], row_index, header_row)
          
          unless conditional_styles_for_row.empty?
            sheet.add_style(
              "#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES.first}#{row_index+1}:#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES[max_row_length-1]}#{row_index+1}", 
              SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(conditional_styles_for_row)
            )
          end
        end
      end
    end

    if options[:data].empty?
      break
    end

    row_style_index = package.workbook.styles.add_style(row_style)

    default_date_style_index = nil
    default_time_style_index = nil

    options[:data].each do |row_data|
      row_index += 1

      missing = max_row_length - row_data.count
      if missing > 0
        missing.times do
          row_data.push(nil)
        end
      end

      types = []
      styles = []
      row_data.each_with_index do |x,i|
        if (x.respond_to?(:empty) ? x.empty? : x.nil?)
          types[i] = nil
          styles[i] = row_style_index
        else
          if options[:column_types]
            types[i] = options[:column_types][i]
          end

          types[i] ||= SpreadsheetArchitect::Utils::XLSX.get_type(x)

          if [:date, :time].include?(types[i])
            if types[i] == :date
              default_date_style_index ||= package.workbook.styles.add_style(row_style.merge({format_code: 'yyyy-mm-dd'}))
              styles[i] = default_date_style_index
            else
              default_time_style_index ||= package.workbook.styles.add_style(row_style.merge({format_code: 'yyyy-mm-dd h:mm AM/PM'}))
              styles[i] = default_time_style_index
            end
          else
            styles[i] = row_style_index
          end
        end
      end

      sheet.add_row row_data, style: styles, types: types

      if options[:conditional_row_styles]
        conditional_styles_for_row = SpreadsheetArchitect::Utils::XLSX.conditional_styles_for_row(options[:conditional_row_styles], row_index, row_data)
        
        unless conditional_styles_for_row.empty?
          sheet.add_style(
            "#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES.first}#{row_index+1}:#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES[max_row_length-1]}#{row_index+1}", 
            SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(conditional_styles_for_row)
          )
        end
      end
    end

    if options[:column_widths]
      sheet.column_widths(*options[:column_widths])
    end

    if options[:borders] || options[:column_styles] || options[:range_styles] || options[:merges]
      num_rows = options[:data].count + (options[:headers] ? options[:headers].count : 0)
    end

    if options[:borders]
      options[:borders].each do |x|
        if x[:range].is_a?(Hash)
          x[:range] = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str(x[:range], max_row_length, num_rows)
        else
          SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows)
        end
      end
    end

    if options[:column_styles]
      options[:column_styles].each do |x|
        start_row = (options[:headers] ? options[:headers].count : 0) + 1

        x[:styles] = SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(x[:styles])

        add_column_style = ->(col){
          SpreadsheetArchitect::Utils::XLSX.verify_column(col, max_row_length)

          range_str = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str({rows: (start_row..num_rows), columns: col}, max_row_length, num_rows)
          sheet.add_style range_str, x[:styles]

          if x[:include_header] && start_row > 1
            range_str = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str({rows: (1..start_row-1), columns: col}, max_row_length, num_rows)
            sheet.add_style(range_str, x[:styles])
          end
        }

        case x[:columns]
        when Array, Range
          x[:columns].each do |col|
            add_column_style.call(col)
          end
        when Integer, String
          add_column_style.call(x[:columns])
        else
          SpreadsheetArchitect::Utils::XLSX.verify_column(x[:columns], max_row_length)
        end
      end
    end

    if options[:range_styles]
      options[:range_styles].each do |x|
        styles = SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(x[:styles])

        if x[:range].is_a?(Hash)
          x[:range] = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str(x[:range], max_row_length, num_rows)
        else
          SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows)
        end

        sheet.add_style x[:range], styles
      end
    end

    if options[:merges]
      options[:merges].each do |x|
        if x[:range].is_a?(Hash)
          x[:range] = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str(x[:range], max_row_length, num_rows)
        else
          SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows)
        end

        sheet.merge_cells x[:range]
      end
    end
  end

  return package
end

#to_csv(opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/spreadsheet_architect/class_methods/csv.rb', line 5

def to_csv(opts={})
  opts = SpreadsheetArchitect::Utils.get_options(opts, self)
  options = SpreadsheetArchitect::Utils.get_cell_data(opts, self)

  CSV.generate do |csv|
    if options[:headers]
      options[:headers].each do |header_row|
        csv << header_row
      end
    end
    
    options[:data].each do |row_data|
      csv << row_data
    end
  end
end

#to_ods(opts = {}) ⇒ Object



5
6
7
# File 'lib/spreadsheet_architect/class_methods/ods.rb', line 5

def to_ods(opts={})
  return to_rodf_spreadsheet(opts).bytes
end

#to_rodf_spreadsheet(opts = {}, spreadsheet = nil) ⇒ Object



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
# File 'lib/spreadsheet_architect/class_methods/ods.rb', line 9

def to_rodf_spreadsheet(opts={}, spreadsheet=nil)
  opts = SpreadsheetArchitect::Utils.get_options(opts, self)
  options = SpreadsheetArchitect::Utils.get_cell_data(opts, self)

  if !spreadsheet
    spreadsheet = RODF::Spreadsheet.new
  end

  spreadsheet.office_style :header_style, family: :cell do
    if options[:header_style]
      SpreadsheetArchitect::Utils.convert_styles_to_ods(options[:header_style]).each do |prop, styles|
        styles.each do |k,v|
          property prop.to_sym, k => v
        end
      end
    end
  end

  spreadsheet.office_style :row_style, family: :cell do
    if options[:row_style]
      SpreadsheetArchitect::Utils.convert_styles_to_ods(options[:row_style]).each do |prop, styles|
        styles.each do |k,v|
          property prop.to_sym, k => v
        end
      end
    end
  end

  spreadsheet.table options[:sheet_name] do 
    if options[:headers]
      options[:headers].each do |header_row|
        row do
          header_row.each_with_index do |header, i|
            cell header, style: :header_style
          end
        end
      end
    end

    options[:data].each_with_index do |row_data, index|
      row do 
        row_data.each_with_index do |val, i|
          if options[:types]
            type = options[:types][i]
          end

          if type
            if [:date, :time].include?(type)
              type = :string
              val = val.to_s
            end
          elsif val.respond_to?(:strftime)
            type = :string
            val = val.to_s 
          end

          cell val, style: :row_style, type: (options[:types][i] if options[:types])
        end
      end
    end
  end

  return spreadsheet
end

#to_xlsx(opts = {}) ⇒ Object



8
9
10
# File 'lib/spreadsheet_architect/class_methods/xlsx.rb', line 8

def to_xlsx(opts={})
  return to_axlsx_package(opts).to_stream.read
end