catsheet 1.1.0 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/catsheet +3 -0
- data/lib/catsheet.rb +83 -71
- metadata +6 -5
data/bin/catsheet
ADDED
data/lib/catsheet.rb
CHANGED
@@ -3,85 +3,97 @@
|
|
3
3
|
# Website: https://github.com/the-abe
|
4
4
|
# Date: 2015-04-16
|
5
5
|
# File: catsheet.rb
|
6
|
-
|
7
6
|
require 'rubygems'
|
8
|
-
|
9
|
-
begin
|
10
|
-
require 'spreadsheet'
|
11
|
-
rescue LoadError
|
12
|
-
puts "Could not load the spreadsheet gem. Please run \"gem install spreadsheet\"."
|
13
|
-
exit 1
|
14
|
-
end
|
7
|
+
require 'catsheet'
|
15
8
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
9
|
+
module Catsheet
|
10
|
+
class Sheetscript
|
11
|
+
# Require the spreadsheet gem and check for success.
|
12
|
+
begin
|
13
|
+
require 'spreadsheet'
|
14
|
+
rescue LoadError
|
15
|
+
puts "Could not load the spreadsheet gem. Please run \"gem install spreadsheet\"."
|
16
|
+
exit 1
|
17
|
+
end
|
20
18
|
|
21
|
-
|
22
|
-
if
|
23
|
-
|
24
|
-
|
25
|
-
|
19
|
+
def self.main
|
20
|
+
# Check if the name of a spreadsheet was supplied.
|
21
|
+
if ARGV[0].nil?
|
22
|
+
puts "You did not supply a spreadsheet to open."
|
23
|
+
exit 1
|
24
|
+
end
|
26
25
|
|
27
|
-
# Open the spreadsheet supplied in the arguments
|
28
|
-
begin
|
29
|
-
|
30
|
-
rescue Ole::Storage::FormatError
|
31
|
-
|
32
|
-
|
33
|
-
|
26
|
+
# Open the spreadsheet supplied in the arguments
|
27
|
+
begin
|
28
|
+
book = Spreadsheet.open(ARGV[0],'r')
|
29
|
+
rescue Ole::Storage::FormatError
|
30
|
+
puts "The file you wanted to open seems to be invalid."
|
31
|
+
exit 1
|
32
|
+
rescue Errno::ENOENT
|
33
|
+
puts "The file you wanted to open can't be opened."
|
34
|
+
exit 1
|
35
|
+
end
|
34
36
|
|
35
|
-
# If a sheet number is supplied, use it.
|
36
|
-
# The spreadsheet gem starts counting from 0, to we decrease the number by one.
|
37
|
-
# If no number is supplied we assume the first one.
|
38
|
-
sheet_number = (ARGV[1]||1).to_i - 1
|
39
|
-
sheet = book.worksheet sheet_number
|
40
|
-
if sheet.nil?
|
41
|
-
|
42
|
-
|
43
|
-
end
|
37
|
+
# If a sheet number is supplied, use it.
|
38
|
+
# The spreadsheet gem starts counting from 0, to we decrease the number by one.
|
39
|
+
# If no number is supplied we assume the first one.
|
40
|
+
sheet_number = (ARGV[1]||1).to_i - 1
|
41
|
+
sheet = book.worksheet sheet_number
|
42
|
+
if sheet.nil?
|
43
|
+
puts "There doesn't seem to be a sheet #{sheet_number}."
|
44
|
+
exit 1
|
45
|
+
end
|
44
46
|
|
45
|
-
# Build and array of column widths based on the max width per column.
|
46
|
-
width_array = []
|
47
|
-
column_counter = 0
|
48
|
-
sheet.each do |row|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
47
|
+
# Build and array of column widths based on the max width per column.
|
48
|
+
width_array = []
|
49
|
+
column_counter = 0
|
50
|
+
sheet.each do |row|
|
51
|
+
column_counter = row.length-1 if row.length-1 > column_counter
|
52
|
+
row.each_with_index do |cell,index|
|
53
|
+
row.format 2
|
54
|
+
if width_array[index].nil? || width_array[index] < string_length(cell.to_s)
|
55
|
+
cell = " " if cell.nil?
|
56
|
+
width_array[index] = string_length(cell.to_s)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Now that we've got the maximum width per column, we can print a table!
|
62
|
+
sheet.each do |row|
|
63
|
+
# Assume we only need two decimals for numbers.
|
64
|
+
row.format 2
|
65
|
+
row_array = []
|
66
|
+
row.each_with_index do |cell,index|
|
67
|
+
# Put a space in empty cells so we don't break the layout of the table.
|
68
|
+
cell = " " if cell.nil?
|
69
|
+
# Get the maximum length from the array and add spaces to make the cell as
|
70
|
+
# long as the rest.
|
71
|
+
length = width_array[index]
|
72
|
+
filler = length - string_length(cell.to_s)
|
73
|
+
#Cells without digits are left aligned number cells are right aligned
|
74
|
+
if cell =~ /[^\d\.]+/
|
75
|
+
row_array << "#{cell}#{' '*filler}"
|
76
|
+
else
|
77
|
+
row_array << "#{' '*filler}#{cell}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
# Loop through it all and fill empty cells at the end of the row.
|
81
|
+
result_columns = (0..column_counter).map do |index|
|
82
|
+
row_array[index].nil? ? " "*width_array[index] : row_array[index]
|
83
|
+
end
|
84
|
+
result = "|"
|
85
|
+
result += result_columns.join("|")
|
86
|
+
result += "|"
|
87
|
+
puts result
|
88
|
+
end
|
55
89
|
end
|
56
|
-
end
|
57
|
-
end
|
58
90
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
row.each_with_index do |cell,index|
|
65
|
-
# Put a space in empty cells so we don't break the layout of the table.
|
66
|
-
cell = " " if cell.nil?
|
67
|
-
# Get the maximum length from the array and add spaces to make the cell as
|
68
|
-
# long as the rest.
|
69
|
-
length = width_array[index]
|
70
|
-
filler = length - string_length(cell.to_s)
|
71
|
-
#Cells without digits are left aligned number cells are right aligned
|
72
|
-
if cell =~ /[^\d\.]+/
|
73
|
-
row_array << "#{cell}#{' '*filler}"
|
74
|
-
else
|
75
|
-
row_array << "#{' '*filler}#{cell}"
|
91
|
+
private
|
92
|
+
|
93
|
+
# Return the length of the string, taking multibyte characters into account.
|
94
|
+
def string_length(string)
|
95
|
+
return string.scan(/./mu).size
|
76
96
|
end
|
97
|
+
|
77
98
|
end
|
78
|
-
# Loop through it all and fill empty cells at the end of the row.
|
79
|
-
result_columns = (0..column_counter).map do |index|
|
80
|
-
row_array[index].nil? ? " "*width_array[index] : row_array[index]
|
81
|
-
end
|
82
|
-
result = "|"
|
83
|
-
result += result_columns.join("|")
|
84
|
-
result += "|"
|
85
|
-
puts result
|
86
99
|
end
|
87
|
-
exit 0
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: catsheet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 1.1.
|
9
|
+
- 2
|
10
|
+
version: 1.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Abe van der Wielen
|
@@ -33,13 +33,14 @@ dependencies:
|
|
33
33
|
version_requirements: *id001
|
34
34
|
description: A simple gem that prints spreadsheets like cat prints text files.
|
35
35
|
email: abevanderwielen@gmail.com
|
36
|
-
executables:
|
37
|
-
|
36
|
+
executables:
|
37
|
+
- catsheet
|
38
38
|
extensions: []
|
39
39
|
|
40
40
|
extra_rdoc_files: []
|
41
41
|
|
42
42
|
files:
|
43
|
+
- bin/catsheet
|
43
44
|
- lib/catsheet.rb
|
44
45
|
homepage: https://github.com/the-abe/catsheet
|
45
46
|
licenses: []
|