excel_utils 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83f4fd8fec5396120145aefdaa76246f93310b40a6a80d9d799223546e8de9d2
4
- data.tar.gz: 45cce54a288cd0e462f60341d0fc4f8884eea4c640db9f92850a50f35e1f32bf
3
+ metadata.gz: 70807b62912a2e82da93e85e0275b6f3f6b6a8f010f70a7924546f31f0d93087
4
+ data.tar.gz: 6b78f90fd4174174044c9464299bf645bc13619251b1fde6f0ac9079a00b6d14
5
5
  SHA512:
6
- metadata.gz: 20d1dfdf88228997f8c999348541dd269ef2bd9510bb5480663a3aa9c1cef9528ffe1287c198355b55347299d0d89ba6e6946e4950bf11b9b398d66f59ecebdc
7
- data.tar.gz: 7e0d52d3569e343780230ba2efff28c69f449502de5dab3931d1caa426e3748c4131d72cabe003b28d4eac70602ed5be1c031470028069dcc8d0c6ddffe11544
6
+ metadata.gz: c8619bbea23bbad8b25c4014e0ead0bb8b81d901dce0c0e355e47b3b1fd2742f7c307d25980afc7d5066515ef33547d89d8c71df586ffe06bcdd3bc5dc6f9de4
7
+ data.tar.gz: 98a84f2fa77259e4ce3bbb691d267d4427e7e887eb0a41385ee7b82e59b7e1e6d06e1da333a257f3cdc9c54768a530c7c709608c3e8b89726c89ae61185b46fa
data/lib/excel_utils.rb CHANGED
@@ -30,4 +30,4 @@ module ExcelUtils
30
30
  Writer.write filename, data
31
31
  end
32
32
 
33
- end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module ExcelUtils
2
- VERSION = '1.2.0'
3
- end
2
+ VERSION = '1.3.0'
3
+ end
@@ -1,20 +1,24 @@
1
1
  module ExcelUtils
2
2
  class Writer
3
3
 
4
- TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
4
+ DEFAULT_SHEET_NAME = 'Sheet1'.freeze
5
+
6
+ TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'.freeze
5
7
 
6
8
  EXCEL_FORMATS = {
7
9
  date: 'yyyy-mm-dd',
8
10
  date_time: 'yyyy-mm-dd hh:mm:ss'
9
- }
11
+ }.freeze
10
12
 
11
13
  class << self
12
-
14
+
13
15
  def write(filename, data)
14
16
  workbook = WriteXLSX.new filename, strings_to_urls: false
15
17
 
16
18
  formats = add_formats workbook
17
19
 
20
+ data = {DEFAULT_SHEET_NAME => data} if data.is_a? Array
21
+
18
22
  data.each do |sheet_name, sheet_data|
19
23
  add_sheet workbook, sheet_name, sheet_data, formats
20
24
  end
@@ -41,14 +45,14 @@ module ExcelUtils
41
45
  row_index = r + 1
42
46
  header.each_with_index do |column, col_index|
43
47
  if row[column]
44
- if row[column].is_a? String
48
+ if row[column].is_a?(String) || row[column].is_a?(Array)
45
49
  sheet.write_string row_index, col_index, row[column]
46
-
50
+
47
51
  elsif row[column].respond_to? :to_time
48
52
  time = row[column].to_time
49
53
  type = date?(time) ? :date : :date_time
50
54
  sheet.write_date_time row_index, col_index, time.to_time.strftime(TIME_FORMAT), formats[type]
51
-
55
+
52
56
  else
53
57
  sheet.write row_index, col_index, row[column]
54
58
  end
@@ -7,10 +7,23 @@ require 'excel_utils'
7
7
 
8
8
  RESOURCES_PATH = File.expand_path '../resources', __FILE__
9
9
 
10
+ TMP_PATH = File.expand_path '../../tmp', __FILE__
11
+ FileUtils.mkpath TMP_PATH unless Dir.exists? TMP_PATH
12
+
10
13
  class Minitest::Test
11
14
 
12
15
  def resource_path(relative_path)
13
16
  File.join RESOURCES_PATH, relative_path
14
17
  end
15
18
 
19
+ def tmp_path(relative_path)
20
+ File.join TMP_PATH, relative_path
21
+ end
22
+
23
+ end
24
+
25
+ Minitest.after_run do
26
+ Dir.glob(File.join(TMP_PATH, '*')).each do |filename|
27
+ FileUtils.remove_file filename
28
+ end
16
29
  end
