roo 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,5 +1,13 @@
1
+ == 0.5.1 2007-08-26
2
+ * 4 enhancements:
3
+ * Openoffice: Exception if an illegal sheet-name is selected
4
+ * Openoffice/Excel: no need to set a default_sheet if there is only one in
5
+ the document
6
+ * Excel: can now read zip-ed files
7
+ * Excel: can now read files from http://-URL over the net
8
+
1
9
  == 0.5.0 2007-07-20
2
- * 2 enhancements:
10
+ * 3 enhancements:
3
11
  * Excel-objects: the methods default_sheet= and sheets can now handle names instead of numbers
4
12
  * changed the celltype methods to return symbols, not strings anymore (possible values are :formula, :float, :string, :date, :percentage (if you need strings you can convert it with .to_s)
5
13
  * tests can now run on the client machine (not only my machine), if there are not public released files involved these tests are skipped
data/Manifest.txt CHANGED
@@ -21,6 +21,9 @@ test/borders.ods
21
21
  test/borders.xls
22
22
  test/formula.ods
23
23
  test/formula.xls
24
+ test/only_one_sheet.ods
25
+ test/only_one_sheet.xls
26
+ test/bode-v1.xls.zip
24
27
  website/index.html
25
28
  website/index.txt
26
29
  website/javascripts/rounded_corners_lite.inc.js
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ require File.join(File.dirname(__FILE__), 'lib', 'roo', 'version')
14
14
 
15
15
  AUTHOR = 'Thomas Preymesser' # can also be an array of Authors
16
16
  EMAIL = "thopre@gmail.com"
17
- DESCRIPTION = "roo can access the contents of OpenOffice-Spreadsheets"
17
+ DESCRIPTION = "roo can access the contents of OpenOffice-Spreadsheets and Excel-Spreadsheets"
18
18
  GEM_NAME = 'roo' # what ppl will type to install your gem
19
19
 
20
20
  @config_file = "~/.rubyforge/user-config.yml"
@@ -79,6 +79,7 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
79
79
  ['rubyzip', '>= 0.9.1'],
80
80
  ['hpricot', '>= 0.5'],
81
81
  ['hoe', '>= 0.0.0'],
82
+ ['GData', '>= 0.0.3'],
82
83
  ]
83
84
  #p.spec_extras = {} # A hash of extra values to set in the gemspec.
84
85
  end
@@ -45,9 +45,9 @@ puts "last column: #{proxy.last_column}"
45
45
  puts "cell: #{proxy.cell('C',8)}"
46
46
  puts "cell: #{proxy.cell('F',12)}"
47
47
  puts "officeversion: #{proxy.officeversion}"
48
- puts "Bayern:"
48
+ puts "Berlin:"
49
49
 
50
- ferien_fuer_region(proxy, "Bayern")
50
+ ferien_fuer_region(proxy, "Berlin")
51
51
 
52
52
 
53
53
 
data/lib/roo/excel.rb CHANGED
@@ -1,18 +1,37 @@
1
1
  require 'rubygems'
2
+ gem 'parseexcel', '>= 0.5.2'
2
3
  require 'parseexcel'
4
+ require 'pp'
3
5
 
4
6
  class Excel < Openoffice
5
7
 
6
- def initialize(filename)
8
+ def initialize(filename, packed = nil)
9
+ @tmpdir = "oo_"+$$.to_s
10
+ unless File.exists?(@tmpdir)
11
+ FileUtils::mkdir(@tmpdir)
12
+ end
13
+ filename = open_from_uri(filename) if filename[0,7] == "http://"
14
+ filename = unzip(filename) if packed and packed == :zip
7
15
  if filename[-4..-1] != ".xls"
8
16
  warn "are you sure, this is an excel file?"
9
17
  end
10
18
  @filename = filename
19
+ p filename
11
20
  @workbook = Spreadsheet::ParseExcel.parse(filename)
12
21
  @default_sheet = nil
