Skip to content

Commit 71a56aa

Browse files
author
--unset
committed
update roo to use ruby-spreadsheet from parseexcel
1 parent 2a3d371 commit 71a56aa

File tree

6 files changed

+191
-113
lines changed

6 files changed

+191
-113
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg/

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ begin
1717
s.has_rdoc = true
1818
s.extra_rdoc_files = ["README.txt", "History.txt"]
1919
s.rdoc_options = ["--main","README.txt"]
20-
s.add_dependency "parseexcel", [">= 0.5.3"]
20+
s.add_dependency "spreadsheet", [">= 0.6.3.1"]
2121
s.add_dependency "rubyzip", [">= 0.9.1"]
2222
s.add_dependency "hpricot", [">= 0.5"]
2323
s.add_dependency "GData", [">= 0.0.3"]

VERSION.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
:patch: 4
3+
:major: 1
4+
:minor: 2

lib/roo/excel.rb

+103-75
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'rubygems'
2-
gem 'parseexcel', '>= 0.5.3'
3-
require 'parseexcel'
2+
gem 'spreadsheet', '>= 0.6.3.1'
3+
require 'spreadsheet'
44
CHARGUESS = false
55
require 'charguess' if CHARGUESS
66

@@ -37,7 +37,7 @@ def initialize(filename, packed = nil, file_warning = :error)
3737
unless File.file?(@filename)
3838
raise IOError, "file #{@filename} does not exist"
3939
end
40-
@workbook = Spreadsheet::ParseExcel.parse(filename)
40+
@workbook = Spreadsheet.open(filename)
4141
@default_sheet = nil
4242
# no need to set default_sheet if there is only one sheet in the document
4343
if self.sheets.size == 1
@@ -63,19 +63,18 @@ def initialize(filename, packed = nil, file_warning = :error)
6363
# returns an array of sheet names in the spreadsheet
6464
def sheets
6565
result = []
66-
#0.upto(@workbook.worksheets.size - 1) do |i| # spreadsheet
67-
0.upto(@workbook.sheet_count - 1) do |i| # parseexcel
66+
@workbook.worksheets.each do |worksheet|
6867
# TODO: is there a better way to do conversion?
6968
if CHARGUESS
70-
encoding = CharGuess::guess(@workbook.worksheet(i).name)
69+
encoding = CharGuess::guess(worksheet.name)
7170
encoding = 'unicode' unless encoding
7271

7372

7473
result << Iconv.new('utf-8',encoding).iconv(
75-
@workbook.worksheet(i).name
74+
worksheet.name
7675
)
7776
else
78-
result << platform_specific_iconv(@workbook.worksheet(i).name)
77+
result << platform_specific_iconv(worksheet.name)
7978
end
8079
end
8180
return result
@@ -181,17 +180,23 @@ def cell_font(row, col, sheet=nil)
181180

182181
# true if the cell style is bold
183182
def bold?(*args)
184-
cell_font(*args)[:bold]
183+
#From ruby-spreadsheet doc: 100 <= weight <= 1000, bold => 700, normal => 400
184+
case cell_font(*args).weight
185+
when 700
186+
true
187+
else
188+
false
189+
end
185190
end
186191

187192
# true if the cell style is italic
188193
def italic?(*args)
189-
cell_font(*args)[:italic]
194+
cell_font(*args).italic
190195
end
191196

192197
# true if the cell style is underline
193198
def underline?(*args)
194-
cell_font(*args)[:underline]
199+
cell_font(*args).underline != :none
195200
end
196201

197202
# shows the internal representation of all cells
@@ -243,14 +248,15 @@ def get_firsts_lasts(sheet=nil)
243248
# converts name of a sheet to index (0,1,2,..)
244249
def sheet_no(name)
245250
return name-1 if name.kind_of?(Fixnum)
246-
0.upto(@workbook.sheet_count - 1) do |i|
247-
#0.upto(@workbook.worksheets.size - 1) do |i|
251+
i = 0
252+
@workbook.worksheets.each do |worksheet|
248253
# TODO: is there a better way to do conversion?
249254
return i if name == platform_specific_iconv(
250-
@workbook.worksheet(i).name)
255+
worksheet.name)
251256
#Iconv.new('utf-8','unicode').iconv(
252257
# @workbook.worksheet(i).name
253258
# )
259+
i += 1
254260
end
255261
raise StandardError, "sheet '#{name}' not found"
256262
end
@@ -316,9 +322,9 @@ def remove_every_second_null(str)
316322
end
317323