data/spec/write_spec.rb CHANGED
@@ -2,42 +2,95 @@ require 'minitest_helper'
2
2
 
3
3
  describe ExcelUtils do
4
4
 
5
- let(:tmp_path) { File.expand_path '../../tmp', __FILE__ }
5
+ def assert_wrote(data, expected=data)
6
+ filename = tmp_path "#{SecureRandom.uuid}.xlsx"
6
7
 
7
- let(:filename) { File.join tmp_path, "#{SecureRandom.uuid}.xlsx" }
8
+ ExcelUtils.write filename, data
9
+
10
+ workbook = ExcelUtils.read filename
11
+
12
+ expected = {'Sheet1' => expected} if expected.is_a?(Array)
8
13
 
9
- before do
10
- FileUtils.rm_rf tmp_path
11
- FileUtils.mkpath tmp_path
14
+ workbook.to_h.must_equal expected
12
15
  end
13
16
 
14
- it 'Write' do
15
- long_url = 'https://external.xx.fbcdn.net/safe_image.php?d=AQCY1f77h1RtuFfa&w=720&h=720&url=https%3A%2F%2Fwww.mercadolibre.com%2Fjms%2Fmla%2Flgz%2Fbackground%2Fsession%2Farmor.c27f26204365785956aaf9b30c289d0d4b0a201b4b01dfc3d83162b82608973be9a82ac85c2097c3f23b522681adca38b143f5e1a10fda251b430051a1592bf19caba204c07179120c48d47a1ceb5a45.774f4b8036d4b909101bab92870d262a%3Fbackground%3Darmor.c27f26204365785956aaf9b30c289d0d4b0a201b4b01dfc3d83162b82608973be9a82ac85c2097c3f23b522681adca38b143f5e1a10fda251b430051a1592bf19caba204c07179120c48d47a1ceb5a45.774f4b8036d4b909101bab92870d262a%26message%3DeyJqc190eXBlIjoianNfY29va2llIiwidmFsdWUiOiJ4In0&cfs=1&_nc_hash=AQB9Sf1HyWCwCVhF'
16
- long_text = 'AQCY1f77h1RtuFfa&w=720&h=720&url=https%3A%2F%2Fwww.mercadolibre.com%2Fjms%2Fmla%2Flgz%2Fbackground%2Fsession%2Farmor.c27f26204365785956aaf9b30c289d0d4b0a201b4b01dfc3d83162b82608973be9a82ac85c2097c3f23b522681adca38b143f5e1a10fda251b430051a1592bf19caba204c07179120c48d47a1ceb5a45.774f4b8036d4b909101bab92870d262a%3Fbackground%3Darmor.c27f26204365785956aaf9b30c289d0d4b0a201b4b01dfc3d83162b82608973be9a82ac85c2097c3f23b522681adca38b143f5e1a10fda251b430051a1592bf19caba204c07179120c48d47a1ceb5a45.774f4b8036d4b909101bab92870d262a%26message%3DeyJqc190eXBlIjoianNfY29va2llIiwidmFsdWUiOiJ4In0&cfs=1&_nc_hash=AQB9Sf1HyWCwCVhF'
17
+ describe 'Single sheet' do
18
+
19
+ it 'Basic data' do
20
+ rows = [
21
+ {'column_a' => '1', 'column_b' => 'text 1'},
22
+ {'column_a' => '2.5', 'column_b' => nil, 'column_c' => 'text 2'},
23
+ ]
24
+
25
+ assert_wrote rows, rows.map { |r| {'column_c' => nil}.merge r }
26
+ end
27
+
28
+ it 'Symbolized column names' do
29
+ rows = [
30
+ {column_a: 'text 1'},
31
+ {column_a: 'text 2'},
32
+ ]
33
+
34
+ assert_wrote rows, rows.map { |r| {'column_a' => r[:column_a]} }
35
+ end
36
+
37
+ it 'Long values' do
38
+ long_url = 'https://external.xx.fbcdn.net/safe_image.php?d=AQCY1f77h1RtuFfa&w=720&h=720&url=https%3A%2F%2Fwww.mercadolibre.com%2Fjms%2Fmla%2Flgz%2Fbackground%2Fsession%2Farmor.c27f26204365785956aaf9b30c289d0d4b0a201b4b01dfc3d83162b82608973be9a82ac85c2097c3f23b522681adca38b143f5e1a10fda251b430051a1592bf19caba204c07179120c48d47a1ceb5a45.774f4b8036d4b909101bab92870d262a%3Fbackground%3Darmor.c27f26204365785956aaf9b30c289d0d4b0a201b4b01dfc3d83162b82608973be9a82ac85c2097c3f23b522681adca38b143f5e1a10fda251b430051a1592bf19caba204c07179120c48d47a1ceb5a45.774f4b8036d4b909101bab92870d262a%26message%3DeyJqc190eXBlIjoianNfY29va2llIiwidmFsdWUiOiJ4In0&cfs=1&_nc_hash=AQB9Sf1HyWCwCVhF'
39
+ long_text = 'AQCY1f77h1RtuFfa&w=720&h=720&url=https%3A%2F%2Fwww.mercadolibre.com%2Fjms%2Fmla%2Flgz%2Fbackground%2Fsession%2Farmor.c27f26204365785956aaf9b30c289d0d4b0a201b4b01dfc3d83162b82608973be9a82ac85c2097c3f23b522681adca38b143f5e1a10fda251b430051a1592bf19caba204c07179120c48d47a1ceb5a45.774f4b8036d4b909101bab92870d262a%3Fbackground%3Darmor.c27f26204365785956aaf9b30c289d0d4b0a201b4b01dfc3d83162b82608973be9a82ac85c2097c3f23b522681adca38b143f5e1a10fda251b430051a1592bf19caba204c07179120c48d47a1ceb5a45.774f4b8036d4b909101bab92870d262a%26message%3DeyJqc190eXBlIjoianNfY29va2llIiwidmFsdWUiOiJ4In0&cfs=1&_nc_hash=AQB9Sf1HyWCwCVhF'
40
+
41
+ rows = [
42
+ {'column_a' => long_url},
43
+ {'column_a' => long_text},
44
+ ]
45
+
46
+ assert_wrote rows
47
+ end
48
+
49
+ it 'Numbers' do
50
+ rows = [
51
+ {'column_a' => 1},
52
+ {'column_a' => 2.5},
53
+ ]
17
54
 
