sheetsy 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +2 -1
- data/Gemfile.lock +2 -1
- data/README.md +3 -5
- data/lib/sheetsy/cli.rb +3 -10
- data/lib/sheetsy/converter.rb +38 -16
- data/lib/sheetsy/version.rb +1 -1
- data/lib/sheetsy.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5187487347d0e1505b9e40889c488ab8341fb55d866d6af96487addcb0a60150
|
4
|
+
data.tar.gz: 863582071eb4b16b551b034ea0ceb5b380603fff767f2ac4314a55c3d3789d95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be8be3a1ae1e007f319cc65b4f400e95a8acba69715ea17b8844693799d95bce5b95791f5db51bcc2fb9a6e2a98269b626c5c11218279d7bf929612171de2ed3
|
7
|
+
data.tar.gz: 399dea79e08ecdbee1ee49268511a0e2ab1e3a4228706ec59c2db8f520b2c393557710f4df452ffd94f927389b5fd9414bca6556832629bcc3b8a68205406c79
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Sheetsy
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Sheetsy is a simple ruby utility to recursively convert all CSV and Excel-based sheets to individual JSON files.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -19,13 +17,13 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
19
17
|
To get started, run the following command from bash:
|
20
18
|
|
21
19
|
```bash
|
22
|
-
|
20
|
+
$ sheetsy convert
|
23
21
|
```
|
24
22
|
|
25
23
|
To learn more about available options:
|
26
24
|
|
27
25
|
```bash
|
28
|
-
|
26
|
+
$ sheetsy help convert
|
29
27
|
```
|
30
28
|
|
31
29
|
## Development
|
data/lib/sheetsy/cli.rb
CHANGED
@@ -7,7 +7,8 @@ module Sheetsy
|
|
7
7
|
desc "convert", "Convert sheets to JSON"
|
8
8
|
option :source, type: :string, aliases: :s
|
9
9
|
option :destination, type: :string, aliases: :d
|
10
|
-
option :
|
10
|
+
option :overwrite, type: :boolean, aliases: :o, default: false
|
11
|
+
option :debug, type: :boolean, default: false
|
11
12
|
def convert
|
12
13
|
source = if options[:source]
|
13
14
|
options[:source]
|
@@ -27,15 +28,7 @@ module Sheetsy
|
|
27
28
|
get_input("Output Directory", File.join(*default_source_folders, default_output_folder))
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
-
options[:overwrite]
|
32
|
-
elsif ENV.key?("OVERWRITE")
|
33
|
-
ENV.fetch("OVERWRITE")&.downcase == "y"
|
34
|
-
else
|
35
|
-
get_input("Overwrite Y/n? (n): ", "n").downcase == "y"
|
36
|
-
end
|
37
|
-
|
38
|
-
Sheetsy::Converter.new(source, destination, overwrite).run
|
31
|
+
Sheetsy::Converter.new(source, destination, options).run
|
39
32
|
end
|
40
33
|
|
41
34
|
private
|
data/lib/sheetsy/converter.rb
CHANGED
@@ -2,68 +2,90 @@
|
|
2
2
|
|
3
3
|
module Sheetsy
|
4
4
|
class Converter
|
5
|
-
attr_reader :source, :destination, :
|
5
|
+
attr_reader :source, :destination, :options
|
6
6
|
|
7
|
-
def initialize(source, destination,
|
7
|
+
def initialize(source, destination, options = {})
|
8
8
|
@source = source
|
9
9
|
@destination = destination
|
10
|
-
@
|
10
|
+
@options = options
|
11
11
|
end
|
12
12
|
|
13
13
|
def run
|
14
|
-
|
15
|
-
|
14
|
+
debug "Files found: #{files.count}"
|
15
|
+
|
16
|
+
progress_bar.progress = 0
|
16
17
|
|
17
18
|
FileUtils.mkdir_p(destination) unless File.directory?(destination)
|
18
19
|
|
19
20
|
files.each do |file|
|
20
21
|
process_file(file)
|
22
|
+
progress_bar.increment
|
21
23
|
end
|
24
|
+
|
25
|
+
puts "Conversion complete."
|
22
26
|
end
|
23
27
|
|
24
28
|
def overwrite?
|
25
|
-
overwrite
|
29
|
+
@overwrite ||= options[:overwrite]
|
30
|
+
end
|
31
|
+
|
32
|
+
def debug?
|
33
|
+
@debug ||= options[:debug]
|
26
34
|
end
|
27
35
|
|
28
36
|
private
|
29
37
|
|
38
|
+
def files
|
39
|
+
@files ||= Dir.glob(File.join(source, "**/*.{xls,xlsx,csv}"))
|
40
|
+
end
|
41
|
+
|
42
|
+
def progress_bar
|
43
|
+
@progress_bar ||= ProgressBar.create(title: "Files", total: files.count)
|
44
|
+
end
|
45
|
+
|
46
|
+
def debug(str)
|
47
|
+
return unless debug?
|
48
|
+
|
49
|
+
progress_bar.log str
|
50
|
+
end
|
51
|
+
|
30
52
|
def process_csv(file)
|
31
|
-
|
53
|
+
debug "Processing as CSV"
|
32
54
|
|
33
55
|
output_file_path = File.join(destination, "#{nameify(file)}.json")
|
34
56
|
return if skip?(output_file_path)
|
35
57
|
|
36
|
-
|
58
|
+
debug "File #{output_file_path}"
|
37
59
|
|
38
60
|
data = CSV.read(file, headers: true).map(&:to_h)
|
39
61
|
write_json(output_file_path, data)
|
40
|
-
|
62
|
+
debug "Converted #{file} to #{output_file_path}\n\n"
|
41
63
|
end
|
42
64
|
|
43
65
|
def process_excel(file)
|
44
|
-
|
66
|
+
debug "Processing as Excel"
|
45
67
|
excel = Roo::Spreadsheet.open(file)
|
46
68
|
|
47
69
|
excel.sheets.each do |sheet_name|
|
48
70
|
output_file_path = File.join(destination, "#{nameify(file)}__sheet__#{nameify(sheet_name)}.json")
|
49
71
|
return if skip?(output_file_path)
|
50
72
|
|
51
|
-
|
73
|
+
debug "Sheet #{output_file_path}"
|
52
74
|
|
53
75
|
sheet = excel.sheet(sheet_name)
|
54
76
|
header_row = (output_file_path.include?("Synergy_Output__") ? 2 : 1)
|
55
77
|
header = sheet.row(header_row)
|
56
78
|
data = ((header_row + 1)..sheet.last_row).map { |i| Hash[header.zip(sheet.row(i))] }
|
57
79
|
write_json(output_file_path, data)
|
58
|
-
|
80
|
+
debug "Converted #{file} to #{output_file_path}\n\n"
|
59
81
|
end
|
60
82
|
end
|
61
83
|
|
62
84
|
def process_file(file)
|
63
|
-
|
85
|
+
debug "File: #{file}"
|
64
86
|
|
65
87
|
if file.include?("~$")
|
66
|
-
|
88
|
+
debug "Skipping #{file}\n\n"
|
67
89
|
|
68
90
|
return
|
69
91
|
end
|
@@ -76,7 +98,7 @@ module Sheetsy
|
|
76
98
|
elsif [".xls", ".xlsx"].include?(extension)
|
77
99
|
process_excel(file)
|
78
100
|
else
|
79
|
-
|
101
|
+
debug "Unsupported file format: #{file}"
|
80
102
|
return
|
81
103
|
end
|
82
104
|
end
|
@@ -95,7 +117,7 @@ module Sheetsy
|
|
95
117
|
return false if overwrite?
|
96
118
|
return false unless File.exist?(file)
|
97
119
|
|
98
|
-
|
120
|
+
debug "Skipping #{file}"
|
99
121
|
true
|
100
122
|
end
|
101
123
|
end
|
data/lib/sheetsy/version.rb
CHANGED
data/lib/sheetsy.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sheetsy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-10-
|
11
|
+
date: 2023-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Easily convert CSV and Excel sheets to JSON
|
14
14
|
email:
|