318324
# helper function to set the internal representation of cells
319-
def set_cell_values(sheet,x,y,i,v,vt,formula,tr,str_v, font)
325+
def set_cell_values(sheet,row,col,i,v,vt,formula,tr,font)
320326
#key = "#{y},#{x+i}"
321-
key = [y,x+i]
327+
key = [row,col+i]
322328
@cell_type[sheet] = {} unless @cell_type[sheet]
323329
@cell_type[sheet][key] = vt
324330
@formula[sheet] = {} unless @formula[sheet]
@@ -331,7 +337,7 @@ def set_cell_values(sheet,x,y,i,v,vt,formula,tr,str_v, font)
331337
when :float
332338
@cell[sheet][key] = v.to_f
333339
when :string
334-
@cell[sheet][key] = str_v
340+
@cell[sheet][key] = v
335341
when :date
336342
@cell[sheet][key] = v
337343
when :datetime
@@ -356,67 +362,89 @@ def read_cells(sheet=nil)
356362
end
357363

358364
worksheet = @workbook.worksheet(sheet_no(sheet))
359-
skip = 0
360-
x =1
361-
y=1
362-
i=0
363-
worksheet.each(skip) { |row_par|
364-
if row_par
365-
x =1
366-
row_par.each do # |void|
367-
cell = row_par.at(x-1)
368-
if cell
369-
case cell.type
370-
when :numeric
371-
vt = :float
372-
v = cell.to_f
373-
when :text
374-
vt = :string
375-
if cell.to_s.downcase == 'true'
376-
str_v = cell.to_s
377-
else
378-
str_v = cell.to_s('utf-8')
379-
end
380-
when :date
381-
if cell.to_s.to_f < 1.0
382-
vt = :time
383-
f = cell.to_s.to_f*24.0*60.0*60.0
384-
secs = f.round
385-
h = (secs / 3600.0).floor
386-
secs = secs - 3600*h
387-
m = (secs / 60.0).floor
388-
secs = secs - 60*m
389-
s = secs
390-
v = h*3600+m*60+s
391-
else
392-
if cell.datetime.hour != 0 or
393-
cell.datetime.min != 0 or
394-
cell.datetime.sec != 0 or
395-
cell.datetime.msec != 0
396-
vt = :datetime
397-
v = cell.datetime
398-
else
399-
vt = :date
400-
v = cell.date
401-
v = sprintf("%04d-%02d-%02d",v.year,v.month,v.day)
402-
end
403-
end
404-
else
405-
vt = cell.type.to_s.downcase.to_sym
406-
v = nil
407-
end # case
408-
formula = tr = nil #TODO:???
409-
set_cell_values(sheet,x,y,i,v,vt,formula,tr,str_v, cell.font)
410-
end # if cell
411-
412-
x += 1
365+
row_index=1
366+
worksheet.each(0) do |row|
367+
(0..row.size).each do |cell_index|
368+
cell = row.at(cell_index)
369+
next if cell.nil? #skip empty cells
370+
next if cell.class == Spreadsheet::Formula
371+
if date_or_time?(row, cell_index)
372+
vt, v = read_cell_date_or_time(row, cell_index)
373+
else
374+
vt, v = read_cell(row, cell_index)
413375
end
414-
end
415-
y += 1
416-
}
376+
formula = tr = nil #TODO:???
377+
col_index = cell_index + 1
378+
font = row.format(cell_index).font
379+
#puts [row_index, col_index].inspect
380+
#puts vt, v
381+
set_cell_values(sheet,row_index,col_index,0,v,vt,formula,tr,font)
382+
end #row
383+
row_index += 1
384+
end # worksheets
417385
@cells_read[sheet] = true
418386
end
419-
387+
388+
# Test the cell to see if it's a valid date/time.
389+
def date_or_time?(row, idx)
390+
format = row.format(idx)
391+
if format.date_or_time?
392+
cell = row.at(idx)
393+
cell.to_s.to_f > 0 ? true : false # cell value must be numeric
394+
else
395+
false
396+
end
397+
end
398+
private :date_or_time?
399+
400+
def read_cell_date_or_time(row, idx)
401+
cell = row.at(idx).to_s.to_f
402+
if cell < 1.0
403+
value_type = :time
404+
f = cell*24.0*60.0*60.0
405+
secs = f.round
406+
h = (secs / 3600.0).floor
407+
secs = secs - 3600*h
408+
m = (secs / 60.0).floor
409+
secs = secs - 60*m
410+
s = secs
411+
value = h*3600+m*60+s
412+
else
413+
datetime = row.datetime(idx)
414+
if datetime.hour != 0 or
415+
datetime.min != 0 or
416+
datetime.sec != 0
417+
value_type = :datetime
418+
value = datetime
419+
else
420+
value_type = :date
421+
value = row.date(idx)
422+
value = sprintf("%04d-%02d-%02d",value.year,value.month,value.day)
423+
end
424+
end
425+
return value_type, value
426+
end
427+
private :read_cell_date_or_time
428+
429+
# Read the cell and based on the class,
430+
# return the value and value types for Roo
431+
def read_cell(row, idx)
432+
cell = row.at(idx)
433+
case cell
434+
when Float, Integer, Fixnum, Bignum
435+
value_type = :float
436+
value = cell.to_f
437+
when String, TrueClass, FalseClass
438+
value_type = :string
439+
value = cell.to_s
440+
else
441+
value_type = cell.class.to_s.downcase.to_sym
442+
value = nil
443+
end # case
444+
return value_type, value
445+
end
446+
private :read_cell
447+
420448
#TODO: testing only
421449
# def inject_null_characters(str)
422450
# if str.class != String

