workbook 0.4.9 → 0.4.10

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
  SHA1:
3
- metadata.gz: c4b33014d1a79259654ae669373a886007f494f5
4
- data.tar.gz: 6385dfbbe2965a2d4b59aa597ad9875040aa5226
3
+ metadata.gz: ca5f6da9acf713dbf71a56c795e93c951d0838d3
4
+ data.tar.gz: a2252dd8e37faaae531689cb2d7de383d3771994
5
5
  SHA512:
6
- metadata.gz: e2f9ee0754579c29154b399ab4e868bc1a784b71f8cb61487606210ee3fb2b948b72d1182576071945e14574b7b99b087ef36f927d577a2add013546f280991f
7
- data.tar.gz: 5797ba6fa5d14e6259a6a33ab45d1b228afae3eb9c99a8a2633856821978846f3e93f6e98173a085ee00bdf66b8ffff00b9f5411510f2aca981fede53ba1a3bc
6
+ metadata.gz: be613dc8479a2a1d4106b214004cf1274fa9784b4a43cca0ee3fd0e942211a12662abc2b7acf56346ce517fefcabecd51a942db2d48159de2bb7b3e540d8589e
7
+ data.tar.gz: d331c6f871214606845fdb3377db3b65265794154b5b9ad66f1a242ee74cc97f119aee2a8e6f558a1e34fb65697f291039486ff56bfef2038d2b2ca03e840a8c
data/.travis.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.0.0
5
4
  - 2.1.0
5
+ - 2.2.0
6
6
  # - jruby-19mode
