workbook 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/.gitignore +2 -1
  2. data/.yardoc/checksums +22 -21
  3. data/.yardoc/object_types +0 -0
  4. data/.yardoc/objects/root.dat +0 -0
  5. data/Gemfile.lock +2 -2
  6. data/doc/RubyXL.html +1 -1
  7. data/doc/RubyXL/Cell.html +1 -1
  8. data/doc/RubyXL/Workbook.html +31 -31
  9. data/doc/Workbook.html +4 -4
  10. data/doc/Workbook/Book.html +5 -5
  11. data/doc/Workbook/Cell.html +202 -62
  12. data/doc/Workbook/Format.html +32 -32
  13. data/doc/Workbook/Modules.html +1 -1
  14. data/doc/Workbook/Modules/RawObjectsStorage.html +5 -5
  15. data/doc/Workbook/Modules/TableDiffSort.html +14 -14
  16. data/doc/Workbook/Modules/TypeParser.html +7 -7
  17. data/doc/Workbook/NilValue.html +434 -0
  18. data/doc/Workbook/Readers.html +3 -3
  19. data/doc/Workbook/Readers/CsvReader.html +6 -6
  20. data/doc/Workbook/Readers/OdsReader.html +562 -60
  21. data/doc/Workbook/Readers/TxtReader.html +2 -2
  22. data/doc/Workbook/Readers/XlsReader.html +14 -14
  23. data/doc/Workbook/Readers/XlsShared.html +67 -67
  24. data/doc/Workbook/Readers/XlsxReader.html +7 -7
  25. data/doc/Workbook/Row.html +243 -5
  26. data/doc/Workbook/Sheet.html +2 -2
  27. data/doc/Workbook/Table.html +669 -58
  28. data/doc/Workbook/Template.html +5 -5
  29. data/doc/Workbook/Writers.html +1 -1
  30. data/doc/Workbook/Writers/CsvTableWriter.html +1 -1
  31. data/doc/Workbook/Writers/HtmlWriter.html +27 -13
  32. data/doc/Workbook/Writers/XlsWriter.html +2 -2
  33. data/doc/_index.html +23 -8
  34. data/doc/class_list.html +1 -1
  35. data/doc/file.README.html +16 -12
  36. data/doc/index.html +16 -12
  37. data/doc/method_list.html +232 -72
  38. data/doc/top-level-namespace.html +1 -1
  39. data/lib/workbook/book.rb +25 -25
  40. data/lib/workbook/cell.rb +33 -33
  41. data/lib/workbook/format.rb +10 -10
  42. data/lib/workbook/modules/raw_objects_storage.rb +19 -19
  43. data/lib/workbook/modules/table_diff_sort.rb +22 -22
  44. data/lib/workbook/modules/type_parser.rb +18 -18
  45. data/lib/workbook/nil_value.rb +6 -6
  46. data/lib/workbook/readers/csv_reader.rb +8 -8
  47. data/lib/workbook/readers/ods_reader.rb +92 -46
  48. data/lib/workbook/readers/txt_reader.rb +2 -2
  49. data/lib/workbook/readers/xls_reader.rb +19 -19
  50. data/lib/workbook/readers/xls_shared.rb +70 -70
  51. data/lib/workbook/readers/xlsx_reader.rb +42 -42
  52. data/lib/workbook/row.rb +59 -29
  53. data/lib/workbook/sheet.rb +8 -8
  54. data/lib/workbook/table.rb +52 -19
  55. data/lib/workbook/template.rb +10 -10
  56. data/lib/workbook/version.rb +1 -1
  57. data/lib/workbook/writers/csv_table_writer.rb +1 -1
  58. data/lib/workbook/writers/html_writer.rb +6 -6
  59. data/lib/workbook/writers/xls_writer.rb +12 -12
  60. data/rbeautify.rb +232 -0
  61. data/test/artifacts/book_with_colspans.ods +0 -0
  62. data/test/artifacts/book_with_tabs_and_colours.ods +0 -0
  63. data/test/test_book.rb +10 -10
  64. data/test/test_cell.rb +14 -14
  65. data/test/test_format.rb +11 -11
  66. data/test/test_functional.rb +3 -3
  67. data/test/test_modules_table_diff_sort.rb +24 -24
  68. data/test/test_modules_type_parser.rb +27 -27
  69. data/test/test_readers_csv_reader.rb +11 -11
  70. data/test/test_readers_ods_reader.rb +22 -15
  71. data/test/test_readers_txt_reader.rb +13 -13
  72. data/test/test_readers_xls_reader.rb +11 -11
  73. data/test/test_readers_xlsx_reader.rb +5 -5
  74. data/test/test_row.rb +96 -26
  75. data/test/test_sheet.rb +9 -9
  76. data/test/test_table.rb +42 -26
  77. data/test/test_template.rb +3 -3
  78. data/test/test_writers_html_writer.rb +6 -4
  79. data/test/test_writers_xls_writer.rb +7 -7
  80. metadata +5 -1