roo.gemspec

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- encoding: utf-8 -*-
2+
3+
Gem::Specification.new do |s|
4+
s.name = %q{roo}
5+
s.version = "1.2.4"
6+
s.platform = %q{universal-darwin-9}
7+
8+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9+
s.authors = ["Thomas Preymesser"]
10+
s.date = %q{2009-02-21}
11+
s.description = %q{roo can access the contents of OpenOffice-, Excel- or Google-Spreadsheets}
12+
s.email = %q{[email protected]}
13+
s.extra_rdoc_files = ["README.txt", "History.txt"]
14+
s.files = ["lib/roo", "lib/roo/excel.rb", "lib/roo/excelx.rb", "lib/roo/generic_spreadsheet.rb", "lib/roo/google.rb", "lib/roo/openoffice.rb", "lib/roo/roo_rails_helper.rb", "lib/roo/version.rb", "lib/roo.rb", "test/bbu.ods", "test/bbu.xls", "test/bbu.xlsx", "test/Bibelbund.csv", "test/Bibelbund.ods", "test/Bibelbund.xls", "test/Bibelbund.xlsx", "test/Bibelbund1.ods", "test/bode-v1.ods.zip", "test/bode-v1.xls.zip", "test/boolean.ods", "test/boolean.xls", "test/boolean.xlsx", "test/borders.ods", "test/borders.xls", "test/borders.xlsx", "test/bug-row-column-fixnum-float.xls", "test/datetime.ods", "test/datetime.xls", "test/datetime.xlsx", "test/emptysheets.ods", "test/emptysheets.xls", "test/false_encoding.xls", "test/formula.ods", "test/formula.xls", "test/formula.xlsx", "test/html-escape.ods", "test/no_spreadsheet_file.txt", "test/numbers1.csv", "test/numbers1.ods", "test/numbers1.xls", "test/numbers1.xlsx", "test/numbers1_excel.csv", "test/only_one_sheet.ods", "test/only_one_sheet.xls", "test/only_one_sheet.xlsx", "test/ric.ods", "test/simple_spreadsheet.ods", "test/simple_spreadsheet.xls", "test/simple_spreadsheet.xlsx", "test/simple_spreadsheet_from_italo.ods", "test/simple_spreadsheet_from_italo.xls", "test/style.ods", "test/style.xls", "test/style.xlsx", "test/test_helper.rb", "test/test_roo.rb", "test/time-test.csv", "test/time-test.ods", "test/time-test.xls", "test/time-test.xlsx", "README.txt", "History.txt"]
15+
s.has_rdoc = true
16+
s.homepage = %q{http://raa.ruby-lang.org/project/parseexcel/}
17+
s.rdoc_options = ["--main", "README.txt", "--inline-source", "--charset=UTF-8"]
18+
s.require_paths = ["lib"]
19+
s.rubyforge_project = %q{roo}
20+
s.rubygems_version = %q{1.3.1}
21+
s.summary = %q{roo}
22+
23+
if s.respond_to? :specification_version then
24+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25+
s.specification_version = 2
26+
27+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28+
s.add_runtime_dependency(%q<spreadsheet>, [">= 0.6.3.1"])
29+
s.add_runtime_dependency(%q<rubyzip>, [">= 0.9.1"])
30+
s.add_runtime_dependency(%q<hpricot>, [">= 0.5"])
31+
s.add_runtime_dependency(%q<GData>, [">= 0.0.3"])
32+
else
33+
s.add_dependency(%q<spreadsheet>, [">= 0.6.3.1"])
34+
s.add_dependency(%q<rubyzip>, [">= 0.9.1"])
35+
s.add_dependency(%q<hpricot>, [">= 0.5"])
36+
s.add_dependency(%q<GData>, [">= 0.0.3"])
37+
end
38+
else
39+
s.add_dependency(%q<spreadsheet>, [">= 0.6.3.1"])
40+
s.add_dependency(%q<rubyzip>, [">= 0.9.1"])
41+
s.add_dependency(%q<hpricot>, [">= 0.5"])
42+
s.add_dependency(%q<GData>, [">= 0.0.3"])
43+
end
44+
end

0 commit comments

Comments
 (0)