data/README.md CHANGED
@@ -144,7 +144,7 @@ The [ruby toolbox lists plenty of alternatives](https://www.ruby-toolbox.com/sea
144
144
 
145
145
  ## License
146
146
 
147
- This code MIT (but see below) (c) murb / Maarten Brouwers, 2011-2013
147
+ This code MIT (but see below) © murb / Maarten Brouwers, 2011-2015
148
148
 
149
149
  Workbook uses the following gems:
150
150
 
@@ -152,7 +152,6 @@ Workbook uses the following gems:
152
152
  * [ruby-ole](http://code.google.com/p/ruby-ole/) Used in the Spreadsheet Gem (Copyright © 2007-2010 Charles Lowe; MIT)
153
153
  * [FasterCSV](http://fastercsv.rubyforge.org/) Used for reading CSV (comma separated text) and TXT (tab separated text) files (Copyright © James Edward Gray II; GPL2 & Ruby License)
154
154
  * [rchardet](http://rubyforge.org/projects/rchardet) Used for detecting encoding in CSV and TXT importers (Copyright © JMHodges; LGPL)
155
- * [RubyXL](https://github.com/gilt/rubyXL) Used for reading the newer .xlsx files (Copyright © 2011 Vivek Bhagwat; MIT-Licensed)
156
- * [Nokogiri](http://nokogiri.org/) Used for reading ODS documents (Copyright © 2008 - 2012 Aaron Patterson, Mike Dalessio, Charles Nutter, Sergio Arbeo, Patrick Mahoney, Yoko Harada; MIT Licensed)
157
-
158
-
155
+ * [roo](https://github.com/roo-rb/roo) Temporarily used for reading the newer .xlsx files (without formatting) (Copyright © 2008-2014 Thomas Preymesser, Ben Woosley, MIT License)
156
+ * [axslx](https://github.com/randym/axlsx) Used for writing the newer .xlsx files (with formatting) (Copyright © 2011, 2012 Randy Morgan, MIT License)
157
+ * [Nokogiri](http://nokogiri.org/) Used for reading ODS documents (Copyright © 2008 - 2012 Aaron Patterson, Mike Dalessio, Charles Nutter, Sergio Arbeo, Patrick Mahoney, Yoko Harada; MIT Licensed)
data/lib/workbook.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  $KCODE="u" if RUBY_VERSION < "1.9"
3
3
  require_relative 'workbook/modules/cache'
4
+ require_relative 'workbook/modules/cell'
5
+ require_relative 'workbook/types/date'
4
6
  require_relative 'workbook/book'
5
7
  require_relative 'workbook/sheet'
6
8
  require_relative 'workbook/table'
data/lib/workbook/cell.rb CHANGED
@@ -1,218 +1,9 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
- require 'workbook/modules/type_parser'
3
+ require 'workbook/modules/cell'
4
4
 
5
5
  module Workbook
6
6
  class Cell
7
- include Workbook::Modules::TypeParser
8
-
9
- attr_accessor :formula
10
-
11
- # Note that these types are sorted by 'importance'
12
- VALID_TYPES = [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass,Workbook::NilValue,Symbol]
13
-
14
- # Evaluates a value for class-validity
15
- #
16
- # @param [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass,Object] value the value to evaluate
17
- # @return [Boolean] returns true when the value is a valid cell value
18
- def valid_value? value
19
- valid_type = false
20
- VALID_TYPES.each {|t| return true if value.is_a? t}
21
- valid_type
22
- end
23
-
24
- # @param [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass] value a valid value
25
- # @param [Hash] options a reference to :format (Workbook::Format) can be specified
26
- def initialize value=nil, options={}
27
- self.format = options[:format]
28
- self.value = value
29
- @to_sym = nil
30
- end
31
-
32
- # Change the current value
33
- #
34
- # @param [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass,Symbol] value a valid value
35
- def value= value
36
- if valid_value? value
37
- @value = value
38
- @to_sym = nil
39
- else
40
- 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, Symbol])"
41
- end
42
- end
43
-
44
- # Returns the current value
45
- #
46
- # @return [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass] a valid value
47
- def value
48
- @value
49
- end
50
-
51
- # Change the current format
52
- #
53
- # @param [Workbook::Format, Hash] f set the formatting properties of this Cell
54
- def format= f
55
- if f.is_a? Workbook::Format
56
- @format = f
57
- elsif f.is_a? Hash
58
- @format = Workbook::Format.new(f)
59
- elsif f.class == NilClass
60
- @format = Workbook::Format.new
61
- end
62
- end
63
-
64
- # Returns current format
65
- #
66
- # @returns [Workbook::Format] the current format
67
- def format
68
- @format ||= Workbook::Format.new
69
- end
70
-
71
- # Tests for equality based on its value (formatting is irrelevant)
72
- #
73
- # @param [Workbook::Cell] other cell to compare against
74
- # @return [Boolean]
75
- def ==(other)
76
- if other.is_a? Cell
77
- other.value == self.value
78
- else
79
- other == self.value
80
- end
81
- end
82
-
83
- # returns true when the value of the cell is nil.
84
- # @return [Boolean]
85
- def nil?
86
- return value.nil?
87
- end
88
-
89
- # returns a symbol representation of the cell's value
90
- # @return [Symbol] a symbol representation
91
- # @example
92
- #
93
- # <Workbook::Cell value="yet another value">.to_sym # returns :yet_another_value
94
- def to_sym
95
- return @to_sym if @to_sym
96
- v = nil
97
- if value
98
- ends_with_exclamationmark = (value[-1] == '!')
99
- ends_with_questionmark = (value[-1] == '?')
100
- v = value.to_s.downcase
101
-
102
- replacements = {
103
- [/[\(\)\.\?\,\!\=\$\:]/,] => '',
104
- [/\&/] => 'amp',
105
- [/\+/] => '_plus_',
106
- [/\s/,'/_','/',"\\"] => '_',
107
- ['–_','-_','+_','-'] => '',
108
- ['__']=>'_',
109
- ['>']=>'gt',
110
- ['<']=>'lt',
111
- ['á','à','â','ä','ã','å'] => 'a',
112
- ['Ã','Ä','Â','À','�?','Å'] => 'A',
113
- ['é','è','ê','ë'] => 'e',
114
- ['Ë','É','È','Ê'] => 'E',
115
- ['í','ì','î','ï'] => 'i',
116
- ['�?','Î','Ì','�?'] => 'I',
117
- ['ó','ò','ô','ö','õ'] => 'o',
118
- ['Õ','Ö','Ô','Ò','Ó'] => 'O',
119
- ['ú','ù','û','ü'] => 'u',
120
- ['Ú','Û','Ù','Ü'] => 'U',
121
- ['ç'] => 'c',
122
- ['Ç'] => 'C',
123
- ['š', 'ś'] => 's',
124
- ['Š', 'Ś'] => 'S',
125
- ['ž','ź'] => 'z',
126
- ['Ž','Ź'] => 'Z',
127
- ['ñ'] => 'n',
128
- ['Ñ'] => 'N',
129
- ['#'] => 'hash',
130
- ['*'] => 'asterisk'
131
- }
132
- replacements.each do |ac,rep|
133
- ac.each do |s|
134
- v = v.gsub(s, rep)
135
- end
136
- end
137
- if RUBY_VERSION < '1.9'
138
- v = v.gsub(/[^\x00-\x7F]/n,'')
139
- else
140
- # See String#encode
141
- encoding_options = {:invalid => :replace, :undef => :replace, :replace => ''}
142
- v = v.encode(Encoding.find('ASCII'), encoding_options)
143
- end
144
- v = "#{v}!" if ends_with_exclamationmark
145
- v = "#{v}?" if ends_with_questionmark
146
- v = v.downcase.to_sym
147
- end
148
- @to_sym = v
149
- return @to_sym
150
- end
151
-
152
- # Compare
153
- # @param [Workbook::Cell] other cell to compare against (based on value), can compare different value-types using #compare_on_class
154
- # @returns [Fixnum] -1, 0, 1
155
- def <=> other
156
- rv = nil
157
- begin
158
- rv = self.value <=> other.value
159
- rescue NoMethodError
160
- rv = compare_on_class other
161
- end
162
- if rv == nil
163
- rv = compare_on_class other
164
- end
165
- return rv
166
-
167
- end
168
-
169
- # Compare on class level
170
- #
171
- # @param [Workbook::Cell] other cell to compare against
172
- def compare_on_class other
173
- other_value = nil
174
- other_value = other.value if other
175
- self_value = importance_of_class self.value
176
- other_value = importance_of_class other_value
177
- self_value <=> other_value
178
- end
179
-
180
- # Returns the importance of a value's class
181
- #
182
- # @param value a potential value for a cell
183
- def importance_of_class value
184
- VALID_TYPES.each_with_index do |c,i|
185
- return i if value.is_a? c
186
- end
187
- return nil
188
- end
189
-
190
- def inspect
191
- "<Workbook::Cell @value=#{value}>"
192
- end
193
-
194
- # convert value to string, and in case of a Date or Time value, apply formatting
195
- # @return [String]
196
- def to_s
197
- if (value.is_a? Date or value.is_a? Time) and format[:number_format]
198
- value.strftime(format[:number_format])
199
- else
200
- value.to_s
201
- end
202
- end
203
-
204
- def colspan= c
205
- @colspan = c
206
- end
207
- def rowspan= r
208
- @rowspan = r
209
- end
210
-
211
- def colspan
212
- @colspan.to_i if defined?(@colspan) and @colspan.to_i > 1
213
- end
214
- def rowspan
215
- @rowspan.to_i if defined?(@rowspan) and @rowspan.to_i > 1
216
- end
7
+ include Workbook::Modules::Cell
217
8
  end
218
9
  end
@@ -12,7 +12,25 @@ module Workbook
12
12
 
13
13
  # Returns column type, either :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :date, :binary, :boolean
14
14
  def column_type
15
- @column_type
15
+ return @column_type if @column_type
16
+ ind = self.index
17
+ table[1..500].each do |row|
18
+ if row[ind] and row[ind].cell_type
19
+ cel_column_type = row[ind].cell_type
20
+ if cel_column_type == @column_type or @column_type.nil?
21
+ @column_type = cel_column_type
22
+ else
23
+ @column_type = :string
24
+ end
25
+ end
26
+ end
27
+ return @column_type
28
+ end
29
+
30
+ # Returns index of the column within the table's columns-set
31
+ # @return [Integer, NilClass]
32
+ def index
33
+ table.columns.index self
16
34
  end
17
35
 
18
36
  def table= t
@@ -29,6 +47,18 @@ module Workbook
29
47
  end
30
48
  end
31
49
 
50
+ def head_value
51
+ begin
52
+ table.header[index].value
53
+ rescue
54
+ return "!noheader!"
55
+ end
56
+ end
57
+
58
+ def inspect
59
+ "<Workbook::Column index=#{index}, header=#{head_value}>"
60
+ end
61
+
32
62
  #default cell
33
63
  def default
34
64
  return @default
@@ -0,0 +1,279 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'workbook/modules/type_parser'
3
+ require 'workbook/nil_value'
4
+
5
+ module Workbook
6
+ module Modules
7
+ module Cell
8
+ include Workbook::Modules::TypeParser
9
+
10
+
11
+
12
+ # Note that these types are sorted by 'importance'
13
+ VALID_TYPES = [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass,Workbook::NilValue,Symbol]
14
+
15
+ # Evaluates a value for class-validity
16
+ #
17
+ # @param [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass,Object] value the value to evaluate
18
+ # @return [Boolean] returns true when the value is a valid cell value
19
+ def valid_value? value
20
+ valid_type = false
21
+ VALID_TYPES.each {|t| return true if value.is_a? t}
22
+ valid_type
23
+ end
24
+
25
+ def formula
26
+ @formula
27
+ end
28
+
29
+ def formula= f
30
+ @formula = f
31
+ end
32
+
33
+ def row
34
+ @row
35
+ end
36
+
37
+ def row= r
38
+ @row= r
39
+ end
40
+
41
+ # @param [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass] value a valid value
42
+ # @param [Hash] options a reference to :format (Workbook::Format) can be specified
43
+ def initialize value=nil, options={}
44
+ self.format = options[:format] if options[:format]
45
+ self.row = options[:row]
46
+ self.value = value
47
+ @to_sym = nil
48
+ end
49
+
50
+ # Change the current value
51
+ #
52
+ # @param [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass,Symbol] value a valid value
53
+ def value= value
54
+ if valid_value? value
55
+ @value = value
56
+ @to_sym = nil
57
+ else
58
+ 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, Symbol])"
59
+ end
60
+ end
61
+
62
+ # Returns column type, either :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :date, :binary, :boolean
63
+ #
64
+ # @return [Symbol] the type of cell, compatible with Workbook::Column'types
65
+ def cell_type
66
+ tp = case value.class.to_s
67
+ when "String" then :string
68
+ when "FalseClass" then :boolean
69
+ when "TrueClass" then :boolean
70
+ when 'Time' then :time
71
+ when "Date" then :date
72
+ when "DateTime" then :datetime
73
+ when "Float" then :float
74
+ when "Integer" then :integer
75
+ when "Numeric" then :integer
76
+ when "Fixnum" then :integer
77
+ when "Symbol" then :string
78
+ end
79
+ end
80
+
81
+
82
+ # Returns the current value
83
+ #
84
+ # @return [Numeric,String,Time,Date,TrueClass,FalseClass,NilClass] a valid value
85
+ def value
86
+ @value
87
+ end
88
+
89
+ # Returns the sheet its at.
90
+ #
91
+ # @return [Workbook::Table]
92
+ def table
93
+ row.table if row
94
+ end
95
+
96
+ # Quick assessor to the book's template, if it exists
97
+ #
98
+ # @return [Workbook::Template]
99
+ def template
100
+ row.template if row
101
+ end
102
+
103
+ # Change the current format
104
+ #
105
+ # @param [Workbook::Format, Hash] f set the formatting properties of this Cell
106
+ def format= f
107
+ if f.is_a? Workbook::Format
108
+ @workbook_format = f
109
+ elsif f.is_a? Hash
110
+ @workbook_format = Workbook::Format.new(f)
111
+ elsif f.class == NilClass
112
+ @workbook_format = Workbook::Format.new
113
+ end
114
+ end
115
+
116
+ # Returns current format
117
+ #
118
+ # @returns [Workbook::Format] the current format
119
+ def format
120
+ # return @workbook_format if @workbook_format
121
+ if row and template and row.header? and !@workbook_format
122
+ @workbook_format = template.create_or_find_format_by(:header)
123
+ else
124
+ @workbook_format ||= Workbook::Format.new
125
+ end
126
+ @workbook_format
127
+ end
128
+
129
+ # Tests for equality based on its value (formatting is irrelevant)
130
+ #
131
+ # @param [Workbook::Cell] other cell to compare against
132
+ # @return [Boolean]
133
+ def ==(other)
134
+ if other.is_a? Cell
135
+ other.value == self.value
136
+ else
137
+ other == self.value
138
+ end
139
+ end
140
+
141
+ # returns true when the value of the cell is nil.
142
+ # @return [Boolean]
143
+ def nil?
144
+ return value.nil?
145
+ end
146
+
147
+ # returns a symbol representation of the cell's value
148
+ # @return [Symbol] a symbol representation
149
+ # @example
150
+ #
151
+ # <Workbook::Cell value="yet another value">.to_sym # returns :yet_another_value
152
+ def to_sym
153
+ return @to_sym if @to_sym
154
+ v = nil
155
+ if value
156
+ ends_with_exclamationmark = (value[-1] == '!')
157
+ ends_with_questionmark = (value[-1] == '?')
158
+ v = value.to_s.downcase
159
+
160
+ replacements = {
161
+ [/[\(\)\.\?\,\!\=\$\:]/,] => '',
162
+ [/\&/] => 'amp',
163
+ [/\+/] => '_plus_',
164
+ [/\s/,'/_','/',"\\"] => '_',
165
+ ['–_','-_','+_','-'] => '',
166
+ ['__']=>'_',
167
+ ['>']=>'gt',
168
+ ['<']=>'lt',
169
+ ['á','à','â','ä','ã','å'] => 'a',
170
+ ['Ã','Ä','Â','À','�?','Å'] => 'A',
171
+ ['é','è','ê','ë'] => 'e',
172
+ ['Ë','É','È','Ê'] => 'E',
173
+ ['í','ì','î','ï'] => 'i',
174
+ ['�?','Î','Ì','�?'] => 'I',
175
+ ['ó','ò','ô','ö','õ'] => 'o',
176
+ ['Õ','Ö','Ô','Ò','Ó'] => 'O',
177
+ ['ú','ù','û','ü'] => 'u',
178
+ ['Ú','Û','Ù','Ü'] => 'U',
179
+ ['ç'] => 'c',
180
+ ['Ç'] => 'C',
181
+ ['š', 'ś'] => 's',
182
+ ['Š', 'Ś'] => 'S',
183
+ ['ž','ź'] => 'z',
184
+ ['Ž','Ź'] => 'Z',
185
+ ['ñ'] => 'n',
186
+ ['Ñ'] => 'N',
187
+ ['#'] => 'hash',
188
+ ['*'] => 'asterisk'
189
+ }
190
+ replacements.each do |ac,rep|
191
+ ac.each do |s|
192
+ v = v.gsub(s, rep)
193
+ end
194
+ end
195
+ if RUBY_VERSION < '1.9'
196
+ v = v.gsub(/[^\x00-\x7F]/n,'')
197
+ else
198
+ # See String#encode
199
+ encoding_options = {:invalid => :replace, :undef => :replace, :replace => ''}
200
+ v = v.encode(Encoding.find('ASCII'), encoding_options)
201
+ end
202
+ v = "#{v}!" if ends_with_exclamationmark
203
+ v = "#{v}?" if ends_with_questionmark
204
+ v = v.downcase.to_sym
205
+ end
206
+ @to_sym = v
207
+ return @to_sym
208
+ end
209
+
210
+ # Compare
211
+ # @param [Workbook::Cell] other cell to compare against (based on value), can compare different value-types using #compare_on_class
212
+ # @returns [Fixnum] -1, 0, 1
213
+ def <=> other
214
+ rv = nil
215
+ begin
216
+ rv = self.value <=> other.value
217
+ rescue NoMethodError
218
+ rv = compare_on_class other
219
+ end
220
+ if rv == nil
221
+ rv = compare_on_class other
222
+ end
223
+ return rv
224
+
225
+ end
226
+
227
+ # Compare on class level
228
+ #
229
+ # @param [Workbook::Cell] other cell to compare against
230
+ def compare_on_class other
231
+ other_value = nil
232
+ other_value = other.value if other
233
+ self_value = importance_of_class self.value
234
+ other_value = importance_of_class other_value
235
+ self_value <=> other_value
236
+ end
237
+
238
+ # Returns the importance of a value's class
239
+ #
240
+ # @param value a potential value for a cell
241
+ def importance_of_class value
242
+ VALID_TYPES.each_with_index do |c,i|
243
+ return i if value.is_a? c
244
+ end
245
+ return nil
246
+ end
247
+
248
+ def inspect
249
+ "<Workbook::Cell @value=#{value}>"
250
+ end
251
+
252
+ # convert value to string, and in case of a Date or Time value, apply formatting
253
+ # @return [String]
254
+ def to_s
255
+ if (self.is_a? Date or self.is_a? Time) and format[:number_format]
256
+ value.strftime(format[:number_format])
257
+ elsif (self.class == Workbook::Cell)
258
+ value.to_s
259
+ else
260
+ super
261
+ end
262
+ end
263
+
264
+ def colspan= c
265
+ @colspan = c
266
+ end
267
+ def rowspan= r
268
+ @rowspan = r
269
+ end
270
+
271
+ def colspan
272
+ @colspan.to_i if defined?(@colspan) and @colspan.to_i > 1
273
+ end
274
+ def rowspan
275
+ @rowspan.to_i if defined?(@rowspan) and @rowspan.to_i > 1
276
+ end
277
+ end
278
+ end
279
+ end