roo 1.9.3 → 1.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +12 -0
- data/README.txt +3 -0
- data/Rakefile +31 -95
- data/TODO +2 -0
- data/a.xls +0 -0
- data/bin/roo +1 -1
- data/lib/roo.rb +2 -1
- data/lib/roo/.generic_spreadsheet.rb.rb.swp +0 -0
- data/lib/roo/excel.rb +33 -6
- data/lib/roo/excelx.rb +16 -5
- data/lib/roo/generic_spreadsheet.rb +53 -73
- data/lib/roo/google.rb +330 -325
- data/lib/roo/openoffice.rb +520 -503
- data/rm_sub_test.rb +13 -0
- data/rm_test.rb +20 -0
- data/test/Bibelbund.csv +0 -0
- data/test/bode-v1.ods.zip +0 -0
- data/test/bode-v1.xls.zip +0 -0
- data/test/formula_string_error.xlsx +0 -0
- data/test/style.xls +0 -0
- data/test/test_helper.rb +30 -0
- data/test/test_roo.rb +2254 -2022
- data/test/type_excel.ods +0 -0
- data/test/type_excel.xlsx +0 -0
- data/test/type_excelx.ods +0 -0
- data/test/type_excelx.xls +0 -0
- data/test/type_openoffice.xls +0 -0
- data/test/type_openoffice.xlsx +0 -0
- data/tmp.xls +0 -0
- data/{csv9419 → tmpBibelbund.csv} +3741 -3741
- data/tmp_output.xml +85 -0
- data/{test/numbers1.csv → tmpnumbers1.csv} +18 -18
- metadata +93 -51
- data/csv11159 +0 -2888
- data/csv1414 +0 -2525
- data/csv9957 +0 -915
- data/test/ScienceStaff.xls +0 -0
- data/test/ScienceStaff_modified.xls +0 -0
- data/test/test_spreadsheet.rb +0 -19
data/lib/roo/google.rb
CHANGED
@@ -1,325 +1,330 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
class
|
5
|
-
class
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
@
|
29
|
-
@
|
30
|
-
@
|
31
|
-
@
|
32
|
-
@
|
33
|
-
@
|
34
|
-
@
|
35
|
-
|
36
|
-
@
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
@
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
@
|
47
|
-
end
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
#
|
118
|
-
# * :
|
119
|
-
# * :
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
value
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
############
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
elsif
|
293
|
-
ty = :
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
def
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
end
|
1
|
+
require "google_spreadsheet"
|
2
|
+
|
3
|
+
class GoogleHTTPError < RuntimeError; end
|
4
|
+
class GoogleReadError < RuntimeError; end
|
5
|
+
class GoogleWriteError < RuntimeError; end
|
6
|
+
|
7
|
+
class Google < GenericSpreadsheet
|
8
|
+
attr_accessor :date_format, :datetime_format
|
9
|
+
|
10
|
+
# Creates a new Google spreadsheet object.
|
11
|
+
def initialize(spreadsheetkey,user=nil,password=nil)
|
12
|
+
@filename = spreadsheetkey
|
13
|
+
@spreadsheetkey = spreadsheetkey
|
14
|
+
@user = user
|
15
|
+
@password = password
|
16
|
+
unless user
|
17
|
+
user = ENV['GOOGLE_MAIL']
|
18
|
+
end
|
19
|
+
unless password
|
20
|
+
password = ENV['GOOGLE_PASSWORD']
|
21
|
+
end
|
22
|
+
unless user and user.size > 0
|
23
|
+
warn "user not set"
|
24
|
+
end
|
25
|
+
unless password and password.size > 0
|
26
|
+
warn "password not set"
|
27
|
+
end
|
28
|
+
@cell = Hash.new {|h,k| h[k]=Hash.new}
|
29
|
+
@cell_type = Hash.new {|h,k| h[k]=Hash.new}
|
30
|
+
@formula = Hash.new
|
31
|
+
@first_row = Hash.new
|
32
|
+
@last_row = Hash.new
|
33
|
+
@first_column = Hash.new
|
34
|
+
@last_column = Hash.new
|
35
|
+
@cells_read = Hash.new
|
36
|
+
@header_line = 1
|
37
|
+
@date_format = '%d/%m/%Y'
|
38
|
+
@datetime_format = '%d/%m/%Y %H:%M:%S'
|
39
|
+
@time_format = '%H:%M:%S'
|
40
|
+
session = GoogleSpreadsheet.login(user, password)
|
41
|
+
@sheetlist = []
|
42
|
+
session.spreadsheet_by_key(@spreadsheetkey).worksheets.each { |sheet|
|
43
|
+
@sheetlist << sheet.title
|
44
|
+
}
|
45
|
+
@default_sheet = self.sheets.first
|
46
|
+
@worksheets = session.spreadsheet_by_key(@spreadsheetkey).worksheets
|
47
|
+
end
|
48
|
+
|
49
|
+
# returns an array of sheet names in the spreadsheet
|
50
|
+
def sheets
|
51
|
+
@sheetlist
|
52
|
+
end
|
53
|
+
|
54
|
+
def date?(string)
|
55
|
+
begin
|
56
|
+
Date.strptime(string, @date_format)
|
57
|
+
true
|
58
|
+
rescue
|
59
|
+
false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# is String a time with format HH:MM:SS?
|
64
|
+
def time?(string)
|
65
|
+
begin
|
66
|
+
DateTime.strptime(string, @time_format)
|
67
|
+
true
|
68
|
+
rescue
|
69
|
+
false
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def datetime?(string)
|
74
|
+
begin
|
75
|
+
DateTime.strptime(string, @datetime_format)
|
76
|
+
true
|
77
|
+
rescue
|
78
|
+
false
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def numeric?(string)
|
83
|
+
string =~ /^[0-9]+[\.]*[0-9]*$/
|
84
|
+
end
|
85
|
+
|
86
|
+
def timestring_to_seconds(value)
|
87
|
+
hms = value.split(':')
|
88
|
+
hms[0].to_i*3600 + hms[1].to_i*60 + hms[2].to_i
|
89
|
+
end
|
90
|
+
|
91
|
+
# Returns the content of a spreadsheet-cell.
|
92
|
+
# (1,1) is the upper left corner.
|
93
|
+
# (1,1), (1,'A'), ('A',1), ('a',1) all refers to the
|
94
|
+
# cell at the first line and first row.
|
95
|
+
def cell(row, col, sheet=nil)
|
96
|
+
sheet = @default_sheet unless sheet
|
97
|
+
check_default_sheet #TODO: 2007-12-16
|
98
|
+
read_cells(sheet) unless @cells_read[sheet]
|
99
|
+
row,col = normalize(row,col)
|
100
|
+
value = @cell[sheet]["#{row},#{col}"]
|
101
|
+
if celltype(row,col,sheet) == :date
|
102
|
+
begin
|
103
|
+
return Date.strptime(value, @date_format)
|
104
|
+
rescue ArgumentError
|
105
|
+
raise "Invalid Date #{sheet}[#{row},#{col}] #{value} using format '{@date_format}'"
|
106
|
+
end
|
107
|
+
elsif celltype(row,col,sheet) == :datetime
|
108
|
+
begin
|
109
|
+
return DateTime.strptime(value, @datetime_format)
|
110
|
+
rescue ArgumentError
|
111
|
+
raise "Invalid DateTime #{sheet}[#{row},#{col}] #{value} using format '{@datetime_format}'"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
return value
|
115
|
+
end
|
116
|
+
|
117
|
+
# returns the type of a cell:
|
118
|
+
# * :float
|
119
|
+
# * :string
|
120
|
+
# * :date
|
121
|
+
# * :percentage
|
122
|
+
# * :formula
|
123
|
+
# * :time
|
124
|
+
# * :datetime
|
125
|
+
def celltype(row, col, sheet=nil)
|
126
|
+
sheet = @default_sheet unless sheet
|
127
|
+
read_cells(sheet) unless @cells_read[sheet]
|
128
|
+
row,col = normalize(row,col)
|
129
|
+
if @formula.size > 0 && @formula[sheet]["#{row},#{col}"]
|
130
|
+
return :formula
|
131
|
+
else
|
132
|
+
@cell_type[sheet]["#{row},#{col}"]
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Returns the formula at (row,col).
|
137
|
+
# Returns nil if there is no formula.
|
138
|
+
# The method #formula? checks if there is a formula.
|
139
|
+
def formula(row,col,sheet=nil)
|
140
|
+
sheet = @default_sheet unless sheet
|
141
|
+
read_cells(sheet) unless @cells_read[sheet]
|
142
|
+
row,col = normalize(row,col)
|
143
|
+
if @formula[sheet]["#{row},#{col}"] == nil
|
144
|
+
return nil
|
145
|
+
else
|
146
|
+
return @formula[sheet]["#{row},#{col}"]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
# true, if there is a formula
|
151
|
+
def formula?(row,col,sheet=nil)
|
152
|
+
sheet = @default_sheet unless sheet
|
153
|
+
read_cells(sheet) unless @cells_read[sheet]
|
154
|
+
row,col = normalize(row,col)
|
155
|
+
formula(row,col) != nil
|
156
|
+
end
|
157
|
+
|
158
|
+
# returns each formula in the selected sheet as an array of elements
|
159
|
+
# [row, col, formula]
|
160
|
+
def formulas(sheet=nil)
|
161
|
+
theformulas = Array.new
|
162
|
+
sheet = @default_sheet unless sheet
|
163
|
+
read_cells(sheet) unless @cells_read[sheet]
|
164
|
+
first_row(sheet).upto(last_row(sheet)) {|row|
|
165
|
+
first_column(sheet).upto(last_column(sheet)) {|col|
|
166
|
+
if formula?(row,col,sheet)
|
167
|
+
f = [row, col, formula(row,col,sheet)]
|
168
|
+
theformulas << f
|
169
|
+
end
|
170
|
+
}
|
171
|
+
}
|
172
|
+
theformulas
|
173
|
+
end
|
174
|
+
|
175
|
+
# true, if the cell is empty
|
176
|
+
def empty?(row, col, sheet=nil)
|
177
|
+
value = cell(row, col, sheet)
|
178
|
+
return true unless value
|
179
|
+
return false if value.class == Date # a date is never empty
|
180
|
+
return false if value.class == Float
|
181
|
+
return false if celltype(row,col,sheet) == :time
|
182
|
+
value.empty?
|
183
|
+
end
|
184
|
+
|
185
|
+
# sets the cell to the content of 'value'
|
186
|
+
# a formula can be set in the form of '=SUM(...)'
|
187
|
+
def set_value(row,col,value,sheet=nil)
|
188
|
+
sheet = @default_sheet unless sheet
|
189
|
+
raise RangeError, "sheet not set" unless sheet
|
190
|
+
#@@ Set and pass sheet_no
|
191
|
+
begin
|
192
|
+
sheet_no = sheets.index(sheet)+1
|
193
|
+
rescue
|
194
|
+
raise RangeError, "invalid sheet '"+sheet.to_s+"'"
|
195
|
+
end
|
196
|
+
row,col = normalize(row,col)
|
197
|
+
add_to_cell_roo(row,col,value,sheet_no)
|
198
|
+
# re-read the portion of the document that has changed
|
199
|
+
if @cells_read[sheet]
|
200
|
+
key = "#{row},#{col}"
|
201
|
+
(value, value_type) = determine_datatype(value.to_s)
|
202
|
+
@cell[sheet][key] = value
|
203
|
+
@cell_type[sheet][key] = value_type
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
# returns the first non-empty row in a sheet
|
208
|
+
def first_row(sheet=nil)
|
209
|
+
sheet = @default_sheet unless sheet
|
210
|
+
unless @first_row[sheet]
|
211
|
+
sheet_no = sheets.index(sheet) + 1
|
212
|
+
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] =
|
213
|
+
oben_unten_links_rechts(sheet_no)
|
214
|
+
end
|
215
|
+
return @first_row[sheet]
|
216
|
+
end
|
217
|
+
|
218
|
+
# returns the last non-empty row in a sheet
|
219
|
+
def last_row(sheet=nil)
|
220
|
+
sheet = @default_sheet unless sheet
|
221
|
+
unless @last_row[sheet]
|
222
|
+
sheet_no = sheets.index(sheet) + 1
|
223
|
+
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] =
|
224
|
+
oben_unten_links_rechts(sheet_no)
|
225
|
+
end
|
226
|
+
return @last_row[sheet]
|
227
|
+
end
|
228
|
+
|
229
|
+
# returns the first non-empty column in a sheet
|
230
|
+
def first_column(sheet=nil)
|
231
|
+
sheet = @default_sheet unless sheet
|
232
|
+
unless @first_column[sheet]
|
233
|
+
sheet_no = sheets.index(sheet) + 1
|
234
|
+
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] =
|
235
|
+
oben_unten_links_rechts(sheet_no)
|
236
|
+
end
|
237
|
+
return @first_column[sheet]
|
238
|
+
end
|
239
|
+
|
240
|
+
# returns the last non-empty column in a sheet
|
241
|
+
def last_column(sheet=nil)
|
242
|
+
sheet = @default_sheet unless sheet
|
243
|
+
unless @last_column[sheet]
|
244
|
+
sheet_no = sheets.index(sheet) + 1
|
245
|
+
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] =
|
246
|
+
oben_unten_links_rechts(sheet_no)
|
247
|
+
end
|
248
|
+
return @last_column[sheet]
|
249
|
+
end
|
250
|
+
|
251
|
+
private
|
252
|
+
|
253
|
+
# read all cells in a sheet.
|
254
|
+
def read_cells(sheet=nil)
|
255
|
+
sheet = @default_sheet unless sheet
|
256
|
+
raise RangeError, "illegal sheet <#{sheet}>" unless sheets.index(sheet)
|
257
|
+
sheet_no = sheets.index(sheet)
|
258
|
+
ws = @worksheets[sheet_no]
|
259
|
+
for row in 1..ws.num_rows
|
260
|
+
for col in 1..ws.num_cols
|
261
|
+
key = "#{row},#{col}"
|
262
|
+
string_value = ws.input_value(row,col) # item['inputvalue'] || item['inputValue']
|
263
|
+
numeric_value = ws[row,col] #item['numericvalue'] || item['numericValue']
|
264
|
+
(value, value_type) = determine_datatype(string_value, numeric_value)
|
265
|
+
@cell[sheet][key] = value unless value == "" or value == nil
|
266
|
+
@cell_type[sheet][key] = value_type
|
267
|
+
@formula[sheet] = {} unless @formula[sheet]
|
268
|
+
@formula[sheet][key] = string_value if value_type == :formula
|
269
|
+
############
|
270
|
+
#$log.debug("key: #{key}")
|
271
|
+
#$log.debug "#{ws[row,col].inspect}"
|
272
|
+
#@cell[sheet][key] = ws[row,col]
|
273
|
+
#$log.debug "@cell[sheet][key]: #{@cell[sheet][key]}"
|
274
|
+
############
|
275
|
+
end
|
276
|
+
end
|
277
|
+
@cells_read[sheet] = true
|
278
|
+
end
|
279
|
+
|
280
|
+
def determine_datatype(val, numval=nil)
|
281
|
+
# $log.debug "val: #{val} numval: #{numval}"
|
282
|
+
if val.nil? || val[0,1] == '='
|
283
|
+
ty = :formula
|
284
|
+
if numeric?(numval)
|
285
|
+
val = numval.to_f
|
286
|
+
else
|
287
|
+
val = numval
|
288
|
+
end
|
289
|
+
else
|
290
|
+
if datetime?(val)
|
291
|
+
ty = :datetime
|
292
|
+
elsif date?(val)
|
293
|
+
ty = :date
|
294
|
+
elsif numeric?(val)
|
295
|
+
ty = :float
|
296
|
+
val = val.to_f
|
297
|
+
elsif time?(val)
|
298
|
+
ty = :time
|
299
|
+
val = timestring_to_seconds(val)
|
300
|
+
else
|
301
|
+
ty = :string
|
302
|
+
end
|
303
|
+
end
|
304
|
+
#$log.debug "val: #{val} ty: #{ty}" if ty == :date
|
305
|
+
return val, ty
|
306
|
+
end
|
307
|
+
|
308
|
+
def add_to_cell_roo(row,col,value, sheet_no=1)
|
309
|
+
sheet_no -= 1
|
310
|
+
@worksheets[sheet_no][row,col] = value
|
311
|
+
@worksheets[sheet_no].save
|
312
|
+
end
|
313
|
+
def entry_roo(value,row,col)
|
314
|
+
return value,row,col
|
315
|
+
end
|
316
|
+
|
317
|
+
def oben_unten_links_rechts(sheet_no)
|
318
|
+
ws = @worksheets[sheet_no-1]
|
319
|
+
rows = []
|
320
|
+
cols = []
|
321
|
+
for row in 1..ws.num_rows
|
322
|
+
for col in 1..ws.num_cols
|
323
|
+
rows << row if ws[row,col] and ws[row,col] != '' #TODO: besser?
|
324
|
+
cols << col if ws[row,col] and ws[row,col] != '' #TODO: besser?
|
325
|
+
end
|
326
|
+
end
|
327
|
+
return rows.min, rows.max, cols.min, cols.max
|
328
|
+
end
|
329
|
+
|
330
|
+
end # class
|