spreadsheet_exporter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Nicholas Jakobsen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ = SpreadsheetExporter
2
+
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SpreadsheetExporter'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,5 @@
1
+ require 'spreadsheet_exporter/csv'
2
+ require 'spreadsheet_exporter/xlsx'
3
+
4
+ module SpreadsheetExporter
5
+ end
@@ -0,0 +1,23 @@
1
+ require 'csv'
2
+ require_relative 'spreadsheet'
3
+
4
+ module SpreadsheetExporter
5
+ module CSV
6
+ BOM = "\377\376".force_encoding("utf-16le") # Byte Order Mark so Excel displays characters correctly
7
+
8
+ def self.from_objects(objects, options = {})
9
+ spreadsheet = Spreadsheet.from_objects(objects, options)
10
+ from_spreadsheet(spreadsheet)
11
+ end
12
+
13
+ def self.from_spreadsheet(spreadsheet, temp_file_path = 'tmp/items.xlsx')
14
+ output = ::CSV.generate(:encoding => 'UTF-8', :col_sep => "\t") do |csv|
15
+ spreadsheet.each do |row|
16
+ csv << row
17
+ end
18
+ end
19
+
20
+ return BOM + output.encode!('utf-16le')
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,60 @@
1
+ # TODO: Find out why we can't detect arrays properly and must resort to crappy class.name comparison
2
+ module SpreadsheetExporter
3
+ module Spreadsheet
4
+ def self.from_objects(objects, options = {})
5
+ headers = []
6
+ rows = []
7
+
8
+ # Get all the data and accumulate headers from each row (since rows may not have all the same attributes)
9
+ Array(objects).each do |object|
10
+ data = object.respond_to?(:as_csv) ? get_values(object.as_csv(options)) : get_values(object.as_json(options))
11
+ headers = headers | data.keys
12
+ rows << data
13
+ end
14
+
15
+ # Create the csv, ensuring to place each row's attributes under the appropriate header (since rows may not have all the same attributes)
16
+ [].tap do |spreadsheet|
17
+ spreadsheet << headers
18
+ rows.each do |row|
19
+ sorted_row = []
20
+ row.each do |header, value|
21
+ sorted_row[headers.index(header)] = value
22
+ end
23
+
24
+ spreadsheet << sorted_row
25
+ end
26
+ end
27
+ end
28
+
29
+ # Return an array of human_attribute_name's
30
+ # Used by the CSV Import/Export process to match CSV headers to model attribute names
31
+ def self.han(klass, *attributes)
32
+ options = attributes.extract_options!
33
+
34
+ attributes.flatten!
35
+ attributes.collect! {|attribute| klass.human_attribute_name(attribute) }
36
+ attributes.collect!(&:downcase) if options[:downcase]
37
+
38
+ return attributes.many? ? attributes : attributes.first
39
+ end
40
+
41
+ def self.get_values(node, current_header = nil)
42
+ output = {}
43
+ case node.class.name
44
+ when 'Hash'
45
+ node.each do |key, subnode|
46
+ output.merge! get_values(subnode, [current_header, key].compact.join('_'))
47
+ end
48
+ when 'Array'
49
+ node.each do |subnode|
50
+ get_values(subnode, current_header).each do |key, value|
51
+ output[key] = [output[key], value.to_s].compact.join(', ')
52
+ end
53
+ end
54
+ else
55
+ output[current_header] = node.to_s
56
+ end
57
+ return output
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module SpreadsheetExporter
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,43 @@
1
+ require 'write_xlsx'
2
+ require_relative 'spreadsheet'
3
+
4
+ module SpreadsheetExporter
5
+ module XLSX
6
+ def self.from_objects(objects, options = {})
7
+ spreadsheet = Spreadsheet.from_objects(objects, options)
8
+ from_spreadsheet(spreadsheet)
9
+ end
10
+
11
+ def self.from_spreadsheet(spreadsheet, temp_file_path = 'tmp/items.xlsx')
12
+ # Create a new Excel workbook
13
+ workbook = WriteXLSX.new(temp_file_path)
14
+
15
+ # Add a worksheet
16
+ worksheet = workbook.add_worksheet
17
+
18
+ # Add and define a format
19
+ headerFormat = workbook.add_format # Add a format
20
+ headerFormat.set_bold
21
+
22
+ # Write header row
23
+ spreadsheet.first.each_with_index do |column_name, col|
24
+ worksheet.write(0, col, column_name, headerFormat)
25
+ end
26
+
27
+ spreadsheet[1..-1].each_with_index do |values, row|
28
+ values.each_with_index do |value, col|
29
+ worksheet.write(row + 1, col, value)
30
+ end
31
+ end
32
+
33
+ # Output the file contents and delete it
34
+ workbook.close
35
+ file = File.open(temp_file_path)
36
+ output = file.read
37
+ file.close
38
+ File.delete(temp_file_path)
39
+
40
+ return output
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :spreadsheet_exporter do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spreadsheet_exporter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nicholas Jakobsen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-05-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: write_xlsx
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Export your data as various types of spreadsheets. Supports csv and xlsx
31
+ output.
32
+ email:
33
+ - nicholas.jakobsen@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - lib/spreadsheet_exporter/csv.rb
39
+ - lib/spreadsheet_exporter/spreadsheet.rb
40
+ - lib/spreadsheet_exporter/version.rb
41
+ - lib/spreadsheet_exporter/xlsx.rb
42
+ - lib/spreadsheet_exporter.rb
43
+ - lib/tasks/exporter_tasks.rake
44
+ - MIT-LICENSE
45
+ - Rakefile
46
+ - README.md
47
+ homepage: https://github.com/culturecode/spreadsheet_exporter
48
+ licenses:
49
+ - MIT
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.25
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Export your data as various types of spreadsheets
72
+ test_files: []