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
@@ -0,0 +1,451 @@
|
|
1
|
+
require 'xml'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'zip/zipfilesystem'
|
4
|
+
require 'date'
|
5
|
+
require 'base64'
|
6
|
+
require 'cgi'
|
7
|
+
|
8
|
+
class Openoffice < GenericSpreadsheet
|
9
|
+
|
10
|
+
@@nr = 0
|
11
|
+
|
12
|
+
# initialization and opening of a spreadsheet file
|
13
|
+
# values for packed: :zip
|
14
|
+
def initialize(filename, packed=nil, file_warning=:error) #, create = false)
|
15
|
+
@file_warning = file_warning
|
16
|
+
super()
|
17
|
+
@tmpdir = "oo_"+$$.to_s
|
18
|
+
@tmpdir = File.join(ENV['ROO_TMP'], @tmpdir) if ENV['ROO_TMP']
|
19
|
+
unless File.exists?(@tmpdir)
|
20
|
+
FileUtils::mkdir(@tmpdir)
|
21
|
+
end
|
22
|
+
filename = open_from_uri(filename) if filename[0,7] == "http://"
|
23
|
+
filename = unzip(filename) if packed and packed == :zip
|
24
|
+
begin
|
25
|
+
file_type_check(filename,'.ods','an openoffice')
|
26
|
+
#if create and ! File.exists?(filename)
|
27
|
+
# self.create_openoffice(filename)
|
28
|
+
#end
|
29
|
+
@cells_read = Hash.new
|
30
|
+
#TODO: @cells_read[:default] = false
|
31
|
+
@filename = filename
|
32
|
+
unless File.file?(@filename)
|
33
|
+
raise IOError, "file #{@filename} does not exist"
|
34
|
+
end
|
35
|
+
@@nr += 1
|
36
|
+
@file_nr = @@nr
|
37
|
+
extract_content
|
38
|
+
file = File.new(File.join(@tmpdir, @file_nr.to_s+"_roo_content.xml"))
|
39
|
+
@doc = XML::Parser.io(file).parse
|
40
|
+
file.close
|
41
|
+
ensure
|
42
|
+
#if ENV["roo_local"] != "thomas-p"
|
43
|
+
FileUtils::rm_r(@tmpdir)
|
44
|
+
#end
|
45
|
+
end
|
46
|
+
@default_sheet = self.sheets.first
|
47
|
+
@cell = Hash.new
|
48
|
+
@cell_type = Hash.new
|
49
|
+
@formula = Hash.new
|
50
|
+
@first_row = Hash.new
|
51
|
+
@last_row = Hash.new
|
52
|
+
@first_column = Hash.new
|
53
|
+
@last_column = Hash.new
|
54
|
+
@style = Hash.new
|
55
|
+
@style_defaults = Hash.new { |h,k| h[k] = [] }
|
56
|
+
@style_definitions = Hash.new
|
57
|
+
@header_line = 1
|
58
|
+
end
|
59
|
+
|
60
|
+
# creates a new empty openoffice-spreadsheet file
|
61
|
+
def create_openoffice(filename) #:nodoc:
|
62
|
+
#TODO: a better way for creating the file contents
|
63
|
+
# now you have to call mkbase64...rb to create an include file with all
|
64
|
+
# the empty files in an openoffice zip-file
|
65
|
+
load 'base64include.rb'
|
66
|
+
# puts @@empty_spreadsheet
|
67
|
+
f = File.open(filename,'wb')
|
68
|
+
f.print(Base64.decode64(@@empty_spreadsheet))
|
69
|
+
f.close
|
70
|
+
end
|
71
|
+
|
72
|
+
# Returns the content of a spreadsheet-cell.
|
73
|
+
# (1,1) is the upper left corner.
|
74
|
+
# (1,1), (1,'A'), ('A',1), ('a',1) all refers to the
|
75
|
+
# cell at the first line and first row.
|
76
|
+
def cell(row, col, sheet=nil)
|
77
|
+
sheet = @default_sheet unless sheet
|
78
|
+
read_cells(sheet) unless @cells_read[sheet]
|
79
|
+
row,col = normalize(row,col)
|
80
|
+
if celltype(row,col,sheet) == :date
|
81
|
+
yyyy,mm,dd = @cell[sheet][[row,col]].split('-')
|
82
|
+
return Date.new(yyyy.to_i,mm.to_i,dd.to_i)
|
83
|
+
end
|
84
|
+
@cell[sheet][[row,col]]
|
85
|
+
end
|
86
|
+
|
87
|
+
# Returns the formula at (row,col).
|
88
|
+
# Returns nil if there is no formula.
|
89
|
+
# The method #formula? checks if there is a formula.
|
90
|
+
def formula(row,col,sheet=nil)
|
91
|
+
sheet = @default_sheet unless sheet
|
92
|
+
read_cells(sheet) unless @cells_read[sheet]
|
93
|
+
row,col = normalize(row,col)
|
94
|
+
if @formula[sheet][[row,col]] == nil
|
95
|
+
return nil
|
96
|
+
else
|
97
|
+
return @formula[sheet][[row,col]]["oooc:".length..-1]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# true, if there is a formula
|
102
|
+
def formula?(row,col,sheet=nil)
|
103
|
+
sheet = @default_sheet unless sheet
|
104
|
+
read_cells(sheet) unless @cells_read[sheet]
|
105
|
+
row,col = normalize(row,col)
|
106
|
+
formula(row,col) != nil
|
107
|
+
end
|
108
|
+
|
109
|
+
class Font
|
110
|
+
attr_accessor :bold, :italic, :underline
|
111
|
+
|
112
|
+
def bold?
|
113
|
+
@bold == 'bold'
|
114
|
+
end
|
115
|
+
|
116
|
+
def italic?
|
117
|
+
@italic == 'italic'
|
118
|
+
end
|
119
|
+
|
120
|
+
def underline?
|
121
|
+
@underline != nil
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# Given a cell, return the cell's style
|
126
|
+
def font(row, col, sheet=nil)
|
127
|
+
sheet = @default_sheet unless sheet
|
128
|
+
read_cells(sheet) unless @cells_read[sheet]
|
129
|
+
row,col = normalize(row,col)
|
130
|
+
style_name = @style[sheet][[row,col]] || @style_defaults[sheet][col - 1] || 'Default'
|
131
|
+
@style_definitions[style_name]
|
132
|
+
end
|
133
|
+
|
134
|
+
# set a cell to a certain value
|
135
|
+
# (this will not be saved back to the spreadsheet file!)
|
136
|
+
def set(row,col,value,sheet=nil) #:nodoc:
|
137
|
+
sheet = @default_sheet unless sheet
|
138
|
+
read_cells(sheet) unless @cells_read[sheet]
|
139
|
+
row,col = normalize(row,col)
|
140
|
+
set_value(row,col,value,sheet)
|
141
|
+
if value.class == Fixnum
|
142
|
+
set_type(row,col,:float,sheet)
|
143
|
+
elsif value.class == String
|
144
|
+
set_type(row,col,:string,sheet)
|
145
|
+
elsif value.class == Float
|
146
|
+
set_type(row,col,:string,sheet)
|
147
|
+
else
|
148
|
+
raise ArgumentError, "Type for "+value.to_s+" not set"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# returns the type of a cell:
|
153
|
+
# * :float
|
154
|
+
# * :string
|
155
|
+
# * :date
|
156
|
+
# * :percentage
|
157
|
+
# * :formula
|
158
|
+
# * :time
|
159
|
+
# * :datetime
|
160
|
+
def celltype(row,col,sheet=nil)
|
161
|
+
sheet = @default_sheet unless sheet
|
162
|
+
read_cells(sheet) unless @cells_read[sheet]
|
163
|
+
row,col = normalize(row,col)
|
164
|
+
if @formula[sheet][[row,col]]
|
165
|
+
return :formula
|
166
|
+
else
|
167
|
+
@cell_type[sheet][[row,col]]
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def sheets
|
172
|
+
return_sheets = []
|
173
|
+
@doc.find("//*[local-name()='table']").each do |sheet|
|
174
|
+
return_sheets << sheet.attributes['name']
|
175
|
+
end
|
176
|
+
return_sheets
|
177
|
+
end
|
178
|
+
|
179
|
+
# version of the openoffice document
|
180
|
+
# at 2007 this is always "1.0"
|
181
|
+
def officeversion
|
182
|
+
oo_version
|
183
|
+
@officeversion
|
184
|
+
end
|
185
|
+
|
186
|
+
# shows the internal representation of all cells
|
187
|
+
# mainly for debugging purposes
|
188
|
+
def to_s(sheet=nil)
|
189
|
+
sheet = @default_sheet unless sheet
|
190
|
+
read_cells(sheet) unless @cells_read[sheet]
|
191
|
+
@cell[sheet].inspect
|
192
|
+
end
|
193
|
+
|
194
|
+
# save spreadsheet
|
195
|
+
def save #:nodoc:
|
196
|
+
42
|
197
|
+
end
|
198
|
+
|
199
|
+
# returns each formula in the selected sheet as an array of elements
|
200
|
+
# [row, col, formula]
|
201
|
+
def formulas(sheet=nil)
|
202
|
+
theformulas = Array.new
|
203
|
+
sheet = @default_sheet unless sheet
|
204
|
+
read_cells(sheet) unless @cells_read[sheet]
|
205
|
+
first_row(sheet).upto(last_row(sheet)) {|row|
|
206
|
+
first_column(sheet).upto(last_column(sheet)) {|col|
|
207
|
+
if formula?(row,col,sheet)
|
208
|
+
f = [row, col, formula(row,col,sheet)]
|
209
|
+
theformulas << f
|
210
|
+
end
|
211
|
+
}
|
212
|
+
}
|
213
|
+
theformulas
|
214
|
+
end
|
215
|
+
|
216
|
+
private
|
217
|
+
|
218
|
+
# read the version of the OO-Version
|
219
|
+
def oo_version
|
220
|
+
@doc.find("//*[local-name()='document-content']").each do |office|
|
221
|
+
@officeversion = office.attributes['version']
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
# helper function to set the internal representation of cells
|
226
|
+
def set_cell_values(sheet,x,y,i,v,vt,formula,table_cell,str_v,style_name)
|
227
|
+
key = [y,x+i]
|
228
|
+
@cell_type[sheet] = {} unless @cell_type[sheet]
|
229
|
+
@cell_type[sheet][key] = Openoffice.oo_type_2_roo_type(vt)
|
230
|
+
@formula[sheet] = {} unless @formula[sheet]
|
231
|
+
@formula[sheet][key] = formula if formula
|
232
|
+
@cell[sheet] = {} unless @cell[sheet]
|
233
|
+
@style[sheet] = {} unless @style[sheet]
|
234
|
+
@style[sheet][key] = style_name
|
235
|
+
case @cell_type[sheet][key]
|
236
|
+
when :float
|
237
|
+
@cell[sheet][key] = v.to_f
|
238
|
+
when :string
|
239
|
+
@cell[sheet][key] = str_v
|
240
|
+
when :date
|
241
|
+
if table_cell.attributes['date-value'].size != "XXXX-XX-XX".size
|
242
|
+
#-- dann ist noch eine Uhrzeit vorhanden
|
243
|
+
#-- "1961-11-21T12:17:18"
|
244
|
+
@cell[sheet][key] = DateTime.parse(table_cell.attributes['date-value'])
|
245
|
+
@cell_type[sheet][key] = :datetime
|
246
|
+
else
|
247
|
+
@cell[sheet][key] = table_cell.attributes['date-value']
|
248
|
+
end
|
249
|
+
when :percentage
|
250
|
+
@cell[sheet][key] = v.to_f
|
251
|
+
when :time
|
252
|
+
hms = v.split(':')
|
253
|
+
@cell[sheet][key] = hms[0].to_i*3600 + hms[1].to_i*60 + hms[2].to_i
|
254
|
+
else
|
255
|
+
@cell[sheet][key] = v
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
# read all cells in the selected sheet
|
260
|
+
#--
|
261
|
+
# the following construct means '4 blanks'
|
262
|
+
# some content <text:s text:c="3"/>
|
263
|
+
#++
|
264
|
+
def read_cells(sheet=nil)
|
265
|
+
sheet = @default_sheet unless sheet
|
266
|
+
sheet_found = false
|
267
|
+
raise ArgumentError, "Error: sheet '#{sheet||'nil'}' not valid" if @default_sheet == nil and sheet==nil
|
268
|
+
raise RangeError unless self.sheets.include? sheet
|
269
|
+
|
270
|
+
@doc.find("//*[local-name()='table']").each do |ws|
|
271
|
+
if sheet == ws.attributes['name']
|
272
|
+
sheet_found = true
|
273
|
+
col = 1
|
274
|
+
row = 1
|
275
|
+
ws.each_element do |table_element|
|
276
|
+
case table_element.name
|
277
|
+
when 'table-column'
|
278
|
+
@style_defaults[sheet] << table_element.attributes['default-cell-style-name']
|
279
|
+
when 'table-row'
|
280
|
+
if table_element.attributes['number-rows-repeated']
|
281
|
+
skip_row = table_element.attributes['number-rows-repeated'].to_i
|
282
|
+
row = row + skip_row - 1
|
283
|
+
end
|
284
|
+
table_element.each_element do |cell|
|
285
|
+
skip_col = cell.attributes['number-columns-repeated']
|
286
|
+
formula = cell.attributes['formula']
|
287
|
+
vt = cell.attributes['value-type']
|
288
|
+
v = cell.attributes['value']
|
289
|
+
style_name = cell.attributes['style-name']
|
290
|
+
if vt == 'string'
|
291
|
+
str_v = ''
|
292
|
+
# insert \n if there is more than one paragraph
|
293
|
+
para_count = 0
|
294
|
+
cell.each_element do |str|
|
295
|
+
if str.name == 'p'
|
296
|
+
v = str.content
|
297
|
+
str_v += "\n" if para_count > 0
|
298
|
+
para_count += 1
|
299
|
+
if str.children.size > 1
|
300
|
+
str_v += children_to_string(str.children)
|
301
|
+
else
|
302
|
+
str.children.each do |child|
|
303
|
+
str_v += child.content #.text
|
304
|
+
end
|
305
|
+
end
|
306
|
+
str_v.gsub!(/'/,"'") # special case not supported by unescapeHTML
|
307
|
+
str_v = CGI.unescapeHTML(str_v)
|
308
|
+
end # == 'p'
|
309
|
+
end
|
310
|
+
elsif vt == 'time'
|
311
|
+
cell.each_element do |str|
|
312
|
+
if str.name == 'p'
|
313
|
+
v = str.content
|
314
|
+
end
|
315
|
+
end
|
316
|
+
elsif vt == '' or vt == nil
|
317
|
+
#
|
318
|
+
elsif vt == 'date'
|
319
|
+
#
|
320
|
+
elsif vt == 'percentage'
|
321
|
+
#
|
322
|
+
elsif vt == 'float'
|
323
|
+
#
|
324
|
+
elsif vt == 'boolean'
|
325
|
+
v = cell.attributes['boolean-value']
|
326
|
+
#
|
327
|
+
else
|
328
|
+
# raise "unknown type #{vt}"
|
329
|
+
end
|
330
|
+
if skip_col
|
331
|
+
if v != nil or cell.attributes['date-value']
|
332
|
+
0.upto(skip_col.to_i-1) do |i|
|
333
|
+
set_cell_values(sheet,col,row,i,v,vt,formula,cell,str_v,style_name)
|
334
|
+
end
|
335
|
+
end
|
336
|
+
col += (skip_col.to_i - 1)
|
337
|
+
end # if skip
|
338
|
+
set_cell_values(sheet,col,row,0,v,vt,formula,cell,str_v,style_name)
|
339
|
+
col += 1
|
340
|
+
end
|
341
|
+
row += 1
|
342
|
+
col = 1
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
@doc.find("//*[local-name()='automatic-styles']").each do |style|
|
349
|
+
read_styles(style)
|
350
|
+
end
|
351
|
+
if !sheet_found
|
352
|
+
raise RangeError
|
353
|
+
end
|
354
|
+
@cells_read[sheet] = true
|
355
|
+
end
|
356
|
+
|
357
|
+
def read_styles(style_elements)
|
358
|
+
@style_definitions['Default'] = Openoffice::Font.new
|
359
|
+
style_elements.each do |style|
|
360
|
+
next unless style.name == 'style'
|
361
|
+
style_name = style.attributes['name']
|
362
|
+
style.each do |properties|
|
363
|
+
font = Openoffice::Font.new
|
364
|
+
font.bold = properties.attributes['font-weight']
|
365
|
+
font.italic = properties.attributes['font-style']
|
366
|
+
font.underline = properties.attributes['text-underline-style']
|
367
|
+
@style_definitions[style_name] = font
|
368
|
+
end
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
# Checks if the default_sheet exists. If not an RangeError exception is
|
373
|
+
# raised
|
374
|
+
def check_default_sheet
|
375
|
+
sheet_found = false
|
376
|
+
raise ArgumentError, "Error: default_sheet not set" if @default_sheet == nil
|
377
|
+
sheet_found = true if sheets.include?(@default_sheet)
|
378
|
+
if ! sheet_found
|
379
|
+
raise RangeError, "sheet '#{@default_sheet}' not found"
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
def process_zipfile(zip, path='')
|
384
|
+
if zip.file.file? path
|
385
|
+
if path == "content.xml"
|
386
|
+
open(File.join(@tmpdir, @file_nr.to_s+'_roo_content.xml'),'wb') {|f|
|
387
|
+
f << zip.read(path)
|
388
|
+
}
|
389
|
+
end
|
390
|
+
else
|
391
|
+
unless path.empty?
|
392
|
+
path += '/'
|
393
|
+
end
|
394
|
+
zip.dir.foreach(path) do |filename|
|
395
|
+
process_zipfile(zip, path+filename)
|
396
|
+
end
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
def extract_content
|
401
|
+
Zip::ZipFile.open(@filename) do |zip|
|
402
|
+
process_zipfile(zip)
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
def set_value(row,col,value,sheet=nil)
|
407
|
+
sheet = @default_value unless sheet
|
408
|
+
@cell[sheet][[row,col]] = value
|
409
|
+
end
|
410
|
+
|
411
|
+
def set_type(row,col,type,sheet=nil)
|
412
|
+
sheet = @default_value unless sheet
|
413
|
+
@cell_type[sheet][[row,col]] = type
|
414
|
+
end
|
415
|
+
|
416
|
+
A_ROO_TYPE = {
|
417
|
+
"float" => :float,
|
418
|
+
"string" => :string,
|
419
|
+
"date" => :date,
|
420
|
+
"percentage" => :percentage,
|
421
|
+
"time" => :time,
|
422
|
+
}
|
423
|
+
|
424
|
+
def Openoffice.oo_type_2_roo_type(ootype)
|
425
|
+
return A_ROO_TYPE[ootype]
|
426
|
+
end
|
427
|
+
|
428
|
+
# helper method to convert compressed spaces and other elements within
|
429
|
+
# an text into a string
|
430
|
+
def children_to_string(children)
|
431
|
+
result = ''
|
432
|
+
children.each {|child|
|
433
|
+
if child.text?
|
434
|
+
result = result + child.content
|
435
|
+
else
|
436
|
+
if child.name == 's'
|
437
|
+
compressed_spaces = child.attributes['c'].to_i
|
438
|
+
# no explicit number means a count of 1:
|
439
|
+
if compressed_spaces == 0
|
440
|
+
compressed_spaces = 1
|
441
|
+
end
|
442
|
+
result = result + " "*compressed_spaces
|
443
|
+
else
|
444
|
+
result = result + child.content
|
445
|
+
end
|
446
|
+
end
|
447
|
+
}
|
448
|
+
result
|
449
|
+
end
|
450
|
+
|
451
|
+
end # class
|
@@ -0,0 +1,82 @@
|
|
1
|
+
def spreadsheet(spreadsheet, sheets, options={})
|
2
|
+
@rspreadsheet = spreadsheet
|
3
|
+
coordinates = true # default
|
4
|
+
o=""
|
5
|
+
if options[:coordinates] != nil
|
6
|
+
o << ":coordinates uebergeben: #{options[:coordinates]}"
|
7
|
+
coordinates = options[:coordinates]
|
8
|
+
end
|
9
|
+
if options[:bgcolor]
|
10
|
+
bgcolor = options[:bgcolor]
|
11
|
+
else
|
12
|
+
bgcolor = false
|
13
|
+
end
|
14
|
+
|
15
|
+
sheets.each { |sheet|
|
16
|
+
@rspreadsheet.default_sheet = sheet
|
17
|
+
linenumber = @rspreadsheet.first_row(sheet)
|
18
|
+
if options[:first_row]
|
19
|
+
linenumber += (options[:first_row]-1)
|
20
|
+
end
|
21
|
+
o << '<table border="0" cellspacing="1" cellpadding="1">'
|
22
|
+
if options[:first_row]
|
23
|
+
first_row = options[:first_row]
|
24
|
+
end
|
25
|
+
if options[:last_row]
|
26
|
+
last_row = options[:last_row]
|
27
|
+
end
|
28
|
+
if options[:first_column]
|
29
|
+
first_column = options[:first_column]
|
30
|
+
end
|
31
|
+
if options[:last_column]
|
32
|
+
last_column = options[:last_column]
|
33
|
+
end
|
34
|
+
first_row = @rspreadsheet.first_row(sheet) unless first_row
|
35
|
+
last_row = @rspreadsheet.last_row(sheet) unless last_row
|
36
|
+
first_column = @rspreadsheet.first_column(sheet) unless first_column
|
37
|
+
last_column = @rspreadsheet.last_column(sheet) unless last_column
|
38
|
+
if coordinates
|
39
|
+
o << " <tr>"
|
40
|
+
o << " <td> </td>"
|
41
|
+
@rspreadsheet.first_column(sheet).upto(@rspreadsheet.last_column(sheet)) {|c|
|
42
|
+
if c < first_column or c > last_column
|
43
|
+
next
|
44
|
+
end
|
45
|
+
o << " <td>"
|
46
|
+
o << " <b>#{GenericSpreadsheet.number_to_letter(c)}</b>"
|
47
|
+
o << " </td>"
|
48
|
+
}
|
49
|
+
o << "</tr>"
|
50
|
+
end
|
51
|
+
@rspreadsheet.first_row.upto(@rspreadsheet.last_row) do |y|
|
52
|
+
if first_row and (y < first_row or y > last_row)
|
53
|
+
next
|
54
|
+
end
|
55
|
+
o << "<tr>"
|
56
|
+
if coordinates
|
57
|
+
o << "<td><b>#{linenumber.to_s}</b></td>"
|
58
|
+
end
|
59
|
+
linenumber += 1
|
60
|
+
@rspreadsheet.first_column(sheet).upto(@rspreadsheet.last_column(sheet)) do |x|
|
61
|
+
if x < first_column or x > last_column
|
62
|
+
next
|
63
|
+
end
|
64
|
+
if bgcolor
|
65
|
+
o << "<td bgcolor=\"#{bgcolor}\">"
|
66
|
+
else
|
67
|
+
o << '<td bgcolor="lightgreen">'
|
68
|
+
end
|
69
|
+
if @rspreadsheet.cell(y,x).to_s.empty?
|
70
|
+
o << " "
|
71
|
+
else
|
72
|
+
o << "#{@rspreadsheet.cell(y,x)}"
|
73
|
+
end
|
74
|
+
o << "</td>"
|
75
|
+
end
|
76
|
+
o << "</tr>"
|
77
|
+
end
|
78
|
+
o << "</table>"
|
79
|
+
} # each sheet
|
80
|
+
return o
|
81
|
+
end
|
82
|
+
|
data/lib/roo/version.rb
ADDED
data/lib/roo.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Roo
|
2
|
+
class Spreadsheet
|
3
|
+
class << self
|
4
|
+
def open(file)
|
5
|
+
case File.extname(file)
|
6
|
+
when '.xls'
|
7
|
+
Excel.new(file)
|
8
|
+
when '.xlsx'
|
9
|
+
Excelx.new(file)
|
10
|
+
when '.ods'
|
11
|
+
Openoffice.new(file)
|
12
|
+
when '.xml'
|
13
|
+
Excel2003XML.new(file)
|
14
|
+
when ''
|
15
|
+
Google.new(file)
|
16
|
+
else
|
17
|
+
raise ArgumentError, "Don't know how to open file #{file}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'roo/version'
|
25
|
+
# require 'roo/spreadsheetparser' TODO:
|
26
|
+
require 'roo/generic_spreadsheet'
|
27
|
+
require 'roo/openoffice'
|
28
|
+
require 'roo/excel'
|
29
|
+
require 'roo/excelx'
|
30
|
+
require 'roo/google'
|
31
|
+
require 'roo/excel2003xml'
|
32
|
+
require 'roo/roo_rails_helper'
|
data/test/1900_base.xls
ADDED
Binary file
|
data/test/1904_base.xls
ADDED
Binary file
|