roo 1.13.2 → 2.0.0beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +7 -0
- data/.simplecov +4 -0
- data/.travis.yml +13 -0
- data/CHANGELOG +21 -0
- data/Gemfile +16 -10
- data/Guardfile +24 -0
- data/LICENSE +3 -1
- data/README.md +254 -0
- data/Rakefile +23 -23
- data/examples/roo_soap_client.rb +28 -31
- data/examples/roo_soap_server.rb +4 -6
- data/examples/write_me.rb +9 -10
- data/lib/roo.rb +18 -24
- data/lib/roo/base.rb +303 -388
- data/lib/roo/csv.rb +120 -113
- data/lib/roo/excelx.rb +452 -484
- data/lib/roo/excelx/comments.rb +24 -0
- data/lib/roo/excelx/extractor.rb +20 -0
- data/lib/roo/excelx/relationships.rb +26 -0
- data/lib/roo/excelx/shared_strings.rb +40 -0
- data/lib/roo/excelx/sheet_doc.rb +202 -0
- data/lib/roo/excelx/styles.rb +62 -0
- data/lib/roo/excelx/workbook.rb +59 -0
- data/lib/roo/font.rb +17 -0
- data/lib/roo/libre_office.rb +5 -0
- data/lib/roo/link.rb +15 -0
- data/lib/roo/{openoffice.rb → open_office.rb} +678 -496
- data/lib/roo/spreadsheet.rb +20 -23
- data/lib/roo/utils.rb +78 -0
- data/lib/roo/version.rb +3 -0
- data/roo.gemspec +20 -204
- data/spec/lib/roo/base_spec.rb +1 -4
- data/spec/lib/roo/csv_spec.rb +21 -13
- data/spec/lib/roo/excelx/format_spec.rb +7 -6
- data/spec/lib/roo/excelx_spec.rb +388 -11
- data/spec/lib/roo/libreoffice_spec.rb +16 -6
- data/spec/lib/roo/openoffice_spec.rb +2 -8
- data/spec/lib/roo/spreadsheet_spec.rb +40 -12
- data/spec/lib/roo/utils_spec.rb +106 -0
- data/spec/spec_helper.rb +2 -1
- data/test/test_generic_spreadsheet.rb +19 -67
- data/test/test_helper.rb +9 -56
- data/test/test_roo.rb +252 -477
- metadata +63 -302
- data/Gemfile.lock +0 -78
- data/README.markdown +0 -126
- data/VERSION +0 -1
- data/lib/roo/excel.rb +0 -355
- data/lib/roo/excel2003xml.rb +0 -300
- data/lib/roo/google.rb +0 -292
- data/lib/roo/roo_rails_helper.rb +0 -83
- data/lib/roo/worksheet.rb +0 -18
- data/spec/lib/roo/excel2003xml_spec.rb +0 -15
- data/spec/lib/roo/excel_spec.rb +0 -17
- data/spec/lib/roo/google_spec.rb +0 -64
- data/test/files/1900_base.xls +0 -0
- data/test/files/1900_base.xlsx +0 -0
- data/test/files/1904_base.xls +0 -0
- data/test/files/1904_base.xlsx +0 -0
- data/test/files/Bibelbund.csv +0 -3741
- data/test/files/Bibelbund.ods +0 -0
- data/test/files/Bibelbund.xls +0 -0
- data/test/files/Bibelbund.xlsx +0 -0
- data/test/files/Bibelbund.xml +0 -62518
- data/test/files/Bibelbund1.ods +0 -0
- data/test/files/Pfand_from_windows_phone.xlsx +0 -0
- data/test/files/bad_excel_date.xls +0 -0
- data/test/files/bbu.ods +0 -0
- data/test/files/bbu.xls +0 -0
- data/test/files/bbu.xlsx +0 -0
- data/test/files/bbu.xml +0 -152
- data/test/files/bode-v1.ods.zip +0 -0
- data/test/files/bode-v1.xls.zip +0 -0
- data/test/files/boolean.csv +0 -2
- data/test/files/boolean.ods +0 -0
- data/test/files/boolean.xls +0 -0
- data/test/files/boolean.xlsx +0 -0
- data/test/files/boolean.xml +0 -112
- data/test/files/borders.ods +0 -0
- data/test/files/borders.xls +0 -0
- data/test/files/borders.xlsx +0 -0
- data/test/files/borders.xml +0 -144
- data/test/files/bug-numbered-sheet-names.xlsx +0 -0
- data/test/files/bug-row-column-fixnum-float.xls +0 -0
- data/test/files/bug-row-column-fixnum-float.xml +0 -127
- data/test/files/comments.ods +0 -0
- data/test/files/comments.xls +0 -0
- data/test/files/comments.xlsx +0 -0
- data/test/files/csvtypes.csv +0 -1
- data/test/files/datetime.ods +0 -0
- data/test/files/datetime.xls +0 -0
- data/test/files/datetime.xlsx +0 -0
- data/test/files/datetime.xml +0 -142
- data/test/files/datetime_floatconv.xls +0 -0
- data/test/files/datetime_floatconv.xml +0 -148
- data/test/files/dreimalvier.ods +0 -0
- data/test/files/emptysheets.ods +0 -0
- data/test/files/emptysheets.xls +0 -0
- data/test/files/emptysheets.xlsx +0 -0
- data/test/files/emptysheets.xml +0 -105
- data/test/files/excel2003.xml +0 -21140
- data/test/files/false_encoding.xls +0 -0
- data/test/files/false_encoding.xml +0 -132
- data/test/files/file_item_error.xlsx +0 -0
- data/test/files/formula.ods +0 -0
- data/test/files/formula.xls +0 -0
- data/test/files/formula.xlsx +0 -0
- data/test/files/formula.xml +0 -134
- data/test/files/formula_parse_error.xls +0 -0
- data/test/files/formula_parse_error.xml +0 -1833
- data/test/files/formula_string_error.xlsx +0 -0
- data/test/files/html-escape.ods +0 -0
- data/test/files/link.xls +0 -0
- data/test/files/link.xlsx +0 -0
- data/test/files/matrix.ods +0 -0
- data/test/files/matrix.xls +0 -0
- data/test/files/named_cells.ods +0 -0
- data/test/files/named_cells.xls +0 -0
- data/test/files/named_cells.xlsx +0 -0
- data/test/files/no_spreadsheet_file.txt +0 -1
- data/test/files/numbers1.csv +0 -18
- data/test/files/numbers1.ods +0 -0
- data/test/files/numbers1.xls +0 -0
- data/test/files/numbers1.xlsx +0 -0
- data/test/files/numbers1.xml +0 -312
- data/test/files/numeric-link.xlsx +0 -0
- data/test/files/only_one_sheet.ods +0 -0
- data/test/files/only_one_sheet.xls +0 -0
- data/test/files/only_one_sheet.xlsx +0 -0
- data/test/files/only_one_sheet.xml +0 -67
- data/test/files/paragraph.ods +0 -0
- data/test/files/paragraph.xls +0 -0
- data/test/files/paragraph.xlsx +0 -0
- data/test/files/paragraph.xml +0 -127
- data/test/files/prova.xls +0 -0
- data/test/files/ric.ods +0 -0
- data/test/files/simple_spreadsheet.ods +0 -0
- data/test/files/simple_spreadsheet.xls +0 -0
- data/test/files/simple_spreadsheet.xlsx +0 -0
- data/test/files/simple_spreadsheet.xml +0 -225
- data/test/files/simple_spreadsheet_from_italo.ods +0 -0
- data/test/files/simple_spreadsheet_from_italo.xls +0 -0
- data/test/files/simple_spreadsheet_from_italo.xml +0 -242
- data/test/files/so_datetime.csv +0 -7
- data/test/files/style.ods +0 -0
- data/test/files/style.xls +0 -0
- data/test/files/style.xlsx +0 -0
- data/test/files/style.xml +0 -154
- data/test/files/time-test.csv +0 -2
- data/test/files/time-test.ods +0 -0
- data/test/files/time-test.xls +0 -0
- data/test/files/time-test.xlsx +0 -0
- data/test/files/time-test.xml +0 -131
- data/test/files/type_excel.ods +0 -0
- data/test/files/type_excel.xlsx +0 -0
- data/test/files/type_excelx.ods +0 -0
- data/test/files/type_excelx.xls +0 -0
- data/test/files/type_openoffice.xls +0 -0
- data/test/files/type_openoffice.xlsx +0 -0
- data/test/files/whitespace.ods +0 -0
- data/test/files/whitespace.xls +0 -0
- data/test/files/whitespace.xlsx +0 -0
- data/test/files/whitespace.xml +0 -184
- data/test/rm_sub_test.rb +0 -12
- data/test/rm_test.rb +0 -7
- data/website/index.html +0 -385
- data/website/index.txt +0 -423
- data/website/javascripts/rounded_corners_lite.inc.js +0 -285
- data/website/stylesheets/screen.css +0 -130
- data/website/template.rhtml +0 -48
data/lib/roo/excel2003xml.rb
DELETED
@@ -1,300 +0,0 @@
|
|
1
|
-
require 'date'
|
2
|
-
require 'base64'
|
3
|
-
require 'nokogiri'
|
4
|
-
|
5
|
-
class Roo::Excel2003XML < Roo::Base
|
6
|
-
|
7
|
-
# initialization and opening of a spreadsheet file
|
8
|
-
# values for packed: :zip
|
9
|
-
def initialize(filename, options={}, deprecated_file_warning=:error)
|
10
|
-
if Hash === options
|
11
|
-
packed = options[:packed]
|
12
|
-
file_warning = options[:file_warning] || :error
|
13
|
-
else
|
14
|
-
warn 'Supplying `packed` or `file_warning` as separate arguments to `Roo::Excel2003XML.new` is deprecated. Use an options hash instead.'
|
15
|
-
packed = options
|
16
|
-
file_warning = deprecated_file_warning
|
17
|
-
end
|
18
|
-
|
19
|
-
make_tmpdir do |tmpdir|
|
20
|
-
filename = download_uri(filename, tmpdir) if uri?(filename)
|
21
|
-
filename = unzip(filename, tmpdir) if packed == :zip
|
22
|
-
|
23
|
-
file_type_check(filename,'.xml','an Excel 2003 XML', file_warning)
|
24
|
-
@filename = filename
|
25
|
-
unless File.file?(@filename)
|
26
|
-
raise IOError, "file #{@filename} does not exist"
|
27
|
-
end
|
28
|
-
@doc = load_xml(@filename)
|
29
|
-
end
|
30
|
-
super(filename, options)
|
31
|
-
@formula = Hash.new
|
32
|
-
@style = Hash.new
|
33
|
-
@style_defaults = Hash.new { |h,k| h[k] = [] }
|
34
|
-
@style_definitions = Hash.new
|
35
|
-
read_styles
|
36
|
-
end
|
37
|
-
|
38
|
-
# Returns the content of a spreadsheet-cell.
|
39
|
-
# (1,1) is the upper left corner.
|
40
|
-
# (1,1), (1,'A'), ('A',1), ('a',1) all refers to the
|
41
|
-
# cell at the first line and first row.
|
42
|
-
def cell(row, col, sheet=nil)
|
43
|
-
sheet ||= @default_sheet
|
44
|
-
read_cells(sheet)
|
45
|
-
row,col = normalize(row,col)
|
46
|
-
if celltype(row,col,sheet) == :date
|
47
|
-
yyyy,mm,dd = @cell[sheet][[row,col]].split('-')
|
48
|
-
return Date.new(yyyy.to_i,mm.to_i,dd.to_i)
|
49
|
-
end
|
50
|
-
@cell[sheet][[row,col]]
|
51
|
-
end
|
52
|
-
|
53
|
-
# Returns the formula at (row,col).
|
54
|
-
# Returns nil if there is no formula.
|
55
|
-
# The method #formula? checks if there is a formula.
|
56
|
-
def formula(row,col,sheet=nil)
|
57
|
-
sheet ||= @default_sheet
|
58
|
-
read_cells(sheet)
|
59
|
-
row,col = normalize(row,col)
|
60
|
-
@formula[sheet][[row,col]] && @formula[sheet][[row,col]]["oooc:".length..-1]
|
61
|
-
end
|
62
|
-
alias_method :formula?, :formula
|
63
|
-
|
64
|
-
class Font
|
65
|
-
attr_accessor :bold, :italic, :underline
|
66
|
-
|
67
|
-
def bold?
|
68
|
-
@bold == '1'
|
69
|
-
end
|
70
|
-
|
71
|
-
def italic?
|
72
|
-
@italic == '1'
|
73
|
-
end
|
74
|
-
|
75
|
-
def underline?
|
76
|
-
@underline != nil
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# Given a cell, return the cell's style
|
81
|
-
def font(row, col, sheet=nil)
|
82
|
-
sheet ||= @default_sheet
|
83
|
-
read_cells(sheet)
|
84
|
-
row,col = normalize(row,col)
|
85
|
-
style_name = @style[sheet][[row,col]] || @style_defaults[sheet][col - 1] || 'Default'
|
86
|
-
@style_definitions[style_name]
|
87
|
-
end
|
88
|
-
|
89
|
-
# returns the type of a cell:
|
90
|
-
# * :float
|
91
|
-
# * :string
|
92
|
-
# * :date
|
93
|
-
# * :percentage
|
94
|
-
# * :formula
|
95
|
-
# * :time
|
96
|
-
# * :datetime
|
97
|
-
def celltype(row,col,sheet=nil)
|
98
|
-
sheet ||= @default_sheet
|
99
|
-
read_cells(sheet)
|
100
|
-
row,col = normalize(row,col)
|
101
|
-
if @formula[sheet][[row,col]]
|
102
|
-
return :formula
|
103
|
-
else
|
104
|
-
@cell_type[sheet][[row,col]]
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
def sheets
|
109
|
-
@doc.xpath("/ss:Workbook/ss:Worksheet").map do |sheet|
|
110
|
-
sheet['ss:Name']
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
# version of the openoffice document
|
115
|
-
# at 2007 this is always "1.0"
|
116
|
-
def officeversion
|
117
|
-
oo_version
|
118
|
-
@officeversion
|
119
|
-
end
|
120
|
-
|
121
|
-
# shows the internal representation of all cells
|
122
|
-
# mainly for debugging purposes
|
123
|
-
def to_s(sheet=nil)
|
124
|
-
sheet ||= @default_sheet
|
125
|
-
read_cells(sheet)
|
126
|
-
@cell[sheet].inspect
|
127
|
-
end
|
128
|
-
|
129
|
-
# save spreadsheet
|
130
|
-
def save #:nodoc:
|
131
|
-
42
|
132
|
-
end
|
133
|
-
|
134
|
-
# returns each formula in the selected sheet as an array of elements
|
135
|
-
# [row, col, formula]
|
136
|
-
def formulas(sheet=nil)
|
137
|
-
theformulas = Array.new
|
138
|
-
sheet ||= @default_sheet
|
139
|
-
read_cells(sheet)
|
140
|
-
first_row(sheet).upto(last_row(sheet)) {|row|
|
141
|
-
first_column(sheet).upto(last_column(sheet)) {|col|
|
142
|
-
if formula?(row,col,sheet)
|
143
|
-
f = [row, col, formula(row,col,sheet)]
|
144
|
-
theformulas << f
|
145
|
-
end
|
146
|
-
}
|
147
|
-
}
|
148
|
-
theformulas
|
149
|
-
end
|
150
|
-
|
151
|
-
private
|
152
|
-
|
153
|
-
# read the version of the OO-Version
|
154
|
-
def oo_version
|
155
|
-
@doc.find("//*[local-name()='document-content']").each do |office|
|
156
|
-
@officeversion = office['version']
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
# helper function to set the internal representation of cells
|
161
|
-
def set_cell_values(sheet,x,y,i,v,value_type,formula,table_cell,str_v,style_name)
|
162
|
-
key = [y,x+i]
|
163
|
-
@cell_type[sheet] = {} unless @cell_type[sheet]
|
164
|
-
@cell_type[sheet][key] = value_type
|
165
|
-
@formula[sheet] = {} unless @formula[sheet]
|
166
|
-
@formula[sheet][key] = formula if formula
|
167
|
-
@cell[sheet] = {} unless @cell[sheet]
|
168
|
-
@style[sheet] = {} unless @style[sheet]
|
169
|
-
@style[sheet][key] = style_name
|
170
|
-
@cell[sheet][key] =
|
171
|
-
case @cell_type[sheet][key]
|
172
|
-
when :float
|
173
|
-
v.to_f
|
174
|
-
when :string
|
175
|
-
str_v
|
176
|
-
when :datetime
|
177
|
-
DateTime.parse(v)
|
178
|
-
when :percentage
|
179
|
-
v.to_f
|
180
|
-
# when :time
|
181
|
-
# hms = v.split(':')
|
182
|
-
# hms[0].to_i*3600 + hms[1].to_i*60 + hms[2].to_i
|
183
|
-
else
|
184
|
-
v
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
# read all cells in the selected sheet
|
189
|
-
#--
|
190
|
-
# the following construct means '4 blanks'
|
191
|
-
# some content <text:s text:c="3"/>
|
192
|
-
#++
|
193
|
-
def read_cells(sheet=nil)
|
194
|
-
sheet ||= @default_sheet
|
195
|
-
validate_sheet!(sheet)
|
196
|
-
return if @cells_read[sheet]
|
197
|
-
sheet_found = false
|
198
|
-
@doc.xpath("/ss:Workbook/ss:Worksheet[@ss:Name='#{sheet}']").each do |ws|
|
199
|
-
sheet_found = true
|
200
|
-
row = 1
|
201
|
-
col = 1
|
202
|
-
column_attributes = {}
|
203
|
-
idx = 0
|
204
|
-
ws.xpath('./ss:Table/ss:Column').each do |c|
|
205
|
-
column_attributes[(idx += 1).to_s] = c['StyleID']
|
206
|
-
end
|
207
|
-
ws.xpath('./ss:Table/ss:Row').each do |r|
|
208
|
-
skip_to_row = r['Index'].to_i
|
209
|
-
row = skip_to_row if skip_to_row > 0
|
210
|
-
style_name = r['StyleID'] if r['StyleID']
|
211
|
-
r.xpath('./ss:Cell').each do |c|
|
212
|
-
skip_to_col = c['Index'].to_i
|
213
|
-
col = skip_to_col if skip_to_col > 0
|
214
|
-
if c['StyleID']
|
215
|
-
style_name = c['StyleID']
|
216
|
-
elsif
|
217
|
-
style_name ||= column_attributes[c['Index']]
|
218
|
-
end
|
219
|
-
c.xpath('./ss:Data').each do |cell|
|
220
|
-
formula = cell['Formula']
|
221
|
-
value_type = cell['ss:Type'].downcase.to_sym
|
222
|
-
v = cell.content
|
223
|
-
str_v = v
|
224
|
-
case value_type
|
225
|
-
when :number
|
226
|
-
v = v.to_f
|
227
|
-
value_type = :float
|
228
|
-
when :datetime
|
229
|
-
if v =~ /^1899-12-31T(\d{2}:\d{2}:\d{2})/
|
230
|
-
v = $1
|
231
|
-
value_type = :time
|
232
|
-
elsif v =~ /([^T]+)T00:00:00.000/
|
233
|
-
v = $1
|
234
|
-
value_type = :date
|
235
|
-
end
|
236
|
-
when :boolean
|
237
|
-
v = cell['boolean-value']
|
238
|
-
end
|
239
|
-
set_cell_values(sheet,col,row,0,v,value_type,formula,cell,str_v,style_name)
|
240
|
-
end
|
241
|
-
col += 1
|
242
|
-
end
|
243
|
-
row += 1
|
244
|
-
col = 1
|
245
|
-
end
|
246
|
-
end
|
247
|
-
if !sheet_found
|
248
|
-
raise RangeError, "Unable to find sheet #{sheet} for reading"
|
249
|
-
end
|
250
|
-
@cells_read[sheet] = true
|
251
|
-
end
|
252
|
-
|
253
|
-
def read_styles
|
254
|
-
@doc.xpath("/ss:Workbook/ss:Styles/ss:Style").each do |style|
|
255
|
-
style_id = style['ID']
|
256
|
-
@style_definitions[style_id] = Roo::Excel2003XML::Font.new
|
257
|
-
if font = style.at_xpath('./ss:Font')
|
258
|
-
@style_definitions[style_id].bold = font['Bold']
|
259
|
-
@style_definitions[style_id].italic = font['Italic']
|
260
|
-
@style_definitions[style_id].underline = font['Underline']
|
261
|
-
end
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
A_ROO_TYPE = {
|
266
|
-
"float" => :float,
|
267
|
-
"string" => :string,
|
268
|
-
"date" => :date,
|
269
|
-
"percentage" => :percentage,
|
270
|
-
"time" => :time,
|
271
|
-
}
|
272
|
-
|
273
|
-
def self.oo_type_2_roo_type(ootype)
|
274
|
-
return A_ROO_TYPE[ootype]
|
275
|
-
end
|
276
|
-
|
277
|
-
# helper method to convert compressed spaces and other elements within
|
278
|
-
# an text into a string
|
279
|
-
def children_to_string(children)
|
280
|
-
result = ''
|
281
|
-
children.each {|child|
|
282
|
-
if child.text?
|
283
|
-
result = result + child.content
|
284
|
-
else
|
285
|
-
if child.name == 's'
|
286
|
-
compressed_spaces = child['c'].to_i
|
287
|
-
# no explicit number means a count of 1:
|
288
|
-
if compressed_spaces == 0
|
289
|
-
compressed_spaces = 1
|
290
|
-
end
|
291
|
-
result = result + " "*compressed_spaces
|
292
|
-
else
|
293
|
-
result = result + child.content
|
294
|
-
end
|
295
|
-
end
|
296
|
-
}
|
297
|
-
result
|
298
|
-
end
|
299
|
-
|
300
|
-
end # class
|
data/lib/roo/google.rb
DELETED
@@ -1,292 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require "google_drive"
|
3
|
-
rescue LoadError => e
|
4
|
-
raise e, "Using Roo::Google requires the google_drive gem"
|
5
|
-
end
|
6
|
-
|
7
|
-
class Roo::Google < Roo::Base
|
8
|
-
attr_accessor :date_format, :datetime_format
|
9
|
-
|
10
|
-
# Creates a new Google Drive object.
|
11
|
-
def initialize(spreadsheet_key, options = {})
|
12
|
-
@filename = spreadsheet_key
|
13
|
-
@user = options[:user] || ENV['GOOGLE_MAIL']
|
14
|
-
@password = options[:password] || ENV['GOOGLE_PASSWORD']
|
15
|
-
@access_token = options[:access_token] || ENV['GOOGLE_TOKEN']
|
16
|
-
|
17
|
-
@worksheets = session.spreadsheet_by_key(@filename).worksheets
|
18
|
-
@sheets = @worksheets.map {|sheet| sheet.title }
|
19
|
-
super
|
20
|
-
@cell = Hash.new {|h,k| h[k]=Hash.new}
|
21
|
-
@cell_type = Hash.new {|h,k| h[k]=Hash.new}
|
22
|
-
@formula = Hash.new
|
23
|
-
@date_format = '%d/%m/%Y'
|
24
|
-
@datetime_format = '%d/%m/%Y %H:%M:%S'
|
25
|
-
@time_format = '%H:%M:%S'
|
26
|
-
end
|
27
|
-
|
28
|
-
# returns an array of sheet names in the spreadsheet
|
29
|
-
attr_reader :sheets
|
30
|
-
|
31
|
-
def date?(string)
|
32
|
-
Date.strptime(string, @date_format)
|
33
|
-
true
|
34
|
-
rescue
|
35
|
-
false
|
36
|
-
end
|
37
|
-
|
38
|
-
# is String a time with format HH:MM:SS?
|
39
|
-
def time?(string)
|
40
|
-
DateTime.strptime(string, @time_format)
|
41
|
-
true
|
42
|
-
rescue
|
43
|
-
false
|
44
|
-
end
|
45
|
-
|
46
|
-
def datetime?(string)
|
47
|
-
DateTime.strptime(string, @datetime_format)
|
48
|
-
true
|
49
|
-
rescue
|
50
|
-
false
|
51
|
-
end
|
52
|
-
|
53
|
-
def numeric?(string)
|
54
|
-
string =~ /^[0-9]+[\.]*[0-9]*$/
|
55
|
-
end
|
56
|
-
|
57
|
-
def timestring_to_seconds(value)
|
58
|
-
hms = value.split(':')
|
59
|
-
hms[0].to_i*3600 + hms[1].to_i*60 + hms[2].to_i
|
60
|
-
end
|
61
|
-
|
62
|
-
# Returns the content of a spreadsheet-cell.
|
63
|
-
# (1,1) is the upper left corner.
|
64
|
-
# (1,1), (1,'A'), ('A',1), ('a',1) all refers to the
|
65
|
-
# cell at the first line and first row.
|
66
|
-
def cell(row, col, sheet=nil)
|
67
|
-
sheet ||= @default_sheet
|
68
|
-
validate_sheet!(sheet) #TODO: 2007-12-16
|
69
|
-
read_cells(sheet)
|
70
|
-
row,col = normalize(row,col)
|
71
|
-
value = @cell[sheet]["#{row},#{col}"]
|
72
|
-
if celltype(row,col,sheet) == :date
|
73
|
-
begin
|
74
|
-
return Date.strptime(value, @date_format)
|
75
|
-
rescue ArgumentError
|
76
|
-
raise "Invalid Date #{sheet}[#{row},#{col}] #{value} using format '{@date_format}'"
|
77
|
-
end
|
78
|
-
elsif celltype(row,col,sheet) == :datetime
|
79
|
-
begin
|
80
|
-
return DateTime.strptime(value, @datetime_format)
|
81
|
-
rescue ArgumentError
|
82
|
-
raise "Invalid DateTime #{sheet}[#{row},#{col}] #{value} using format '{@datetime_format}'"
|
83
|
-
end
|
84
|
-
end
|
85
|
-
return value
|
86
|
-
end
|
87
|
-
|
88
|
-
# returns the type of a cell:
|
89
|
-
# * :float
|
90
|
-
# * :string
|
91
|
-
# * :date
|
92
|
-
# * :percentage
|
93
|
-
# * :formula
|
94
|
-
# * :time
|
95
|
-
# * :datetime
|
96
|
-
def celltype(row, col, sheet=nil)
|
97
|
-
sheet ||= @default_sheet
|
98
|
-
read_cells(sheet)
|
99
|
-
row,col = normalize(row,col)
|
100
|
-
if @formula.size > 0 && @formula[sheet]["#{row},#{col}"]
|
101
|
-
:formula
|
102
|
-
else
|
103
|
-
@cell_type[sheet]["#{row},#{col}"]
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
# Returns the formula at (row,col).
|
108
|
-
# Returns nil if there is no formula.
|
109
|
-
# The method #formula? checks if there is a formula.
|
110
|
-
def formula(row,col,sheet=nil)
|
111
|
-
sheet ||= @default_sheet
|
112
|
-
read_cells(sheet)
|
113
|
-
row,col = normalize(row,col)
|
114
|
-
@formula[sheet]["#{row},#{col}"] && @formula[sheet]["#{row},#{col}"]
|
115
|
-
end
|
116
|
-
alias_method :formula?, :formula
|
117
|
-
|
118
|
-
# true, if the cell is empty
|
119
|
-
def empty?(row, col, sheet=nil)
|
120
|
-
value = cell(row, col, sheet)
|
121
|
-
return true unless value
|
122
|
-
return false if value.class == Date # a date is never empty
|
123
|
-
return false if value.class == Float
|
124
|
-
return false if celltype(row,col,sheet) == :time
|
125
|
-
value.empty?
|
126
|
-
end
|
127
|
-
|
128
|
-
# sets the cell to the content of 'value'
|
129
|
-
# a formula can be set in the form of '=SUM(...)'
|
130
|
-
def set(row,col,value,sheet=nil)
|
131
|
-
sheet ||= @default_sheet
|
132
|
-
validate_sheet!(sheet)
|
133
|
-
|
134
|
-
sheet_no = sheets.index(sheet)+1
|
135
|
-
row,col = normalize(row,col)
|
136
|
-
add_to_cell_roo(row,col,value,sheet_no)
|
137
|
-
# re-read the portion of the document that has changed
|
138
|
-
if @cells_read[sheet]
|
139
|
-
value, value_type = determine_datatype(value.to_s)
|
140
|
-
|
141
|
-
_set_value(row,col,value,sheet)
|
142
|
-
set_type(row,col,value_type,sheet)
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
# *DEPRECATED*: Use Roo::Google#set instead
|
147
|
-
def set_value(row,col,value,sheet=nil)
|
148
|
-
warn "[DEPRECATION] `set_value` is deprecated. Please use `set` instead."
|
149
|
-
set(row,col,value,sheet)
|
150
|
-
end
|
151
|
-
|
152
|
-
# returns the first non-empty row in a sheet
|
153
|
-
def first_row(sheet=nil)
|
154
|
-
sheet ||= @default_sheet
|
155
|
-
unless @first_row[sheet]
|
156
|
-
sheet_no = sheets.index(sheet) + 1
|
157
|
-
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] =
|
158
|
-
oben_unten_links_rechts(sheet_no)
|
159
|
-
end
|
160
|
-
return @first_row[sheet]
|
161
|
-
end
|
162
|
-
|
163
|
-
# returns the last non-empty row in a sheet
|
164
|
-
def last_row(sheet=nil)
|
165
|
-
sheet ||= @default_sheet
|
166
|
-
unless @last_row[sheet]
|
167
|
-
sheet_no = sheets.index(sheet) + 1
|
168
|
-
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] =
|
169
|
-
oben_unten_links_rechts(sheet_no)
|
170
|
-
end
|
171
|
-
return @last_row[sheet]
|
172
|
-
end
|
173
|
-
|
174
|
-
# returns the first non-empty column in a sheet
|
175
|
-
def first_column(sheet=nil)
|
176
|
-
sheet ||= @default_sheet
|
177
|
-
unless @first_column[sheet]
|
178
|
-
sheet_no = sheets.index(sheet) + 1
|
179
|
-
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] =
|
180
|
-
oben_unten_links_rechts(sheet_no)
|
181
|
-
end
|
182
|
-
return @first_column[sheet]
|
183
|
-
end
|
184
|
-
|
185
|
-
# returns the last non-empty column in a sheet
|
186
|
-
def last_column(sheet=nil)
|
187
|
-
sheet ||= @default_sheet
|
188
|
-
unless @last_column[sheet]
|
189
|
-
sheet_no = sheets.index(sheet) + 1
|
190
|
-
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] =
|
191
|
-
oben_unten_links_rechts(sheet_no)
|
192
|
-
end
|
193
|
-
return @last_column[sheet]
|
194
|
-
end
|
195
|
-
|
196
|
-
private
|
197
|
-
|
198
|
-
def _set_value(row,col,value,sheet=nil)
|
199
|
-
sheet ||= @default_sheet
|
200
|
-
@cell[sheet][ "#{row},#{col}"] = value
|
201
|
-
end
|
202
|
-
|
203
|
-
def set_type(row,col,type,sheet=nil)
|
204
|
-
sheet ||= @default_sheet
|
205
|
-
@cell_type[sheet]["#{row},#{col}"] = type
|
206
|
-
end
|
207
|
-
|
208
|
-
# read all cells in a sheet.
|
209
|
-
def read_cells(sheet=nil)
|
210
|
-
sheet ||= @default_sheet
|
211
|
-
validate_sheet!(sheet)
|
212
|
-
return if @cells_read[sheet]
|
213
|
-
|
214
|
-
sheet_no = sheets.index(sheet)
|
215
|
-
ws = @worksheets[sheet_no]
|
216
|
-
for row in 1..ws.num_rows
|
217
|
-
for col in 1..ws.num_cols
|
218
|
-
key = "#{row},#{col}"
|
219
|
-
string_value = ws.input_value(row,col) # item['inputvalue'] || item['inputValue']
|
220
|
-
numeric_value = ws[row,col] #item['numericvalue'] || item['numericValue']
|
221
|
-
(value, value_type) = determine_datatype(string_value, numeric_value)
|
222
|
-
@cell[sheet][key] = value unless value == "" or value == nil
|
223
|
-
@cell_type[sheet][key] = value_type
|
224
|
-
@formula[sheet] = {} unless @formula[sheet]
|
225
|
-
@formula[sheet][key] = string_value if value_type == :formula
|
226
|
-
end
|
227
|
-
end
|
228
|
-
@cells_read[sheet] = true
|
229
|
-
end
|
230
|
-
|
231
|
-
def determine_datatype(val, numval=nil)
|
232
|
-
if val.nil? || val[0,1] == '='
|
233
|
-
ty = :formula
|
234
|
-
if numeric?(numval)
|
235
|
-
val = numval.to_f
|
236
|
-
else
|
237
|
-
val = numval
|
238
|
-
end
|
239
|
-
else
|
240
|
-
if datetime?(val)
|
241
|
-
ty = :datetime
|
242
|
-
elsif date?(val)
|
243
|
-
ty = :date
|
244
|
-
elsif numeric?(val)
|
245
|
-
ty = :float
|
246
|
-
val = val.to_f
|
247
|
-
elsif time?(val)
|
248
|
-
ty = :time
|
249
|
-
val = timestring_to_seconds(val)
|
250
|
-
else
|
251
|
-
ty = :string
|
252
|
-
end
|
253
|
-
end
|
254
|
-
return val, ty
|
255
|
-
end
|
256
|
-
|
257
|
-
def add_to_cell_roo(row,col,value, sheet_no=1)
|
258
|
-
sheet_no -= 1
|
259
|
-
@worksheets[sheet_no][row,col] = value
|
260
|
-
@worksheets[sheet_no].save
|
261
|
-
end
|
262
|
-
def entry_roo(value,row,col)
|
263
|
-
return value,row,col
|
264
|
-
end
|
265
|
-
|
266
|
-
def oben_unten_links_rechts(sheet_no)
|
267
|
-
ws = @worksheets[sheet_no-1]
|
268
|
-
rows = []
|
269
|
-
cols = []
|
270
|
-
for row in 1..ws.num_rows
|
271
|
-
for col in 1..ws.num_cols
|
272
|
-
rows << row if ws[row,col] and ws[row,col] != '' #TODO: besser?
|
273
|
-
cols << col if ws[row,col] and ws[row,col] != '' #TODO: besser?
|
274
|
-
end
|
275
|
-
end
|
276
|
-
return rows.min, rows.max, cols.min, cols.max
|
277
|
-
end
|
278
|
-
|
279
|
-
def reinitialize
|
280
|
-
initialize(@filename, user: @user, password: @password, access_token: @access_token)
|
281
|
-
end
|
282
|
-
|
283
|
-
def session
|
284
|
-
@session ||= if @user && @password
|
285
|
-
GoogleDrive.login(@user, @password)
|
286
|
-
elsif @access_token
|
287
|
-
GoogleDrive.login_with_oauth(@access_token)
|
288
|
-
else
|
289
|
-
warn 'set user and password or access token'
|
290
|
-
end
|
291
|
-
end
|
292
|
-
end
|