@@ -103,7 +103,7 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Sun May 5 14:58:04 2013 by
106
+ Generated on Sun May 19 23:16:28 2013 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
108
  0.8.5.2 (ruby-1.9.3).
109
109
  </div>
data/lib/workbook/book.rb CHANGED
@@ -9,9 +9,9 @@ require 'workbook/readers/csv_reader'
9
9
  require 'workbook/readers/txt_reader'
10
10
 
11
11
  module Workbook
12
- # The Book class is the container of sheets. It can be inialized by either the standard initalizer or the open method. The
12
+ # The Book class is the container of sheets. It can be inialized by either the standard initalizer or the open method. The
13
13
  # Book class can also keep a reference to a template class, storing shared formatting options.
14
- #
14
+ #
15
15
  class Book < Array
16
16
  include Workbook::Readers::XlsShared
17
17
  include Workbook::Writers::XlsWriter
@@ -21,12 +21,12 @@ module Workbook
21
21
  include Workbook::Readers::XlsxReader
22
22
  include Workbook::Readers::CsvReader
23
23
  include Workbook::Readers::TxtReader
24
-
24
+
25
25
  attr_accessor :title
26
26
  attr_accessor :template
27
-
27
+
28
28
  # @param [Workbook::Sheet, Array] sheet create a new workbook based on an existing sheet, or initialize a sheet based on the array
29
- # @return [Workbook::Book]
29
+ # @return [Workbook::Book]
30
30
  def initialize sheet=Workbook::Sheet.new([], self, options={})
31
31
  if sheet.is_a? Workbook::Sheet
32
32
  push sheet
@@ -34,32 +34,32 @@ module Workbook
34
34
  push Workbook::Sheet.new(sheet, self, options)
35
35
  end
36
36
  end
37
-
37
+
38
38
  # @return [Workbook::Format] returns the template describing how the document should be/is formatted
39
39
  def template
40
40
  @template ||= Workbook::Template.new
41
41
  end
42
-
42
+
43
43
  # @param [Workbook::Format] template a template describing how the document should be/is formatted
44
44
  def template= template
45
45
  raise ArgumentError, "format should be a Workboot::Format" unless template.is_a? Workbook::Template
46
46
  @template = template
47
47
  end
48
-
48
+
49
49
  # The title of the workbook
50
50
  #
51
51
  # @return [String] the title of the workbook
52
52
  def title
53
53
  @title ? @title : "untitled document"
54
54
  end
55
-
55
+
56
56
  # Push (like in array) a sheet to the workbook (parameter is optional, default is a new sheet)
57
57
  #
58
58
  # @param [Workbook::Sheet] sheet
59
- def push sheet=Workbook::Sheet.new
59
+ def push sheet=Workbook::Sheet.new
60
60
  super(sheet)
61
61
  end
62
-
62
+
63
63
  # Sheet returns the first sheet of a workbook, or an empty one.
64
64
  #
65
65
  # @return [Workbook::Sheet] The first sheet, and creates an empty one if one doesn't exists
@@ -67,14 +67,14 @@ module Workbook
67
67
  push Workbook::Sheet.new unless first
68
68
  first
69
69
  end
70
-
70
+
71
71
  # If the first sheet has any contents
72
72
  #
73
73
  # @return [Boolean] returns true if the first sheet has contents
74
74
  def has_contents?
75
75
  sheet.has_contents?
76
76
  end
77
-
77
+
78
78
  # Loads an external file into an existing worbook
79
79
  #
80
80
  # @param [String] filename a string with a reference to the file to be opened
@@ -88,7 +88,7 @@ module Workbook
88
88
  open_binary filename, extension
89
89
  end
90
90
  end
91
-
91
+
92
92
  # Open the file in binary, read-only mode, do not read it, but pas it throug to the extension determined loaded