22
+ # no need to set default_sheet if there is only one sheet in the document
23
+ if self.sheets.size == 1
24
+ @default_sheet = self.sheets.first
25
+ end
13
26
  @first_row = @last_row = @first_column = @last_column = nil
14
27
  end
15
28
 
29
+ def remove_tmp
30
+ if File.exists?(@tmpdir)
31
+ FileUtils::rm_r(@tmpdir)
32
+ end
33
+ end
34
+
16
35
  def sheets
17
36
  #if DateTime.now < Date.new(2007,6,10)
18
37
  # return ["Tabelle1", "Name of Sheet 2", "Sheet3"]
@@ -29,19 +48,15 @@ class Excel < Openoffice
29
48
  end
30
49
 
31
50
  # sets the working sheet (1,2,3,..)
32
- #--
33
- # im Excel-Bereich muesste man wahrscheinlich intern mit Nummern arbeiten
34
- # von aussen arbeite ich mit (1,2,3... intern wird Index 0,1,2,...
35
- # verwendet.
36
51
  def default_sheet=(n)
37
- if DateTime.now < Date.new(2007,7,19)
38
- unless n.kind_of?(Fixnum)
39
- fail ArgumentError.new("Number expected")
40
- end
41
- @default_sheet = n-1
42
- else
52
+ if n.kind_of?(Fixnum)
53
+ @default_sheet = n #-1
54
+ elsif n.kind_of?(String)
55
+ raise RangeError if ! self.sheets.include?(n)
43
56
  # parseexcel supports now the name of a sheet
44
57
  @default_sheet = n
58
+ else
59
+ raise TypeError, "what are you trying to set as default sheet?"
45
60
  end
46
61
  @first_row = @last_row = @first_column = @last_column = nil
47
62
  @cells_read = false
@@ -106,11 +121,8 @@ class Excel < Openoffice
106
121
  when :text then result << cell.to_s('utf-8')
107
122
  when :date then result << cell.date
108
123
  else
109
- #p cell.type
110
124
  return result << cell.to_s('utf-8')
111
125
  end
112
-
113
- #result << cell.value
114
126
  }
115
127
  return result
116
128
  end
@@ -152,6 +164,37 @@ class Excel < Openoffice
152
164
  false
153
165
  end
154
166
 
167
+ def open_from_uri(uri)
168
+ require 'open-uri' ;
169
+
170
+ script = nil ;
171
+ result = nil ;
172
+ #uri = "/home/tp/ruby-test/roo/test/numbers1.xls"
173
+
174
+ #begin ;
175
+ # result = open(page) {|w| script = w.readlines } ;
176
+ #rescue => e ;
177
+ # puts "e=#{e}, e.class=#{e.class}" ;
178
+ # result = [] ;
179
+ #end ;
180
+ #
181
+ # puts "result class=#{result.class}, count=#{result.length}" ; #,
182
+ # data=#{result}" ;
183
+ # puts "result[0] =>#{result[0].chomp}<=" if result[0] ;
184
+
185
+ tempfilename = File.join(@tmpdir, File.basename(uri))
186
+ f = File.open( File.join(@tmpdir, File.basename(uri)),"w")
187
+ begin
188
+ open(uri) do |net|
189
+ f.write(net.read)
190
+ end
191
+ rescue
192
+ raise "could not open #{uri}"
193
+ end
194
+ f.close
195
+ File.join(@tmpdir, File.basename(uri))
196
+ end
197
+
155
198
  private
156
199
 
157
200
  # check if default_sheet was set
@@ -170,18 +213,13 @@ private
170
213
  if row_par
171
214
  row_par.each_with_index {|cell,i|
172
215
  # nicht beruecksichtigen, wenn nil und vorher noch nichts war
173
- # p cell
174
- if !cell
175
- # nix
176
- else
216
+ if cell
177
217
  fc = [fc, i+1].min
178
218
  lc = [lc, i+1].max
179
219
  fr = [fr, line].min
180
220
  lr = [lr, line].max
181
221
  end
182
222
  }
183
- else
184
- #???
185
223
  end
186
224
  line += 1
187
225
  }
@@ -204,4 +242,40 @@ private
204
242
  end
205
243
  raise StandardError, "sheet '#{name}' not found"
206
244
  end
245
+
246
+ def process_zipfile(zip, path='')
247
+ ret=nil
248
+ if zip.file.file? path
249
+ #puts %{#{path}: "#{zip.read(path)}"}
250
+ puts %{#{path}: }
251
+ # extract and return filename
252
+ @tmpdir = "oo_"+$$.to_s
253
+ unless File.exists?(@tmpdir)
254
+ FileUtils::mkdir(@tmpdir)
255
+ end
256
+ #file = File.new(File.join(@tmpdir, path))
257
+ file = File.open(File.join(@tmpdir, path),"w")
258
+ file.write(zip.read(path))
259
+ file.close
260
+ p File.join(@tmpdir, path)
261
+ return File.join(@tmpdir, path)
262
+ else
263
+ unless path.empty?
264
+ path += '/'
265
+ puts path
266
+ end
267
+ zip.dir.foreach(path) do |filename|
268
+ ret = process_zipfile(zip, path + filename)
269
+ end
270
+ end
271
+ ret
272
+ end
273
+
274
+ def unzip(filename)
275
+ ret = nil
276
+ Zip::ZipFile.open(filename) do |zip|
277
+ ret = process_zipfile zip
278
+ end
279
+ ret
280
+ end
207
281
  end
data/lib/roo/google.rb CHANGED
@@ -1,112 +1,124 @@
1
1
 
2
- require 'net/http'
3
- require 'net/https'
4
- require 'uri'
2
+ #require 'net/http'
3
+ #require 'net/https'
4
+ #require 'uri'
5
5
  require 'rubygems'
6
- require 'hpricot'
6
+ #require 'hpricot'
7
7
  require 'timeout'
8
-
8
+ #require 'GData'
9
+ require 'gdata'
9
10
 
10
11
  # Make it easy to use some of the convenience methods using https
11
12
  #
12
- module Net class HTTPS < HTTP
13
- def initialize(address, port = nil)
14
- super(address, port)
15
- self.use_ssl = true
16
- end
17
- end
18
- end
19
-
20
- class GoogleSpreadSheet
21
- GOOGLE_LOGIN_URL = URI.parse('https://www.google.com/accounts/ClientLogin')
22
-
23
- def initialize(spreadsheet_key)
24
- @spreadsheet_key = spreadsheet_key
25
- @headers = nil
26
- @default_sheet = nil
27
- end
28
-
29
- def default_sheet=(numberofsheet)
30
- @default_sheet = numberofsheet
31
- end
32
-
33
- def authenticate(email, password)
34
- $VERBOSE = nil
35
- response = Net::HTTPS.post_form(GOOGLE_LOGIN_URL,
36
- {'Email' => email,
37
- 'Passwd' => password,
38
- 'source' => "formula",
39
- 'service' => 'wise' })
40
- @headers = { 'Authorization' => "GoogleLogin auth=#{response.body.split(/=/).last}",
41
- 'Content-Type' => 'application/atom+xml'
42
- }
43
- end
44
-
45
- def evaluate_cell(cell)
46
- path = "/feeds/cells/#{@spreadsheet_key}/#{@default_sheet}/#{@headers ? "private" : "public"}/basic/#{cell}"
47
-
48
- doc = Hpricot(request(path))
49
- result = (doc/"content[@type='text']").inner_html
50
- end
51
-
52
- def set_entry(entry)
53
- path = "/feeds/cells/#{@spreadsheet_key}/#{@default_sheet}/#{@headers ? 'private' : 'public'}/full"
54
-
55
- post(path, entry)
56
- end
57
-
58
- def entry(formula, row=1, col=1)
59
- <<XML
60
- <?xml version='1.0' ?>
61
- <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'>
62
- <gs:cell row='#{row}' col='#{col}' inputValue='=#{formula}' />
63
- </entry>
64
- XML
65
- end
66
-
67
- def add_to_cell(formula) #puts entry(formula)
68
- set_entry(entry(formula))
69
- end
70
-
71
- private
72
- def request(path)
73
- response, data = get_http.get(path, @headers)
74
- data
75
- end
76
-
77
- def post(path, entry)
78
- get_http.post(path, entry, @headers)
79
- end
80
-
81
- def get_http
82
- http = Net::HTTP.new('spreadsheets.google.com', 80)
83
- #http.set_debug_output $stderr
84
- http
85
- end
86
- end
13
+ #module Net class HTTPS < HTTP
14
+ # def initialize(address, port = nil)
15
+ # super(address, port)
16
+ # self.use_ssl = true
17
+ # end
18
+ # end
19
+ #end
20
+ #
21
+ #class GoogleSpreadSheet
22
+ # GOOGLE_LOGIN_URL = URI.parse('https://www.google.com/accounts/ClientLogin')
23
+ #
24
+ # def initialize(spreadsheet_key)
25
+ # @spreadsheet_key = spreadsheet_key
26
+ # @headers = nil
27
+ # @default_sheet = nil
28
+ # end
29
+ #
30
+ # def default_sheet=(numberofsheet)
31
+ # @default_sheet = numberofsheet
32
+ # end
33
+ #
34
+ # def authenticate(email, password)
35
+ # $VERBOSE = nil
36
+ # response = Net::HTTPS.post_form(GOOGLE_LOGIN_URL,
37
+ # {'Email' => email,
38
+ # 'Passwd' => password,
39
+ # 'source' => "formula",
40
+ # 'service' => 'wise' })
41
+ # @headers = { 'Authorization' => "GoogleLogin auth=#{response.body.split(/=/).last}",
42
+ # 'Content-Type' => 'application/atom+xml'
43
+ # }
44
+ # end
45
+ #
46
+ # def evaluate_cell(cell)
47
+ # path = "/feeds/cells/#{@spreadsheet_key}/#{@default_sheet}/#{@headers ? "private" : "public"}/basic/#{cell}"
48
+ #
49
+ # doc = Hpricot(request(path))
50
+ # result = (doc/"content[@type='text']").inner_html
51
+ # end
52
+ #
53
+ # def set_entry(entry)
54
+ # path = "/feeds/cells/#{@spreadsheet_key}/#{@default_sheet}/#{@headers ? 'private' : 'public'}/full"
55
+ #
56
+ # post(path, entry)
57
+ # end
58
+ #
59
+ # def entry(formula, row=1, col=1)
60
+ # <<XML
61
+ #<?xml version='1.0' ?>
62
+ #<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'>
63
+ # <gs:cell row='#{row}' col='#{col}' inputValue='=#{formula}' />
64
+ #</entry>
65
+ #XML
66
+ # end
67
+ #
68
+ # def add_to_cell(formula) #puts entry(formula)
69
+ # set_entry(entry(formula))
70
+ # end
71
+ #
72
+ # private
73
+ # def request(path)
74
+ # response, data = get_http.get(path, @headers)
75
+ # data
76
+ # end
77
+ #
78
+ # def post(path, entry)
79
+ # get_http.post(path, entry, @headers)
80
+ # end
81
+ #
82
+ # def get_http
83
+ # http = Net::HTTP.new('spreadsheets.google.com', 80)
84
+ # #http.set_debug_output $stderr
85
+ # http
86
+ # end
87
+ #end
87
88
 
88
89
  class Google < Openoffice
89
90
 
90
91
  TIMEOUT_IN_SECONDS = 2
91
92
 
92
- def initialize(user, password, spreadsheetkey)
93
+ def initialize(user, password, spreadsheetkey, spreadsheetname)
93
94
  @cells_read = false
94
95
  @cell = Hash.new
95
96
  @cell_type = Hash.new
96
- Timeout.timeout(TIMEOUT_IN_SECONDS) {
97
- @gs = GoogleSpreadSheet.new(spreadsheetkey)
98
- @gs.authenticate(user, password)
99
- @default_sheet = nil
100
- }
97
+ #Timeout.timeout(TIMEOUT_IN_SECONDS) {
98
+ # @gs = GoogleSpreadSheet.new(spreadsheetkey)
99
+ # @gs.authenticate(user, password)
100
+ # @default_sheet = nil
101
+ #}
102
+ # alle eigenen Spreadsheets bei Google
103
+ # @spreadsheets= GData::Spreadsheet.spreadsheets("emmanuel.pirsch@gmail.com", "secret") # funktioniert anscheinend noch nicht!!!!!!!!!
104
+ # ein einzelnes Spreadsheet bei Google
105
+ # @spreadsheet= spreadsheet[spreadsheetname]
106
+
107
+ #gb = GData::Base.new
108
+ #gb.authenticate("thopre@gmail.com","nora3033")
109
+
110
+ g = GData::Spreadsheet.new("ttt")
111
+ @default_sheet = nil
101
112
  end
102
113
 
103
114
  def sheets
104
- # http://spreadsheets.google.com/feeds/worksheets/ptu6bbahNZpYrdGHwteUNCw/private/full
105
- if DateTime.now < Date.new(2007,6,15)
106
- return ["Sheet eins","Sheet zwei","Sheet drei"]
107
- else
108
- return []
109
- end
115
+ # http://spreadsheets.google.com/feeds/worksheets/ptu6bbahNZpYrdGHwteUNCw/private/full
116
+ #if DateTime.now < Date.new(2007,6,15)
117
+ # return ["Sheet eins","Sheet zwei","Sheet drei"]
118
+ #else
119
+ # return []
120
+ #end
121
+ @spreadsheet
110
122
  end
111
123
 
112
124
  def default_sheet=(numberofsheet)
@@ -4,7 +4,7 @@ require 'rexml/document'
4
4
  require 'fileutils'
5
5
  require 'zip/zipfilesystem'
6
6
  require 'date'
7
- require 'llip'
7
+ #require 'llip'
8
8
  require 'base64'
9
9
 
10
10
  #require 'lib/roo/spreadsheetparser'
@@ -16,13 +16,13 @@ class Openoffice
16
16
  # initialization and opening of a spreasheet file
17
17
  # file will be created if 'create' is true
18
18
  # and the file does not exist
19
- def initialize(filename, create = false)
19
+ def initialize(filename, packed=nil) #, create = false)
20
20
  if filename[-4..-1] != ".ods"
21
21
  warn "are you sure, this is an openoffice file?"
22
22
  end
23
- if create and ! File.exists?(filename)
24
- self.create_openoffice(filename)
25
- end
23
+ #if create and ! File.exists?(filename)
24
+ # self.create_openoffice(filename)
25
+ #end
26
26
  @cells_read = false
27
27
  @filename = filename
28
28
  @tmpdir = "oo_"+$$.to_s
@@ -38,15 +38,19 @@ class Openoffice
38
38
  @cell = Hash.new
39
39
  @cell_type = Hash.new
40
40
  @formula = Hash.new
41
- if ENV["roo_local"] != "thomas-p"
41
+ #if ENV["roo_local"] != "thomas-p"
42
42
  FileUtils::rm_r(@tmpdir)
43
- end
43
+ #end
44
44
  @default_sheet = nil
45
+ # no need to set default_sheet if there is only one sheet in the document
46
+ if self.sheets.size == 1
47
+ @default_sheet = self.sheets.first
48
+ end
45
49
  @first_column = @last_column = nil
46
50
  @first_row = @last_row = nil
47
- trap('INT') {
48
- FileUtils::rm_r(@tmpdir)
49
- }
51
+ # trap('INT') {
52
+ # FileUtils::rm_r(@tmpdir)
53
+ # }
50
54
  end
51
55
 
52
56
  # creates a new empty openoffice-spreadsheet file
@@ -61,33 +65,32 @@ class Openoffice
61
65
  f.close
62
66
  end
63
67
 
64
- # reopens and read a spreadsheet document
65
- if false
66
- def reload
67
- @cells_read = false
68
- @tmpdir = "oo_"+$$.to_s
69
- unless File.exists?(@tmpdir)
70
- FileUtils::mkdir(@tmpdir)
71
- end
72
- extract_content
73
- file = File.new(File.join(@tmpdir, @file_nr.to_s+"_roo_content.xml"))
74
- @doc = REXML::Document.new file
75
- file.close
76
- @cell = Hash.new
77
- @cell_type = Hash.new
78
- FileUtils::rm_r(@tmpdir)
79
- @default_sheet = nil
80
- @first_column = @last_column = nil
81
- @first_row = @last_row = nil
82
- end
83
- end
68
+ #if false
69
+ #def reload
70
+ # @cells_read = false
71
+ # @tmpdir = "oo_"+$$.to_s
72
+ # unless File.exists?(@tmpdir)
73
+ # FileUtils::mkdir(@tmpdir)
74
+ # end
75
+ # extract_content
76
+ # file = File.new(File.join(@tmpdir, @file_nr.to_s+"_roo_content.xml"))
77
+ # @doc = REXML::Document.new file
78
+ # file.close
79
+ # @cell = Hash.new
80
+ # @cell_type = Hash.new
81
+ # FileUtils::rm_r(@tmpdir)
82
+ # @default_sheet = nil
83
+ # @first_column = @last_column = nil
84
+ # @first_row = @last_row = nil
85
+ #end
86
+ #end
84
87
 
88
+ # reopens and read a spreadsheet document
85
89
  def reload
86
90
  default_sheet = @default_sheet
87
91
  initialize(@filename)
88
92
  self.default_sheet = default_sheet
89
- @first_row = @last_row = @first_column = @last_column = nil
90
-
93
+ @first_row = @last_row = @first_column = @last_column = nil
91
94
  end
92
95
 
93
96
  # returns the content of a spreadsheet-cell
@@ -177,13 +180,17 @@ class Openoffice
177
180
  end
178
181
 
179
182
  # set the working sheet in the document
180
- def default_sheet=(s)
181
- @default_sheet = s
183
+ def default_sheet=(sheet)
184
+ if ! sheet.kind_of?(String)
185
+ raise TypeError, "what are you trying to set as default sheet?"
186
+ end
187
+ @default_sheet = sheet
182
188
  @first_row = @last_row = @first_column = @last_column = nil
183
189
  @cells_read = false
184
190
  @cell = Hash.new
185
191
  @cell_type = Hash.new
186
192
  @formula = Hash.new
193
+ check_default_sheet
187
194
  end
188
195
 
189
196
  alias set_default_sheet default_sheet=
@@ -191,7 +198,8 @@ class Openoffice
191
198
  # version of the openoffice document
192
199
  # at 2007 this is always "1.0"
193
200
  def officeversion
194
- read_cells(:ignore_default_sheet => true) unless @cells_read
201
+ # read_cells(false) unless @cells_read
202
+ oo_version
195
203
  @officeversion
196
204
  end
197
205
 
@@ -324,9 +332,9 @@ class Openoffice
324
332
  parser = SpreadsheetParser.new
325
333
  visitor = Visitor.new
326
334
  #puts cell(row,col)
327
- puts formula(row,col)
335
+ #puts formula(row,col)
328
336
  formula = formula(row,col)[1..-1] # .downcase
329
- puts formula
337
+ # puts formula
330
338
  #eval formula
331
339
  #parser.parse(formula)
332
340
  parser.parse(formula).accept(visitor)
@@ -371,15 +379,21 @@ class Openoffice
371
379
  end
372
380
 
373
381
  private
382
+ # read the version of the OO-Version
383
+ def oo_version
384
+ sheet_found = false
385
+ @doc.each_element do |oo_document|
386
+ @officeversion = oo_document.attributes['version']
387
+ end
388
+ end
374
389
 
375
390
  # read all cells in the selected sheet
376
- def read_cells(*args)
377
- if :ignore_default_sheet == false
378
- raise ArgumentError, "Error: default_sheet not set" if @default_sheet == nil
379
- end
391
+ def read_cells
392
+ sheet_found = false
393
+ raise ArgumentError, "Error: default_sheet not set" if @default_sheet == nil
380
394
  oo_document_count = 0
381
395
  @doc.each_element do |oo_document|
382
- @officeversion = oo_document.attributes['version']
396
+ # @officeversion = oo_document.attributes['version']
383
397
  oo_document_count += 1
384
398
  oo_element_count = 0
385
399
  oo_document.each_element do |oo_element|
@@ -390,73 +404,73 @@ private
390
404
  be.each_element do |se|
391
405
  if se.name == "table"
392
406
  if se.attributes['name']==@default_sheet
393
-
394
- x=1
395
- y=1
396
- se.each_element do |te|
397
- if te.name == "table-column"
398
- rep = te.attributes["number-columns-repeated"]
399
- elsif te.name == "table-row"
400
- if te.attributes['number-rows-repeated']
401
- skip_y = te.attributes['number-rows-repeated'].to_i
402
- y = y + skip_y - 1 # minus 1 because this line will be counted as a line element
403
- end
404
- te.each_element do |tr|
405
- if tr.name == 'table-cell'
406
- skip = tr.attributes['number-columns-repeated']
407
- formula = tr.attributes['formula']
408
- vt = tr.attributes['value-type']
409
- v = tr.attributes['value']
410
- if vt == 'string'
411
- tr.each_element do |str|
412
- if str.name == 'p'
413
- v = str.text
414
- end
415
- end
416
- end
417
- if skip
418
- if v == nil
419
- x += (skip.to_i - 1)
420
- else
421
- 0.upto(skip.to_i-1) do |i|
422
- @cell_type["#{y},#{x+i}"] = Openoffice.oo_type_2_roo_type(vt)
423
- @formula["#{y},#{x+i}"] = formula if formula
424
- if @cell_type["#{y},#{x+i}"] == :float
425
- @cell["#{y},#{x+i}"] = v.to_f
426
- elsif @cell_type["#{y},#{x+i}"] == :string
427
- @cell["#{y},#{x+i}"] = v
428
- elsif @cell_type["#{y},#{x+i}"] == :date
429
- @cell["#{y},#{x+i}"] = tr.attributes['date-value']
430
- else
431
- @cell["#{y},#{x+i}"] = v
432
- end
433
- end
434
- x += 1
435
- end
436
- end # if skip
437
- @formula["#{y},#{x}"] = formula if formula
438
- @cell_type["#{y},#{x}"] = Openoffice.oo_type_2_roo_type(vt)
439
- if @cell_type["#{y},#{x}"] == :float
440
- @cell["#{y},#{x}"] = v.to_f
441
- elsif @cell_type["#{y},#{x}"] == :string
442
- tr.each_element do |str|
443
- if str.name == 'p'
444
- @cell["#{y},#{x}"] = str.text
445
- end
446
- end
447
- elsif @cell_type["#{y},#{x}"] == :date
448
- @cell["#{y},#{x}"] = tr.attributes['date-value']
449
- else
450
- @cell["#{y},#{x}"] = v
451
- end
452
- x += 1
407
+ sheet_found = true
408
+ x=1
409
+ y=1
410
+ se.each_element do |te|
411
+ if te.name == "table-column"
412
+ rep = te.attributes["number-columns-repeated"]
413
+ elsif te.name == "table-row"
414
+ if te.attributes['number-rows-repeated']
415
+ skip_y = te.attributes['number-rows-repeated'].to_i
416
+ y = y + skip_y - 1 # minus 1 because this line will be counted as a line element
417
+ end
418
+ te.each_element do |tr|
419
+ if tr.name == 'table-cell'
420
+ skip = tr.attributes['number-columns-repeated']
421
+ formula = tr.attributes['formula']
422
+ vt = tr.attributes['value-type']
423
+ v = tr.attributes['value']
424
+ if vt == 'string'
425
+ tr.each_element do |str|
426
+ if str.name == 'p'
427
+ v = str.text
428
+ end
429
+ end
430
+ end
431
+ if skip
432
+ if v == nil
433
+ x += (skip.to_i - 1)
434
+ else
435
+ 0.upto(skip.to_i-1) do |i|
436
+ @cell_type["#{y},#{x+i}"] = Openoffice.oo_type_2_roo_type(vt)
437
+ @formula["#{y},#{x+i}"] = formula if formula
438
+ if @cell_type["#{y},#{x+i}"] == :float
439
+ @cell["#{y},#{x+i}"] = v.to_f
440
+ elsif @cell_type["#{y},#{x+i}"] == :string
441
+ @cell["#{y},#{x+i}"] = v
442
+ elsif @cell_type["#{y},#{x+i}"] == :date
443
+ @cell["#{y},#{x+i}"] = tr.attributes['date-value']
444
+ else
445
+ @cell["#{y},#{x+i}"] = v
446
+ end
447
+ end
448
+ x += 1
449
+ end
450
+ end # if skip
451
+ @formula["#{y},#{x}"] = formula if formula
452
+ @cell_type["#{y},#{x}"] = Openoffice.oo_type_2_roo_type(vt)
453
+ if @cell_type["#{y},#{x}"] == :float
454
+ @cell["#{y},#{x}"] = v.to_f
455
+ elsif @cell_type["#{y},#{x}"] == :string
456
+ tr.each_element do |str|
457
+ if str.name == 'p'
458
+ @cell["#{y},#{x}"] = str.text
459
+ end
460
+ end
461
+ elsif @cell_type["#{y},#{x}"] == :date
462
+ @cell["#{y},#{x}"] = tr.attributes['date-value']
463
+ else
464
+ @cell["#{y},#{x}"] = v
465
+ end
466
+ x += 1
467
+ end
453
468
  end
469
+ y += 1
470
+ x = 1
454
471
  end
455
- y += 1
456
- x = 1
457
472
  end
458
- end
459
- end # sheet
473
+ end # sheet
460
474
  end
461
475
  end
462
476
  end
@@ -464,11 +478,42 @@ private
464
478
  end
465
479
  end
466
480
  end
481
+ if sheet_found == false
482
+ raise RangeError
483
+ end
467
484
  @cells_read = true
468
485
  end
469
486
 
487
+ # Checks if the default_sheet exists. If not an RangeError exception is
488
+ # raised
489
+ def check_default_sheet
490
+ sheet_found = false
491
+ raise ArgumentError, "Error: default_sheet not set" if @default_sheet == nil
492
+ @doc.each_element do |oo_document|
493
+ oo_element_count = 0
494
+ oo_document.each_element do |oo_element|
495
+ oo_element_count += 1
496
+ if oo_element.name == "body"
497
+ oo_element.each_element do |be|
498
+ if be.name == "spreadsheet"
499
+ be.each_element do |se|
500
+ if se.name == "table"
501
+ if se.attributes['name'] == @default_sheet
502
+ sheet_found = true
503
+ end # sheet
504
+ end
505
+ end
506
+ end
507
+ end
508
+ end
509
+ end
510
+ end
511
+ if ! sheet_found
512
+ raise RangeError, "sheet '#{@default_sheet}' not found"
513
+ end
514
+ end
515
+
470
516
  def process_zipfile(zip, path='')
471
- #p path
472
517
  if zip.file.file? path
473
518
  if path == "content.xml"
474
519
  open(@tmpdir+'/'+@file_nr.to_s+'_roo_content.xml','w') {|f|