Module: SpreadsheetArchitect::ClassMethods

Defined in:
lib/spreadsheet_architect.rb

Instance Method Summary collapse

Instance Method Details

#to_csv(opts = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/spreadsheet_architect.rb', line 107

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

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

#to_ods(opts = {}) ⇒ Object



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

def to_ods(opts={})
  options = SpreadsheetArchitect::Helpers.get_options(opts, self)

  spreadsheet = ODF::Spreadsheet.new

  spreadsheet.office_style :header_style, family: :cell do
    if options[:header_style]
      unless opts[:header_style] && opts[:header_style][:bold] == false #uses opts, temporary
        property :text, 'font-weight': :bold
      end
      if options[:header_style][:align]
        property :text, 'align': options[:header_style][:align]
      end
      if options[:header_style][:size]
        property :text, 'font-size': options[:header_style][:size]
      end
      if options[:header_style][:color] && opts[:header_style] && opts[:header_style][:color] #temporary
        property :text, 'color': "##{options[:header_style][:color]}"
      end
    end
  end
  spreadsheet.office_style :row_style, family: :cell do
    if options[:row_style]
      if options[:row_style][:bold]
        property :text, 'font-weight': :bold
      end
      if options[:row_style][:align]
        property :text, 'align': options[:row_style][:align]
      end
      if options[:row_style][:size]
        property :text, 'font-size': options[:row_style][:size]
      end
      if opts[:row_style] && opts[:row_style][:color] #uses opts, temporary
        property :text, 'color': "##{options[:row_style][:color]}"
      end
    end
  end

  spreadsheet.table options[:sheet_name] do 
    if options[:headers]
      row do
        options[:headers].each do |header|
          cell header, style: :header_style
        end
      end
    end
    options[:data].each do |row_data|
      row do 
        row_data.each do |y|
          cell y, style: :row_style
        end
      end
    end
  end

  return spreadsheet.bytes
end

#to_xlsx(opts = {}) ⇒ Object



177
178
179
180
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/spreadsheet_architect.rb', line 177

def to_xlsx(opts={})
  options = SpreadsheetArchitect::Helpers.get_options(opts, self)

  header_style = {}
  header_style[:fg_color] = options[:header_style].delete(:color)
  header_style[:bg_color] = options[:header_style].delete(:background_color)
  if header_style[:align]
    header_style[:alignment] = {}
    header_style[:alignment][:horizontal] = options[:header_style].delete(:align)
  end
  header_style[:b] = options[:header_style].delete(:bold)
  header_style[:sz] = options[:header_style].delete(:font_size)
  header_style[:i] = options[:header_style].delete(:italic)
  if options[:header_style][:underline]
    header_style[:u] = options[:header_style].delete(:underline)
  end
  header_style.delete_if{|x| x.nil?}

  row_style = {}
  row_style[:fg_color] = options[:row_style].delete(:color)
  row_style[:bg_color] = options[:row_style].delete(:background_color)
  if row_style[:align]
    row_style[:alignment] = {}
    row_style[:alignment][:horizontal] = options[:row_style][:align]
  end
  row_style[:b] = options[:row_style].delete(:bold)
  row_style[:sz] = options[:row_style].delete(:font_size)
  row_style[:i] = options[:row_style].delete(:italic)
  if options[:row_style][:underline]
    row_style[:u] = options[:row_style].delete(:underline)
  end
  row_style.delete_if{|x| x.nil?}
  
  package = Axlsx::Package.new

  return package if options[:data].empty?

  package.workbook.add_worksheet(name: options[:sheet_name]) do |sheet|
    if options[:headers]
      sheet.add_row options[:headers], style: package.workbook.styles.add_style(header_style)
    end
    
    options[:data].each do |row_data|
      sheet.add_row row_data, style: package.workbook.styles.add_style(row_style), types: options[:types]
    end
  end

  return package.to_stream.read
end