93
93
  #
94
94
  # @param [String] filename a string with a reference to the file to be opened
@@ -99,7 +99,7 @@ module Workbook
99
99
  f = File.open(filename,'rb')
100
100
  send("load_#{extension}".to_sym,f)
101
101
  end
102
-
102
+
103
103
  # Open the file in non-binary, read-only mode, read it and parse it to UTF-8
104
104
  #
105
105
  # @param [String] filename a string with a reference to the file to be opened
@@ -111,14 +111,14 @@ module Workbook
111
111
  t = text_to_utf8(t)
112
112
  send("load_#{extension}".to_sym,t)
113
113
  end
114
-
115
-
114
+
115
+
116
116
  # Helper method to convert text in a file to UTF-8
117
- #
117
+ #
118
118
  # @param [String] text a string to convert
119
119
  def text_to_utf8 text
120
120
  if RUBY_VERSION < '1.9'
121
- require 'rchardet'
121
+ require 'rchardet'
122
122
  require 'iconv'
123
123
  detected_encoding = CharDet.detect(text)
124
124
  detected_encoding = detected_encoding['encoding']
@@ -134,14 +134,14 @@ module Workbook
134
134
  end
135
135
  return text
136
136
  end
137
-
137
+
138
138
  # @param [String] filename The full filename, or path
139
139
  #
140
140
  # @return [String] The file extension
141
141
  def file_extension(filename)
142
142
  File.extname(filename).gsub('.','').downcase if filename
143
143
  end
144
-
144
+
145
145
  # Create an instance from a file, using open.
146
146
  #
147
147
  # @param [String] filename of the document
@@ -163,7 +163,7 @@ module Workbook
163
163
  wb.read(stringio_or_string, filetype)
164
164
  wb
165
165
  end
166
-
166
+
167
167
  # Load the CSV data contained in the given StringIO or String object
168
168
  #
169
169
  # @param [StringIO] stringio_or_string StringIO stream or String object, with data in CSV format
@@ -176,14 +176,14 @@ module Workbook
176
176
  end
177
177
 
178
178
  # Create or open the existing sheet at an index value
179
- #
179
+ #
180
180
  # @param [Integer] index the index of the sheet
181
181
  def create_or_open_sheet_at index
182
182
  s = self[index]
183
183
  s = self[index] = Workbook::Sheet.new if s == nil
184
184
  s.book = self
185
- s
185
+ s
186
186
  end
187
-
187
+
188
188
  end
189
189
  end
data/lib/workbook/cell.rb CHANGED
@@ -5,37 +5,37 @@ require 'workbook/modules/type_parser'
5
5
  module Workbook
6
6
  class Cell
7
7
  include Workbook::Modules::TypeParser
8
-
8
+
9
9
  attr_accessor :value
10
10
  attr_accessor :format
11
11
  attr_accessor :formula
12
12
  attr_accessor :colspan
13
13
  attr_accessor :rowspan
14
-
14
+
15
15
  # Note that these types are sorted by 'importance'
16
16
  VALID_TYPES = [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass,Workbook::NilValue]
17
-
17
+
18
18
  # Evaluates a value for class-validity
19
19
  #
20
20
  # @param [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass,Object] value the value to evaluate
21
21
  # @return [Boolean] returns true when the value is a valid cell value
22
22
  def valid_value? value
23
- valid_type = false
24
- VALID_TYPES.each {|t| return true if value.is_a? t}
23
+ valid_type = false
24
+ VALID_TYPES.each {|t| return true if value.is_a? t}
25
25
  valid_type
26
26
  end
27
-
27
+
28
28
  # @param [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass] value a valid value
29
29
  # @param [Hash] options a reference to :format (Workbook::Format) can be specified
30
30
  def initialize value=nil, options={}
31
31
  if valid_value? value
32
- format = options[:format]
32
+ format = options[:format]
33
33
  @value = value
34
34
  else
35
35
  raise ArgumentError, "value should be of a primitive type, e.g. a string, or an integer, not a #{value.class} (is_a? [TrueClass,FalseClass,Date,Time,Numeric,String, NilClass])"
36
36
  end
37
37
  end
38
-
38
+
39
39
  # @param [Workbook::Format, Hash] f set the formatting properties of this Cell
40
40
  def format= f
41
41
  if f.is_a? Workbook::Format
@@ -46,7 +46,7 @@ module Workbook
46
46
  @format = Workbook::Format.new
