catsheet 1.1.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.
Files changed (2) hide show
  1. data/lib/catsheet.rb +87 -0
  2. metadata +79 -0
@@ -0,0 +1,87 @@
1
+ # Author: Abe van der Wielen
2
+ # Email: abevanderwielen@gmail.com
3
+ # Website: https://github.com/the-abe
4
+ # Date: 2015-04-16
5
+ # File: catsheet.rb
6
+
7
+ require 'rubygems'
8
+ # Require the spreadsheet gem and check for success.
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
15
+
16
+ # Return the length of the string, taking multibyte characters into account.
17
+ def string_length(string)
18
+ return string.scan(/./mu).size
19
+ end
20
+
21
+ # Check if the name of a spreadsheet was supplied.
22
+ if ARGV[0].nil?
23
+ puts "You did not supply a spreadsheet to open."
24
+ exit 1
25
+ end
26
+
27
+ # Open the spreadsheet supplied in the arguments
28
+ begin
29
+ book = Spreadsheet.open(ARGV[0],'r')
30
+ rescue Ole::Storage::FormatError
31
+ puts "The file you wanted to open seems to be invalid."
32
+ exit 1
33
+ end
34
+
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
+ puts "There doesn't seem to be a sheet #{sheet_number}."
42
+ exit 1
43
+ end
44
+
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
+ column_counter = row.length-1 if row.length-1 > column_counter
50
+ row.each_with_index do |cell,index|
51
+ row.format 2
52
+ if width_array[index].nil? || width_array[index] < string_length(cell.to_s)
53
+ cell = " " if cell.nil?
54
+ width_array[index] = string_length(cell.to_s)
55
+ end
56
+ end
57
+ end
58
+
59
+ # Now that we've got the maximum width per column, we can print a table!
60
+ sheet.each do |row|
61
+ # Assume we only need two decimals for numbers.
62
+ row.format 2
63
+ row_array = []
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}"
76
+ end
77
+ 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
+ end
87
+ exit 0
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: catsheet
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 1
9
+ - 0
10
+ version: 1.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Abe van der Wielen
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2015-04-16 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: spreadsheet
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: A simple gem that prints spreadsheets like cat prints text files.
35
+ email: abevanderwielen@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - lib/catsheet.rb
44
+ homepage: https://github.com/the-abe/catsheet
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.15
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: A cat-like tool for spreadsheets.
77
+ test_files: []
78
+
79
+ has_rdoc: