donibuchanan-roo 1.3.12
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +225 -0
- data/README.markdown +55 -0
- data/examples/roo_soap_client.rb +53 -0
- data/examples/roo_soap_server.rb +29 -0
- data/examples/write_me.rb +33 -0
- data/lib/roo/excel.rb +468 -0
- data/lib/roo/excel2003xml.rb +411 -0
- data/lib/roo/excelx.rb +602 -0
- data/lib/roo/generic_spreadsheet.rb +628 -0
- data/lib/roo/google.rb +379 -0
- data/lib/roo/openoffice.rb +451 -0
- data/lib/roo/roo_rails_helper.rb +82 -0
- data/lib/roo/version.rb +9 -0
- data/lib/roo.rb +32 -0
- data/test/1900_base.xls +0 -0
- data/test/1904_base.xls +0 -0
- data/test/Bibelbund.csv +3741 -0
- data/test/Bibelbund.ods +0 -0
- data/test/Bibelbund.xls +0 -0
- data/test/Bibelbund.xlsx +0 -0
- data/test/Bibelbund1.ods +0 -0
- data/test/bad_excel_date.xls +0 -0
- data/test/bbu.ods +0 -0
- data/test/bbu.xls +0 -0
- data/test/bbu.xlsx +0 -0
- data/test/bode-v1.ods.zip +0 -0
- data/test/bode-v1.xls.zip +0 -0
- data/test/boolean.ods +0 -0
- data/test/boolean.xls +0 -0
- data/test/boolean.xlsx +0 -0
- data/test/borders.ods +0 -0
- data/test/borders.xls +0 -0
- data/test/borders.xlsx +0 -0
- data/test/bug-row-column-fixnum-float.xls +0 -0
- data/test/datetime.ods +0 -0
- data/test/datetime.xls +0 -0
- data/test/datetime.xlsx +0 -0
- data/test/datetime_floatconv.xls +0 -0
- data/test/emptysheets.ods +0 -0
- data/test/emptysheets.xls +0 -0
- data/test/excel2003.xml +21140 -0
- data/test/false_encoding.xls +0 -0
- data/test/formula.ods +0 -0
- data/test/formula.xls +0 -0
- data/test/formula.xlsx +0 -0
- data/test/formula_parse_error.xls +0 -0
- data/test/html-escape.ods +0 -0
- data/test/no_spreadsheet_file.txt +1 -0
- data/test/numbers1.csv +18 -0
- data/test/numbers1.ods +0 -0
- data/test/numbers1.xls +0 -0
- data/test/numbers1.xlsx +0 -0
- data/test/only_one_sheet.ods +0 -0
- data/test/only_one_sheet.xls +0 -0
- data/test/only_one_sheet.xlsx +0 -0
- data/test/paragraph.ods +0 -0
- data/test/paragraph.xls +0 -0
- data/test/paragraph.xlsx +0 -0
- data/test/ric.ods +0 -0
- data/test/simple_spreadsheet.ods +0 -0
- data/test/simple_spreadsheet.xls +0 -0
- data/test/simple_spreadsheet.xlsx +0 -0
- data/test/simple_spreadsheet_from_italo.ods +0 -0
- data/test/simple_spreadsheet_from_italo.xls +0 -0
- data/test/skipped_tests.rb +789 -0
- data/test/style.ods +0 -0
- data/test/style.xls +0 -0
- data/test/style.xlsx +0 -0
- data/test/test_helper.rb +19 -0
- data/test/test_roo.rb +1834 -0
- data/test/time-test.csv +2 -0
- data/test/time-test.ods +0 -0
- data/test/time-test.xls +0 -0
- data/test/time-test.xlsx +0 -0
- data/test/whitespace.ods +0 -0
- data/test/whitespace.xls +0 -0
- data/test/whitespace.xlsx +0 -0
- metadata +185 -0
data/lib/roo/google.rb
ADDED
@@ -0,0 +1,379 @@
|
|
1
|
+
require 'gdata/spreadsheet'
|
2
|
+
require 'xml'
|
3
|
+
|
4
|
+
class GoogleHTTPError < RuntimeError; end
|
5
|
+
class GoogleReadError < RuntimeError; end
|
6
|
+
class GoogleWriteError < RuntimeError; end
|
7
|
+
|
8
|
+
# overwrite some methods from the gdata-gem:
|
9
|
+
module GData
|
10
|
+
class Spreadsheet < GData::Base
|
11
|
+
|
12
|
+
def visibility
|
13
|
+
@headers ? "private" : "public"
|
14
|
+
end
|
15
|
+
|
16
|
+
def projection
|
17
|
+
@headers ? "full" : "values"
|
18
|
+
end
|
19
|
+
|
20
|
+
#-- modified
|
21
|
+
def evaluate_cell(cell, sheet_no=1)
|
22
|
+
raise ArgumentError, "invalid cell: #{cell}" unless cell
|
23
|
+
raise ArgumentError, "invalid sheet_no: #{sheet_no}" unless sheet_no >0 and sheet_no.class == Fixnum
|
24
|
+
path = "/feeds/cells/#{@spreadsheet_id}/#{sheet_no}/#{visibility}/#{projection}/#{cell}"
|
25
|
+
doc = Hpricot(request(path))
|
26
|
+
result = (doc/"content").inner_html
|
27
|
+
end
|
28
|
+
|
29
|
+
#-- new
|
30
|
+
def sheetlist
|
31
|
+
path = "/feeds/worksheets/#{@spreadsheet_id}/#{visibility}/#{projection}"
|
32
|
+
doc = Hpricot(request(path))
|
33
|
+
result = []
|
34
|
+
(doc/"content").each { |elem|
|
35
|
+
result << elem.inner_html
|
36
|
+
}
|
37
|
+
if result.size == 0
|
38
|
+
if (doc/"h2").inner_html =~ /Error/
|
39
|
+
raise GoogleHTTPError, "#{(doc/'h2').inner_html}: #{(doc/'title').inner_html} [key '#{@spreadsheet_id}']"
|
40
|
+
else
|
41
|
+
raise GoogleReadError, "#{doc} [key '#{@spreadsheet_id}']"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
result
|
45
|
+
end
|
46
|
+
|
47
|
+
#-- new
|
48
|
+
#@@ added sheet_no to definition
|
49
|
+
def save_entry_roo(entry, sheet_no)
|
50
|
+
raise GoogleWriteError, "unable to write to public spreadsheets" if visibility == 'public'
|
51
|
+
path = "/feeds/cells/#{@spreadsheet_id}/#{sheet_no}/#{visibility}/#{projection}"
|
52
|
+
post(path, entry)
|
53
|
+
end
|
54
|
+
|
55
|
+
#-- new
|
56
|
+
def entry_roo(formula, row=1, col=1)
|
57
|
+
<<-XML
|
58
|
+
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'>
|
59
|
+
<gs:cell row='#{row}' col='#{col}' inputValue='#{formula}' />
|
60
|
+
</entry>
|
61
|
+
XML
|
62
|
+
end
|
63
|
+
|
64
|
+
#-- new
|
65
|
+
#@@ added sheet_no to definition
|
66
|
+
def add_to_cell_roo(row,col,value, sheet_no=1)
|
67
|
+
save_entry_roo(entry_roo(value,row,col), sheet_no)
|
68
|
+
end
|
69
|
+
|
70
|
+
#-- new
|
71
|
+
def get_one_sheet
|
72
|
+
path = "/feeds/cells/#{@spreadsheet_id}/1/#{visibility}/#{projection}"
|
73
|
+
doc = Hpricot(request(path))
|
74
|
+
end
|
75
|
+
|
76
|
+
#new
|
77
|
+
def oben_unten_links_rechts(sheet_no)
|
78
|
+
path = "/feeds/cells/#{@spreadsheet_id}/#{sheet_no}/#{visibility}/#{projection}"
|
79
|
+
doc = Hpricot(request(path))
|
80
|
+
rows = []
|
81
|
+
cols = []
|
82
|
+
(doc/"gs:cell").each {|item|
|
83
|
+
rows.push item['row'].to_i
|
84
|
+
cols.push item['col'].to_i
|
85
|
+
}
|
86
|
+
return rows.min, rows.max, cols.min, cols.max
|
87
|
+
end
|
88
|
+
|
89
|
+
def fulldoc(sheet_no)
|
90
|
+
path = "/feeds/cells/#{@spreadsheet_id}/#{sheet_no}/#{visibility}/#{projection}"
|
91
|
+
doc = Hpricot(request(path))
|
92
|
+
return doc
|
93
|
+
end
|
94
|
+
|
95
|
+
end # class
|
96
|
+
end # module
|
97
|
+
|
98
|
+
class Google < GenericSpreadsheet
|
99
|
+
attr_accessor :date_format, :datetime_format
|
100
|
+
|
101
|
+
# Creates a new Google spreadsheet object.
|
102
|
+
def initialize(spreadsheetkey,user=nil,password=nil)
|
103
|
+
@filename = spreadsheetkey
|
104
|
+
@spreadsheetkey = spreadsheetkey
|
105
|
+
@user = user
|
106
|
+
@password = password
|
107
|
+
unless user
|
108
|
+
user = ENV['GOOGLE_MAIL']
|
109
|
+
end
|
110
|
+
unless password
|
111
|
+
password = ENV['GOOGLE_PASSWORD']
|
112
|
+
end
|
113
|
+
@cell = Hash.new {|h,k| h[k]=Hash.new}
|
114
|
+
@cell_type = Hash.new {|h,k| h[k]=Hash.new}
|
115
|
+
@formula = Hash.new
|
116
|
+
@first_row = Hash.new
|
117
|
+
@last_row = Hash.new
|
118
|
+
@first_column = Hash.new
|
119
|
+
@last_column = Hash.new
|
120
|
+
@cells_read = Hash.new
|
121
|
+
@header_line = 1
|
122
|
+
@date_format = '%d/%m/%Y'
|
123
|
+
@datetime_format = '%d/%m/%Y %H:%M:%S'
|
124
|
+
@time_format = '%H:%M:%S'
|
125
|
+
@gs = GData::Spreadsheet.new(spreadsheetkey)
|
126
|
+
@gs.authenticate(user, password) unless user.empty? || password.empty?
|
127
|
+
@sheetlist = @gs.sheetlist
|
128
|
+
@default_sheet = self.sheets.first
|
129
|
+
end
|
130
|
+
|
131
|
+
# returns an array of sheet names in the spreadsheet
|
132
|
+
def sheets
|
133
|
+
@sheetlist
|
134
|
+
end
|
135
|
+
|
136
|
+
def date?(string)
|
137
|
+
begin
|
138
|
+
Date.strptime(string, @date_format)
|
139
|
+
true
|
140
|
+
rescue
|
141
|
+
false
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# is String a time with format HH:MM:SS?
|
146
|
+
def time?(string)
|
147
|
+
begin
|
148
|
+
DateTime.strptime(string, @time_format)
|
149
|
+
true
|
150
|
+
rescue
|
151
|
+
false
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def datetime?(string)
|
156
|
+
begin
|
157
|
+
DateTime.strptime(string, @datetime_format)
|
158
|
+
true
|
159
|
+
rescue
|
160
|
+
false
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def numeric?(string)
|
165
|
+
string =~ /^[0-9]+[\.]*[0-9]*$/
|
166
|
+
end
|
167
|
+
|
168
|
+
def timestring_to_seconds(value)
|
169
|
+
hms = value.split(':')
|
170
|
+
hms[0].to_i*3600 + hms[1].to_i*60 + hms[2].to_i
|
171
|
+
end
|
172
|
+
|
173
|
+
# Returns the content of a spreadsheet-cell.
|
174
|
+
# (1,1) is the upper left corner.
|
175
|
+
# (1,1), (1,'A'), ('A',1), ('a',1) all refers to the
|
176
|
+
# cell at the first line and first row.
|
177
|
+
def cell(row, col, sheet=nil)
|
178
|
+
sheet = @default_sheet unless sheet
|
179
|
+
check_default_sheet #TODO: 2007-12-16
|
180
|
+
read_cells(sheet) unless @cells_read[sheet]
|
181
|
+
row,col = normalize(row,col)
|
182
|
+
value = @cell[sheet]["#{row},#{col}"]
|
183
|
+
if celltype(row,col,sheet) == :date
|
184
|
+
begin
|
185
|
+
return Date.strptime(value, @date_format)
|
186
|
+
rescue ArgumentError
|
187
|
+
raise "Invalid Date #{sheet}[#{row},#{col}] #{value} using format '{@date_format}'"
|
188
|
+
end
|
189
|
+
elsif celltype(row,col,sheet) == :datetime
|
190
|
+
begin
|
191
|
+
return DateTime.strptime(value, @datetime_format)
|
192
|
+
rescue ArgumentError
|
193
|
+
raise "Invalid DateTime #{sheet}[#{row},#{col}] #{value} using format '{@datetime_format}'"
|
194
|
+
end
|
195
|
+
end
|
196
|
+
return value
|
197
|
+
end
|
198
|
+
|
199
|
+
# returns the type of a cell:
|
200
|
+
# * :float
|
201
|
+
# * :string
|
202
|
+
# * :date
|
203
|
+
# * :percentage
|
204
|
+
# * :formula
|
205
|
+
# * :time
|
206
|
+
# * :datetime
|
207
|
+
def celltype(row, col, sheet=nil)
|
208
|
+
sheet = @default_sheet unless sheet
|
209
|
+
read_cells(sheet) unless @cells_read[sheet]
|
210
|
+
row,col = normalize(row,col)
|
211
|
+
if @formula.size > 0 && @formula[sheet]["#{row},#{col}"]
|
212
|
+
return :formula
|
213
|
+
else
|
214
|
+
@cell_type[sheet]["#{row},#{col}"]
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
# Returns the formula at (row,col).
|
219
|
+
# Returns nil if there is no formula.
|
220
|
+
# The method #formula? checks if there is a formula.
|
221
|
+
def formula(row,col,sheet=nil)
|
222
|
+
sheet = @default_sheet unless sheet
|
223
|
+
read_cells(sheet) unless @cells_read[sheet]
|
224
|
+
row,col = normalize(row,col)
|
225
|
+
if @formula[sheet]["#{row},#{col}"] == nil
|
226
|
+
return nil
|
227
|
+
else
|
228
|
+
return @formula[sheet]["#{row},#{col}"]
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
# true, if there is a formula
|
233
|
+
def formula?(row,col,sheet=nil)
|
234
|
+
sheet = @default_sheet unless sheet
|
235
|
+
read_cells(sheet) unless @cells_read[sheet]
|
236
|
+
row,col = normalize(row,col)
|
237
|
+
formula(row,col) != nil
|
238
|
+
end
|
239
|
+
|
240
|
+
# returns each formula in the selected sheet as an array of elements
|
241
|
+
# [row, col, formula]
|
242
|
+
def formulas(sheet=nil)
|
243
|
+
theformulas = Array.new
|
244
|
+
sheet = @default_sheet unless sheet
|
245
|
+
read_cells(sheet) unless @cells_read[sheet]
|
246
|
+
first_row(sheet).upto(last_row(sheet)) {|row|
|
247
|
+
first_column(sheet).upto(last_column(sheet)) {|col|
|
248
|
+
if formula?(row,col,sheet)
|
249
|
+
f = [row, col, formula(row,col,sheet)]
|
250
|
+
theformulas << f
|
251
|
+
end
|
252
|
+
}
|
253
|
+
}
|
254
|
+
theformulas
|
255
|
+
end
|
256
|
+
|
257
|
+
# true, if the cell is empty
|
258
|
+
def empty?(row, col, sheet=nil)
|
259
|
+
value = cell(row, col, sheet)
|
260
|
+
return true unless value
|
261
|
+
return false if value.class == Date # a date is never empty
|
262
|
+
return false if value.class == Float
|
263
|
+
return false if celltype(row,col,sheet) == :time
|
264
|
+
value.empty?
|
265
|
+
end
|
266
|
+
|
267
|
+
# sets the cell to the content of 'value'
|
268
|
+
# a formula can be set in the form of '=SUM(...)'
|
269
|
+
def set_value(row,col,value,sheet=nil)
|
270
|
+
sheet = @default_sheet unless sheet
|
271
|
+
raise RangeError, "sheet not set" unless sheet
|
272
|
+
#@@ Set and pass sheet_no
|
273
|
+
begin
|
274
|
+
sheet_no = sheets.index(sheet)+1
|
275
|
+
rescue
|
276
|
+
raise RangeError, "invalid sheet '"+sheet.to_s+"'"
|
277
|
+
end
|
278
|
+
row,col = normalize(row,col)
|
279
|
+
@gs.add_to_cell_roo(row,col,value,sheet_no)
|
280
|
+
# re-read the portion of the document that has changed
|
281
|
+
if @cells_read[sheet]
|
282
|
+
key = "#{row},#{col}"
|
283
|
+
(value, value_type) = determine_datatype(value.to_s)
|
284
|
+
@cell[sheet][key] = value
|
285
|
+
@cell_type[sheet][key] = value_type
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
# returns the first non-empty row in a sheet
|
290
|
+
def first_row(sheet=nil)
|
291
|
+
sheet = @default_sheet unless sheet
|
292
|
+
unless @first_row[sheet]
|
293
|
+
sheet_no = sheets.index(sheet) + 1
|
294
|
+
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] = @gs.oben_unten_links_rechts(sheet_no)
|
295
|
+
end
|
296
|
+
return @first_row[sheet]
|
297
|
+
end
|
298
|
+
|
299
|
+
# returns the last non-empty row in a sheet
|
300
|
+
def last_row(sheet=nil)
|
301
|
+
sheet = @default_sheet unless sheet
|
302
|
+
unless @last_row[sheet]
|
303
|
+
sheet_no = sheets.index(sheet) + 1
|
304
|
+
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] = @gs.oben_unten_links_rechts(sheet_no)
|
305
|
+
end
|
306
|
+
return @last_row[sheet]
|
307
|
+
end
|
308
|
+
|
309
|
+
# returns the first non-empty column in a sheet
|
310
|
+
def first_column(sheet=nil)
|
311
|
+
sheet = @default_sheet unless sheet
|
312
|
+
unless @first_column[sheet]
|
313
|
+
sheet_no = sheets.index(sheet) + 1
|
314
|
+
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] = @gs.oben_unten_links_rechts(sheet_no)
|
315
|
+
end
|
316
|
+
return @first_column[sheet]
|
317
|
+
end
|
318
|
+
|
319
|
+
# returns the last non-empty column in a sheet
|
320
|
+
def last_column(sheet=nil)
|
321
|
+
sheet = @default_sheet unless sheet
|
322
|
+
unless @last_column[sheet]
|
323
|
+
sheet_no = sheets.index(sheet) + 1
|
324
|
+
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] = @gs.oben_unten_links_rechts(sheet_no)
|
325
|
+
end
|
326
|
+
return @last_column[sheet]
|
327
|
+
end
|
328
|
+
|
329
|
+
private
|
330
|
+
|
331
|
+
# read all cells in a sheet.
|
332
|
+
def read_cells(sheet=nil)
|
333
|
+
sheet = @default_sheet unless sheet
|
334
|
+
raise RangeError, "illegal sheet <#{sheet}>" unless sheets.index(sheet)
|
335
|
+
sheet_no = sheets.index(sheet)+1
|
336
|
+
xml = @gs.fulldoc(sheet_no).to_s
|
337
|
+
doc = XML::Parser.string(xml).parse
|
338
|
+
doc.find("//*[local-name()='cell']").each do |item|
|
339
|
+
row = item['row']
|
340
|
+
col = item['col']
|
341
|
+
key = "#{row},#{col}"
|
342
|
+
string_value = item['inputvalue'] || item['inputValue']
|
343
|
+
numeric_value = item['numericvalue'] || item['numericValue']
|
344
|
+
(value, value_type) = determine_datatype(string_value, numeric_value)
|
345
|
+
@cell[sheet][key] = value unless value == "" or value == nil
|
346
|
+
@cell_type[sheet][key] = value_type
|
347
|
+
@formula[sheet] = {} unless @formula[sheet]
|
348
|
+
@formula[sheet][key] = string_value if value_type == :formula
|
349
|
+
end
|
350
|
+
@cells_read[sheet] = true
|
351
|
+
end
|
352
|
+
|
353
|
+
def determine_datatype(val, numval=nil)
|
354
|
+
if val.nil? || val[0,1] == '='
|
355
|
+
ty = :formula
|
356
|
+
if numeric?(numval)
|
357
|
+
val = numval.to_f
|
358
|
+
else
|
359
|
+
val = numval
|
360
|
+
end
|
361
|
+
else
|
362
|
+
if datetime?(val)
|
363
|
+
ty = :datetime
|
364
|
+
elsif date?(val)
|
365
|
+
ty = :date
|
366
|
+
elsif numeric?(val)
|
367
|
+
ty = :float
|
368
|
+
val = val.to_f
|
369
|
+
elsif time?(val)
|
370
|
+
ty = :time
|
371
|
+
val = timestring_to_seconds(val)
|
372
|
+
else
|
373
|
+
ty = :string
|
374
|
+
end
|
375
|
+
end
|
376
|
+
return val, ty
|
377
|
+
end
|
378
|
+
|
379
|
+
end # class
|