47
47
  end
48
48
  end
49
-
49
+
50
50
  def format
51
51
  @format ||= Workbook::Format.new
52
52
  end
@@ -60,16 +60,16 @@ module Workbook
60
60
  other == self.value
61
61
  end
62
62
  end
63
-
63
+
64
64
  # returns true when the value of the cell is nil.
65
65
  # @return [Boolean]
66
66
  def nil?
67
67
  return value.nil?
68
68
  end
69
-
69
+
70
70
  # returns a symbol representation of the cell's value
71
71
  # @return [Symbol] a symbol representation
72
- # @example
72
+ # @example
73
73
  #
74
74
  # <Workbook::Cell value="yet another value">.to_sym # returns :yet_another_value
75
75
  def to_sym
@@ -79,19 +79,19 @@ module Workbook
79
79
  v = value.to_s.downcase
80
80
  v = v.gsub(' (j/?/leeg)','').gsub(/dd-mm-(.*)/,'').gsub(/\ja\/nee/,'').gsub(/\(\)/,'').gsub(/[\(\)]+/, '')
81
81
  v = v.strip.gsub(/(\.|\?|,|\=)/,'').
82
- gsub('$','').
83
- gsub(/\&/,'en').
84
- gsub(/\+/,'_plus_').
85
- gsub(/\s/, "_").
86
- gsub('–_','').
87
- gsub('-_','').
88
- gsub('+_','').
89
- gsub('/_','_').
90
- gsub('/','_').
91
- gsub('__','_').
92
- gsub('-','')
93
-
94
- accents = {
82
+ gsub('$','').
83
+ gsub(/\&/,'en').
84
+ gsub(/\+/,'_plus_').
85
+ gsub(/\s/, "_").
86
+ gsub('–_','').
87
+ gsub('-_','').
88
+ gsub('+_','').
89
+ gsub('/_','_').
90
+ gsub('/','_').
91
+ gsub('__','_').
92
+ gsub('-','')
93
+
94
+ accents = {
95
95
  ['á','à','â','ä','ã'] => 'a',
96
96
  ['Ã','Ä','Â','À','�?'] => 'A',
97
97
  ['é','è','ê','ë'] => 'e',
@@ -121,7 +121,7 @@ module Workbook
121
121
  end
122
122
  v
123
123
  end
124
-
124
+
125
125
  # @param [Workbook::Cell] other cell to compare against, can compare different value-types using #compare_on_class
126
126
  def <=> other
127
127
  rv = nil
@@ -130,13 +130,13 @@ module Workbook
130
130
  rescue NoMethodError => e
131
131
  rv = compare_on_class other
132
132
  end
133
- if rv == nil
133
+ if rv == nil
134
134
  rv = compare_on_class other
135
135
  end
136
136
  return rv
137
137
 
138
138
  end
139
-
139
+
140
140
  # Compare on class level
141
141
  #
142
142
  # @param [Workbook::Cell] other cell to compare against
@@ -147,21 +147,21 @@ module Workbook
147
147
  other_value = importance_of_class other_value
148
148
  self_value <=> other_value
149
149
  end
150
-
150
+
151
151
  # Returns the importance of a value's class
152
152
  #
153
153
  # @param value a potential value for a cell
154
154
  def importance_of_class value
155
- VALID_TYPES.each_with_index do |c,i|
155
+ VALID_TYPES.each_with_index do |c,i|
156
156
  return i if value.is_a? c
157
157
  end
158
158
  return nil
159
159
  end
160
-
160
+
161
161
  def inspect
162
162
  "<Workbook::Cell @value=#{value}>"
163
163
  end
164
-
164
+
165
165
  # convert value to string, and in case of a Date or Time value, apply formatting
166
166
  # @return [String]
167
167
  def to_s
@@ -171,7 +171,7 @@ module Workbook
171
171
  value.to_s
172
172
  end
173
173
  end
174
-
174
+
175
175
  def colspan
176
176
  @colspan.to_i if @colspan.to_i > 1
177
177
  end
@@ -8,14 +8,14 @@ module Workbook
8
8
  alias_method :merge_hash, :merge
9
9
  alias_method :merge_hash!, :merge!
10
10
  attr_accessor :name, :parent
11
-
11
+
12
12
  # Initialize
13
13
  # @param [Workbook::Format, Hash] options (e.g. :background, :color
14
14
  def initialize options={}, name=nil
15
15
  options.each {|k,v| self[k]=v}
16
16
  self.name = name
17
17
  end
18
-
18
+
19
19
  # Does the current format feature a background *color*? (not black or white or transparant).
20
20
  def has_background_color? color=:any
21
21
  if flattened[:background_color]
@@ -24,7 +24,7 @@ module Workbook
24
24
  return false
25
25
  end
26
26
  end
27
-
27
+
28
28
  # Returns a string that can be used as inline cell styling (e.g. `<td style="<%=cell.format.to_css%>"><%=cell%></td>`)
29
29
  # @return String very basic CSS styling string
30
30
  def to_css
@@ -34,7 +34,7 @@ module Workbook
34
34
  css_parts.push("color: #{flattened[:color].to_s}") if flattened[:color]
35
35
  css_parts.join("; ")
36
36
  end
37
-
37
+
38
38
  # Combines the formatting options of one with another, removes as a consequence the reference to the raw object's equivalent.
39
39
  # @param [Workbook::Format] other_format
40
40
  # @return [Workbook::Format] a new resulting Workbook::Format
@@ -42,15 +42,15 @@ module Workbook
42
42
  self.remove_all_raws!
43
43
  self.merge_hash(other_format)
44
44
  end
45
-
45
+
46
46
  # Applies the formatting options of self with another, removes as a consequence the reference to the raw object's equivalent.
47
- # @param [Workbook::Format] other_format
47
+ # @param [Workbook::Format] other_format
48
48
  # @return [Workbook::Format] self
49
49
  def merge!(other_format)
50
50
  self.remove_all_raws!
51
51
  self.merge_hash!(other_format)
52
52
  end
53
-
53
+
54
54
  # returns an array of all formats this style is inheriting from (including itself)
55
55
  # @return [Array<Workbook::Format>] an array of Workbook::Formats
56
56
  def formats
@@ -63,13 +63,13 @@ module Workbook
63
63
  end
64
64
  formats.reverse
65
65
  end
66
-
66
+
67
67
  # returns an array of all format-names this style is inheriting from (and this style)
68
68
  # @return [Array<String>] an array of Workbook::Formats
69
- def all_names
69
+ def all_names
70
70
  formats.collect{|a| a.name}
71
71
  end
72
-
72
+
73
73
  # Applies the formatting options of self with its parents until no parent can be found
74
74
  # @return [Workbook::Format] new Workbook::Format that is the result of merging current style with all its parent's styles.
75
75
  def flattened
@@ -1,37 +1,37 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Workbook
3
- module Modules
3
+ module Modules
4
4
  # Adds support for storing raw objects, used in e.g. Format and Template
5
- module RawObjectsStorage
6
-
7
- # A raw is a 'raw' object, representing a workbook, or cell, or whatever... in a particular format (defined by its class)
5
+ module RawObjectsStorage
6
+
7
+ # A raw is a 'raw' object, representing a workbook, or cell, or whatever... in a particular format (defined by its class)
8
8
  def add_raw raw_object
9
- raws[raw_object.class]=raw_object
10
- end
11
-
9
+ raws[raw_object.class]=raw_object
10
+ end
11
+
12
12
  # Returns true if there is a template for a certain class, otherwise false
13
- def has_raw_for? raw_object_class
14
- raws.each { |tc,t| return true if tc == raw_object_class}
13
+ def has_raw_for? raw_object_class
14
+ raws.each { |tc,t| return true if tc == raw_object_class}
15
15
  return false
16
- end
17
-
16
+ end
17
+
18
18
  # Returns raw data stored for a type of raw object (if available)
19
19
  # @param [Class] raw_object_class (e.g. Spreadsheet::Format for the Spreadsheet-gem)
20
20
  def return_raw_for raw_object_class
21
- raws.each { |tc,t| return t if tc == raw_object_class}
21
+ raws.each { |tc,t| return t if tc == raw_object_class}
22
22
  return nil
23
- end
24
-
23
+ end
24
+
25
25
  # Remove all raw data references
26
26
  def remove_all_raws!
27
27
  @raws = {}
28
28
  end
29
-
29
+
30
30
  # Return all raw data references
31
- def raws
31
+ def raws
32
32
  @raws = {} unless defined? @raws
33
33
  @raws
34
- end
35
- end
36
- end
34
+ end
35
+ end
36
+ end
37
37
  end