55
+ assert_wrote rows
56
+ end
57
+
58
+ it 'Date and DateTime' do
59
+ rows = [
60
+ {'column_a' => DateTime.parse('2019-05-25T16:30:00')},
61
+ {'column_a' => Date.parse('2019-07-09')},
62
+ ]
63
+
64
+ assert_wrote rows
65
+ end
66
+
67
+ it 'Objects' do
68
+ rows = [
69
+ {'column_a' => ['text', 1]},
70
+ {'column_a' => {key: 123}},
71
+ {'column_a' => Set.new(['text', 1])}
72
+ ]
73
+
74
+ assert_wrote rows, rows.map { |r| {'column_a' => r['column_a'].to_s} }
75
+ end
76
+
77
+ end
78
+
79
+ it 'Multiple sheets' do
18
80
  data = {
19
- 'Sheet 1' => [
20
- {'column_a' => 1.5, 'column_b' => 'text 1', 'column_c' => DateTime.parse('2019-05-25T16:30:00')},
21
- {'column_b' => 'text 2', 'column_c' => Date.parse('2019-07-09'), 'column_a' => 2, 'column_d' => '0123'},
81
+ 'strings' => [
82
+ {'column_a' => 'text 1', 'column_b' => 'text 2'},
83
+ {'column_a' => 'text 3', 'column_b' => 'text 4'}
22
84
  ],
23
- 'Sheet 2' => [
24
- {'Column A' => long_url, 'Column B' => long_text}
85
+ 'numbers' => [
86
+ {'x' => 1, 'y' => 2},
87
+ {'x' => 3, 'y' => 4},
88
+ {'x' => 5, 'y' => 6}
25
89
  ],
26
- 'Sheet 3' => []
90
+ 'empty' => [],
27
91
  }
28
92
 
29
- ExcelUtils.write filename, data
30
-
31
- workbook = ExcelUtils.read filename
32
-
33
- workbook.sheets.each do |sheet|
34
- sheet.each_with_index do |row, i|
35
- columns = row.keys | data[sheet.name][i].keys
36
- columns.each do |column|
37
- row[column].must_equal data[sheet.name][i][column]
38
- end
39
- end
40
- end
93
+ assert_wrote data
41
94
  end
42
95
 
43
96
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: excel_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-16 00:00:00.000000000 Z
11
+ date: 2021-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inflecto