rubyXL 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/Gemfile +16 -0
  2. data/Gemfile.lock +34 -0
  3. data/LICENSE.txt +20 -0
  4. data/README +0 -0
  5. data/README.rdoc +19 -0
  6. data/Rakefile +53 -0
  7. data/VERSION +1 -0
  8. data/lib/.DS_Store +0 -0
  9. data/lib/Hash.rb +60 -0
  10. data/lib/cell.rb +360 -0
  11. data/lib/color.rb +14 -0
  12. data/lib/parser.rb +413 -0
  13. data/lib/private_class.rb +182 -0
  14. data/lib/rubyXL.rb +9 -0
  15. data/lib/test.html +1 -0
  16. data/lib/tests/test.rb +110 -0
  17. data/lib/tests/test10.rb +16 -0
  18. data/lib/tests/test2.rb +118 -0
  19. data/lib/tests/test3.rb +76 -0
  20. data/lib/tests/test4.rb +92 -0
  21. data/lib/tests/test5.rb +90 -0
  22. data/lib/tests/test6.rb +50 -0
  23. data/lib/tests/test7.rb +48 -0
  24. data/lib/tests/test8.rb +12 -0
  25. data/lib/tests/test9.rb +60 -0
  26. data/lib/workbook.rb +336 -0
  27. data/lib/worksheet.rb +1245 -0
  28. data/lib/writer/app_writer.rb +62 -0
  29. data/lib/writer/calc_chain_writer.rb +33 -0
  30. data/lib/writer/content_types_writer.rb +77 -0
  31. data/lib/writer/core_writer.rb +51 -0
  32. data/lib/writer/root_rels_writer.rb +25 -0
  33. data/lib/writer/shared_strings_writer.rb +44 -0
  34. data/lib/writer/styles_writer.rb +376 -0
  35. data/lib/writer/theme_writer.rb +346 -0
  36. data/lib/writer/workbook_rels_writer.rb +59 -0
  37. data/lib/writer/workbook_writer.rb +77 -0
  38. data/lib/writer/worksheet_writer.rb +208 -0
  39. data/lib/zip.rb +20 -0
  40. data/pkg/rubyXL-1.0.4.gem +0 -0
  41. data/rubyXL.gemspec +106 -0
  42. data/spec/lib/cell_spec.rb +359 -0
  43. data/spec/lib/color_spec.rb +14 -0
  44. data/spec/lib/hash_spec.rb +28 -0
  45. data/spec/lib/parser_spec.rb +49 -0
  46. data/spec/lib/workbook_spec.rb +51 -0
  47. data/spec/lib/worksheet_spec.rb +1650 -0
  48. metadata +222 -0
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.0"
12
+ gem "rcov", ">= 0"
13
+ gem "nokogiri", ">= 1.4.4"
14
+ gem "rubyzip", ">= 0.9.4"
15
+ gem "rspec", ">= 1.3.4"
16
+ end
@@ -0,0 +1,34 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.6.0)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ nokogiri (1.4.4)
11
+ rake (0.8.7)
12
+ rcov (0.9.9)
13
+ rspec (2.6.0)
14
+ rspec-core (~> 2.6.0)
15
+ rspec-expectations (~> 2.6.0)
16
+ rspec-mocks (~> 2.6.0)
17
+ rspec-core (2.6.4)
18
+ rspec-expectations (2.6.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.6.0)
21
+ rubyzip (0.9.4)
22
+ shoulda (2.11.3)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (~> 1.0.0)
29
+ jeweler (~> 1.6.0)
30
+ nokogiri (>= 1.4.4)
31
+ rcov
32
+ rspec (>= 1.3.4)
33
+ rubyzip (>= 0.9.4)
34
+ shoulda
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Vivek Bhagwat
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
File without changes
@@ -0,0 +1,19 @@
1
+ = rubyXL
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to rubyXL
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Vivek Bhagwat. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "rubyXL"
18
+ gem.homepage = "http://github.com/vivekbhagwat/rubyXL"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx) Documents}
21
+ gem.description = %Q{rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx) Documents}
22
+ gem.email = "bhagwat.vivek@gmail.com"
23
+ gem.authors = ["Vivek Bhagwat"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "rubyXL #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.4
Binary file
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'nokogiri'
3
+ # modified from http://stackoverflow.com/questions/1230741/convert-a-nokogiri-document-to-a-ruby-hash/1231297#1231297
4
+
5
+ module RubyXL
6
+ # class Hash
7
+ class Hash < ::Hash
8
+ def self.from_xml(xml_io)
9
+ begin
10
+ result = Nokogiri::XML(xml_io)
11
+ return { result.root.name.to_sym => xml_node_to_hash(result.root)}
12
+ rescue Exception => e
13
+ # raise your custom exception here
14
+ end
15
+ end
16
+
17
+ def self.xml_node_to_hash(node)
18
+ # If we are at the root of the document, start the hash
19
+ if node.element?
20
+ result_hash = {}
21
+ if node.attributes != {}
22
+ result_hash[:attributes] = {}
23
+ node.attributes.keys.each do |key|
24
+ result_hash[:attributes][node.attributes[key].name.to_sym] = prepare(node.attributes[key].value)
25
+ end
26
+ end
27
+ if node.children.size > 0
28
+ node.children.each do |child|
29
+ result = xml_node_to_hash(child)
30
+
31
+ if child.name == "text"
32
+ unless child.next_sibling || child.previous_sibling
33
+ return prepare(result)
34
+ end
35
+ elsif result_hash[child.name.to_sym]
36
+ if result_hash[child.name.to_sym].is_a?(Object::Array)
37
+ result_hash[child.name.to_sym] << prepare(result)
38
+ else
39
+ result_hash[child.name.to_sym] = [result_hash[child.name.to_sym]] << prepare(result)
40
+ end
41
+ else
42
+ result_hash[child.name.to_sym] = prepare(result)
43
+ end
44
+ end
45
+
46
+ return result_hash
47
+ else
48
+ return result_hash
49
+ end
50
+ else
51
+ return prepare(node.content.to_s)
52
+ end
53
+ end
54
+
55
+ def self.prepare(data)
56
+ (data.class == String && data.to_i.to_s == data) ? data.to_i : data
57
+ end
58
+ end
59
+ # end
60
+ end
@@ -0,0 +1,360 @@
1
+ module RubyXL
2
+ class Cell < PrivateClass
3
+
4
+ attr_accessor :row, :column, :datatype, :style_index, :value, :formula, :worksheet
5
+ attr_reader :workbook
6
+
7
+ def initialize(worksheet,row,column,value=nil,formula=nil,datatype='s',style_index=0)
8
+ @worksheet = worksheet
9
+
10
+ @workbook = worksheet.workbook
11
+ @row = row
12
+ @column = column
13
+ @datatype = datatype
14
+ @value = value
15
+ @formula=formula
16
+ @style_index = style_index
17
+ end
18
+
19
+ # changes fill color of cell
20
+ def change_fill(rgb='ffffff')
21
+ validate_worksheet
22
+ Color.validate_color(rgb)
23
+ @style_index = modify_fill(@workbook, @style_index,rgb)
24
+ end
25
+
26
+ # changes font name of cell
27
+ def change_font_name(font_name='Verdana')
28
+ validate_worksheet
29
+ @style_index = modify_font(@workbook,@style_index)
30
+ @workbook.fonts[font_id()][:font][:name][:attributes][:val] = font_name.to_s
31
+ end
32
+
33
+ # changes font size of cell
34
+ def change_font_size(font_size=10)
35
+ validate_worksheet
36
+ if font_size.is_a?(Integer) || font_size.is_a?(Float)
37
+ @style_index = modify_font(@workbook, @style_index)
38
+ @workbook.fonts[font_id()][:font][:sz][:attributes][:val] = font_size
39
+ else
40
+ raise 'Argument must be a number'
41
+ end
42
+ end
43
+
44
+ # changes font color of cell
45
+ def change_font_color(font_color='000000')
46
+ validate_worksheet
47
+ #if arg is a color name, convert to integer
48
+ Color.validate_color(font_color)
49
+
50
+ @style_index = modify_font(@workbook,@style_index)
51
+ font_id = font_id()
52
+ if @workbook.fonts[font_id][:font][:color].nil?
53
+ @workbook.fonts[font_id][:font][:color] = {:attributes => {:rgb => ''}}
54
+ end
55
+ @workbook.fonts[font_id][:font][:color][:attributes][:rgb] = font_color.to_s
56
+ end
57
+
58
+ # changes if font is italicized or not
59
+ def change_font_italics(italicized=false)
60
+ validate_worksheet
61
+ @style_index = modify_font(@workbook,@style_index)
62
+ if italicized
63
+ @workbook.fonts[font_id()][:font][:i] = {}
64
+ else
65
+ @workbook.fonts[font_id()][:font][:i] = nil
66
+ end
67
+ end
68
+
69
+ # changes if font is bolded or not
70
+ def change_font_bold(bolded=false)
71
+ validate_worksheet
72
+ @style_index = modify_font(@workbook,@style_index)
73
+ if bolded
74
+ @workbook.fonts[font_id()][:font][:b] = {}
75
+ else
76
+ @workbook.fonts[font_id()][:font][:b] = nil
77
+ end
78
+ end
79
+
80
+ # changes if font is underlined or not
81
+ def change_font_underline(underlined=false)
82
+ validate_worksheet
83
+ @style_index = modify_font(@workbook,@style_index)
84
+
85
+ if underlined
86
+ @workbook.fonts[font_id()][:font][:u] = {}
87
+ else
88
+ @workbook.fonts[font_id()][:font][:u] = nil
89
+ end
90
+ end
91
+
92
+ # changes if font has a strikethrough or not
93
+ def change_font_strikethrough(struckthrough=false)
94
+ validate_worksheet
95
+ @style_index = modify_font(@workbook,@style_index)
96
+
97
+ if struckthrough
98
+ @workbook.fonts[font_id()][:font][:strike] = {}
99
+ else
100
+ @workbook.fonts[font_id()][:font][:strike] = nil
101
+ end
102
+ end
103
+
104
+ # changes horizontal alignment of cell
105
+ def change_horizontal_alignment(alignment='center')
106
+ validate_worksheet
107
+ validate_horizontal_alignment(alignment)
108
+ @style_index = modify_alignment(@workbook,@style_index,true,alignment)
109
+ end
110
+
111
+ # changes vertical alignment of cell
112
+ def change_vertical_alignment(alignment='center')
113
+ validate_worksheet
114
+ validate_vertical_alignment(alignment)
115
+ @style_index = modify_alignment(@workbook,@style_index,false,alignment)
116
+ end
117
+
118
+ # changes top border of cell
119
+ def change_border_top(weight='thin')
120
+ change_border(:top, weight)
121
+ end
122
+
123
+ # changes left border of cell
124
+ def change_border_left(weight='thin')
125
+ change_border(:left, weight)
126
+ end
127
+
128
+ # changes right border of cell
129
+ def change_border_right(weight='thin')
130
+ change_border(:right, weight)
131
+ end
132
+
133
+ # changes bottom border of cell
134
+ def change_border_bottom(weight='thin')
135
+ change_border(:bottom, weight)
136
+ end
137
+
138
+ # changes diagonal border of cell
139
+ def change_border_diagonal(weight='thin')
140
+ change_border(:diagonal, weight)
141
+ end
142
+
143
+ # changes contents of cell, with formula option
144
+ def change_contents(data, formula=nil)
145
+ validate_worksheet
146
+ @datatype='str'
147
+ if (data.is_a?Integer) || (data.is_a?Float)
148
+ @datatype = ''
149
+ end
150
+ @value=data
151
+ @formula=formula
152
+ end
153
+
154
+ # returns if font is italicized
155
+ def is_italicized()
156
+ validate_worksheet
157
+ if @workbook.fonts[font_id()][:font][:i].nil?
158
+ false
159
+ else
160
+ true
161
+ end
162
+ end
163
+
164
+ # returns if font is bolded
165
+ def is_bolded()
166
+ validate_worksheet
167
+ if @workbook.fonts[font_id()][:font][:b].nil?
168
+ false
169
+ else
170
+ true
171
+ end
172
+ end
173
+
174
+ # returns if font is underlined
175
+ def is_underlined()
176
+ validate_worksheet
177
+ xf = @workbook.get_style_attributes(@workbook.get_style(@style_index))
178
+ if @workbook.fonts[font_id()][:font][:u].nil?
179
+ false
180
+ else
181
+ true
182
+ end
183
+ end
184
+
185
+ # returns if font has a strike through it
186
+ def is_struckthrough()
187
+ validate_worksheet
188
+ xf = @workbook.get_style_attributes(@workbook.get_style(@style_index))
189
+ if @workbook.fonts[font_id()][:font][:strike].nil?
190
+ false
191
+ else
192
+ true
193
+ end
194
+ end
195
+
196
+ # returns cell's font name
197
+ def font_name()
198
+ validate_worksheet
199
+ @workbook.fonts[font_id()][:font][:name][:attributes][:val]
200
+ end
201
+
202
+ # returns cell's font size
203
+ def font_size()
204
+ validate_worksheet
205
+ return @workbook.fonts[font_id()][:font][:sz][:attributes][:val]
206
+ end
207
+
208
+ # returns cell's font color
209
+ def font_color()
210
+ validate_worksheet
211
+ if @workbook.fonts[font_id()][:font][:color].nil?
212
+ '000000' #black
213
+ else
214
+ @workbook.fonts[font_id()][:font][:color][:attributes][:rgb]
215
+ end
216
+ end
217
+
218
+ # returns cell's fill color
219
+ def fill_color()
220
+ validate_worksheet
221
+ xf = @workbook.get_style_attributes(@workbook.get_style(@style_index))
222
+ return @workbook.get_fill_color(xf)
223
+ end
224
+
225
+ # returns cell's horizontal alignment
226
+ def horizontal_alignment()
227
+ validate_worksheet
228
+ xf_obj = @workbook.get_style(@style_index)
229
+ if xf_obj[:alignment].nil? || xf_obj[:alignment][:attributes].nil?
230
+ return nil
231
+ end
232
+ xf_obj[:alignment][:attributes][:horizontal].to_s
233
+ end
234
+
235
+ # returns cell's vertical alignment
236
+ def vertical_alignment()
237
+ validate_worksheet
238
+ xf_obj = @workbook.get_style(@style_index)
239
+ if xf_obj[:alignment].nil? || xf_obj[:alignment][:attributes].nil?
240
+ return nil
241
+ end
242
+ xf_obj[:alignment][:attributes][:vertical].to_s
243
+ end
244
+
245
+ # returns cell's top border
246
+ def border_top()
247
+ return get_border(:top)
248
+ end
249
+
250
+ # returns cell's left border
251
+ def border_left()
252
+ return get_border(:left)
253
+ end
254
+
255
+ # returns cell's right border
256
+ def border_right()
257
+ return get_border(:right)
258
+ end
259
+
260
+ # returns cell's bottom border
261
+ def border_bottom()
262
+ return get_border(:bottom)
263
+ end
264
+
265
+ # returns cell's diagonal border
266
+ def border_diagonal()
267
+ return get_border(:diagonal)
268
+ end
269
+
270
+ # returns Excel-style cell string from matrix indices
271
+ def Cell.convert_to_cell(row=0,col=0)
272
+ row_string = (row + 1).to_s #+1 for 0 indexing
273
+ col_string = ''
274
+
275
+ if row < 0 || col < 0
276
+ raise 'Invalid input: cannot convert negative numbers'
277
+ end
278
+
279
+ unless col == 0
280
+ col_length = 1+Integer(Math.log(col) / Math.log(26)) #opposite of 26**
281
+ else
282
+ col_length = 1
283
+ end
284
+
285
+ 1.upto(col_length) do |i|
286
+
287
+ #for the last digit, 0 should mean A. easy way to do this.
288
+ if i == col_length
289
+ col+=1
290
+ end
291
+
292
+ if col >= 26**(col_length-i)
293
+ int_val = col / 26**(col_length-i) #+1 for 0 indexing
294
+ int_val += 64 #converts 1 to A, etc.
295
+
296
+ col_string += int_val.chr
297
+
298
+ #intval multiplier decrements by placeholder, essentially
299
+ #a B subtracts more than an A this way.
300
+ col -= (int_val-64)*26**(col_length-i)
301
+ end
302
+ end
303
+ col_string+row_string
304
+ end
305
+
306
+ private
307
+
308
+ def change_border(direction, weight)
309
+ validate_worksheet
310
+ validate_border(weight)
311
+ @style_index = modify_border(@workbook,@style_index)
312
+ if @workbook.borders[border_id()][:border][direction][:attributes].nil?
313
+ @workbook.borders[border_id()][:border][direction][:attributes] = { :style => nil }
314
+ end
315
+ @workbook.borders[border_id()][:border][direction][:attributes][:style] = weight.to_s
316
+ end
317
+
318
+ def get_border(direction)
319
+ validate_worksheet
320
+
321
+ if @workbook.borders[border_id()][:border][direction][:attributes].nil?
322
+ return nil
323
+ end
324
+ return @workbook.borders[border_id()][:border][direction][:attributes][:style]
325
+ end
326
+
327
+ def validate_workbook()
328
+ unless @workbook.nil? || @workbook.worksheets.nil?
329
+ @workbook.worksheets.each do |sheet|
330
+ unless sheet.nil? || sheet.sheet_data.nil? || sheet.sheet_data[@row].nil?
331
+ if sheet.sheet_data[@row][@column] == self
332
+ return
333
+ end
334
+ end
335
+ end
336
+ end
337
+ raise "This cell #{self} is not in workbook #{@workbook}"
338
+ end
339
+
340
+ def validate_worksheet()
341
+ if !@worksheet.nil? && @worksheet[@row][@column] == self
342
+ return
343
+ else
344
+ raise "This cell #{self} is not in worksheet #{worksheet}"
345
+ end
346
+ end
347
+
348
+ def xf_id()
349
+ @workbook.get_style_attributes(@workbook.get_style(@style_index.to_s))
350
+ end
351
+
352
+ def border_id()
353
+ xf_id()[:borderId].to_s
354
+ end
355
+
356
+ def font_id()
357
+ xf_id()[:fontId].to_s
358
+ end
359
+ end
360
+ end