Class: Excel
- Inherits:
-
GenericSpreadsheet
- Object
- GenericSpreadsheet
- Excel
- Defined in:
- lib/roo/excel.rb
Overview
Class for handling Excel-Spreadsheets
Constant Summary collapse
- EXCEL_NO_FORMULAS =
'formulas are not supported for excel spreadsheets'
Instance Attribute Summary
Attributes inherited from GenericSpreadsheet
Instance Method Summary collapse
-
#cell(row, col, sheet = nil) ⇒ Object
returns the content of a cell.
-
#celltype(row, col, sheet = nil) ⇒ Object
returns the type of a cell: :float, :string, :date, :time.
-
#column(columnnumber, sheet = nil) ⇒ Object
returns all values in this column as an array column numbers are 1,2,3,…
-
#first_column(sheet = nil) ⇒ Object
returns the first non empty column.
-
#first_row(sheet = nil) ⇒ Object
returns the first non empty row.
-
#formula(row, col, sheet = nil) ⇒ Object
returns NO formula in excel spreadsheets.
-
#formula?(row, col, sheet = nil) ⇒ Boolean
raises an exception because formulas are not supported for excel files.
-
#formulas(sheet = nil) ⇒ Object
returns NO formulas in excel spreadsheets.
-
#initialize(filename, packed = nil) ⇒ Excel
constructor
Creates a new Excel spreadsheet object.
-
#inject_null_characters(str) ⇒ Object
TODO: testing only.
-
#last_column(sheet = nil) ⇒ Object
returns the last non empty column.
-
#last_row(sheet = nil) ⇒ Object
returns the last non empty row.
-
#row(rownumber, sheet = nil) ⇒ Object
returns all values in this row as an array row numbers are 1,2,3,…
-
#sheets ⇒ Object
returns an array of sheet names in the spreadsheet.
Methods inherited from GenericSpreadsheet
#empty?, #find, #first_column_as_letter, #info, #last_column_as_letter, letter_to_number, #normalize, number_to_letter, #open_from_uri, #reload, #remove_tmp, #to_csv, #to_xml, #to_yaml
Constructor Details
#initialize(filename, packed = nil) ⇒ Excel
Creates a new Excel spreadsheet object. Parameter packed: :zip - File is a zip-file
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 48 49 50 51 52 53 54 55 |
# File 'lib/roo/excel.rb', line 23 def initialize(filename, packed = nil) @tmpdir = "oo_"+$$.to_s unless File.exists?(@tmpdir) FileUtils::mkdir(@tmpdir) end filename = open_from_uri(filename) if filename[0,7] == "http://" filename = unzip(filename) if packed and packed == :zip if File.extname(filename) != ".xls" warn "are you sure, this is an excel file?" end @filename = filename begin unless File.file?(@filename) raise IOError, "file #{@filename} does not exist" end @workbook = Spreadsheet::ParseExcel.parse(filename) @default_sheet = nil # no need to set default_sheet if there is only one sheet in the document if self.sheets.size == 1 @default_sheet = self.sheets.first end # @first_row = @last_row = @first_column = @last_column = nil #if ENV["roo_local"] != "thomas-p" ensure FileUtils::rm_r(@tmpdir) #end end @first_row = Hash.new @last_row = Hash.new @first_column = Hash.new @last_column = Hash.new @cells_read = Hash.new end |
Instance Method Details
#cell(row, col, sheet = nil) ⇒ Object
returns the content of a cell. The upper left corner is (1,1) or (‘A’,1)
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 |
# File 'lib/roo/excel.rb', line 102 def cell(row,col,sheet=nil) sheet = @default_sheet unless sheet row,col = normalize(row,col) worksheet = @workbook.worksheet(sheet_no(sheet)) skip = 0 line = 1 worksheet.each(skip) { |row_par| #TODO: nicht jedesmal durch alle Zeilen gehen, sonder aus interner Repraesentation holen? if line == row if row_par == nil return nil end cell = row_par.at(col-1) return nil unless cell case cell.type when :numeric then return cell.to_f when :text then return cell.to_s('utf-8') # when :date then return cell.date when :date if cell.to_s.to_f < 1.0 f = cell.to_s.to_f*24.0*60.0*60.0 secs = f.round h = (secs / 3600.0).floor secs = secs - 3600*h m = (secs / 60.0).floor secs = secs - 60*m s = secs return h*3600+m*60+s else return cell.date end else return nil # cell.to_s('utf-8') end end line += 1 } end |
#celltype(row, col, sheet = nil) ⇒ Object
returns the type of a cell: :float, :string, :date, :time
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 |
# File 'lib/roo/excel.rb', line 141 def celltype(row,col,sheet=nil) sheet = @default_sheet unless sheet row,col = normalize(row,col) worksheet = @workbook.worksheet(sheet_no(sheet)) skip = 0 line = 1 worksheet.each(skip) { |row_par| if line == row return nil unless row_par cell = row_par.at(col-1) return nil unless cell case cell.type when :numeric return :float when :text return :string when :date if cell.to_s.to_f < 1.0 return :time else return :date end else return cell.type.to_sym end end line += 1 } end |
#column(columnnumber, sheet = nil) ⇒ Object
returns all values in this column as an array column numbers are 1,2,3,… like in the spreadsheet
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 |
# File 'lib/roo/excel.rb', line 197 def column(columnnumber,sheet=nil) if columnnumber.class == String columnnumber = Openoffice.letter_to_number(columnnumber) end sheet = @default_sheet unless sheet worksheet = @workbook.worksheet(sheet_no(sheet)) skip = 0 result = [] worksheet.each(skip) { |row_par| if defined? row_par.at(columnnumber-1) cell = row_par.at(columnnumber-1) #if defined? cell = row_par.at(columnnumber-1) if cell case cell.type when :numeric then result << cell.to_i when :text then result << cell.to_s('utf-8') when :date then result << cell.date else result << cell.to_s('utf-8') end else result << nil end else result << nil end } result end |
#first_column(sheet = nil) ⇒ Object
returns the first non empty column
228 229 230 231 232 233 |
# File 'lib/roo/excel.rb', line 228 def first_column(sheet=nil) sheet = @default_sheet unless sheet return @first_column[sheet] if @first_column[sheet] fr, lr, fc, lc = get_firsts_lasts(sheet) fc end |
#first_row(sheet = nil) ⇒ Object
returns the first non empty row
244 245 246 247 248 249 |
# File 'lib/roo/excel.rb', line 244 def first_row(sheet=nil) sheet = @default_sheet unless sheet return @first_row[sheet] if @first_row[sheet] fr, lr, fc, lc = get_firsts_lasts(sheet) fr end |
#formula(row, col, sheet = nil) ⇒ Object
returns NO formula in excel spreadsheets
260 261 262 |
# File 'lib/roo/excel.rb', line 260 def formula(row,col,sheet=nil) raise EXCEL_NO_FORMULAS end |
#formula?(row, col, sheet = nil) ⇒ Boolean
raises an exception because formulas are not supported for excel files
265 266 267 |
# File 'lib/roo/excel.rb', line 265 def formula?(row,col,sheet=nil) raise EXCEL_NO_FORMULAS end |
#formulas(sheet = nil) ⇒ Object
returns NO formulas in excel spreadsheets
270 271 272 |
# File 'lib/roo/excel.rb', line 270 def formulas(sheet=nil) raise EXCEL_NO_FORMULAS end |
#inject_null_characters(str) ⇒ Object
TODO: testing only
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/roo/excel.rb', line 89 def inject_null_characters(str) if str.class != String return str end new_str='' 0.upto(str.size-1) do |i| new_str += str[i,1] new_str += "\000" end new_str end |
#last_column(sheet = nil) ⇒ Object
returns the last non empty column
236 237 238 239 240 241 |
# File 'lib/roo/excel.rb', line 236 def last_column(sheet=nil) sheet = @default_sheet unless sheet return @last_column[sheet] if @last_column[sheet] fr, lr, fc, lc = get_firsts_lasts(sheet) lc end |
#last_row(sheet = nil) ⇒ Object
returns the last non empty row
252 253 254 255 256 257 |
# File 'lib/roo/excel.rb', line 252 def last_row(sheet=nil) sheet = @default_sheet unless sheet return @last_row[sheet] if @last_row[sheet] fr, lr, fc, lc = get_firsts_lasts(sheet) lr end |
#row(rownumber, sheet = nil) ⇒ Object
returns all values in this row as an array row numbers are 1,2,3,… like in the spreadsheet
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/roo/excel.rb', line 174 def row(rownumber,sheet=nil) sheet = @default_sheet unless sheet worksheet = @workbook.worksheet(sheet_no(sheet)) #therow = worksheet.row(rownumber-1) result = [] worksheet.row(rownumber-1).each {|cell| if cell case cell.type when :numeric then result << cell.to_i when :text then result << cell.to_s('utf-8') when :date then result << cell.date else result << cell.to_s('utf-8') end else result << nil end } return result end |
#sheets ⇒ Object
returns an array of sheet names in the spreadsheet
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 85 86 |
# File 'lib/roo/excel.rb', line 58 def sheets result = [] 0.upto(@workbook.sheet_count - 1) do |i| # TODO: is there a better way to do conversion? if CHARGUESS encoding = CharGuess::guess(@workbook.worksheet(i).name) encoding = 'unicode' unless encoding result << Iconv.new('utf-8',encoding).iconv( @workbook.worksheet(i).name ) else # result << platform_specific_iconv(inject_null_characters(@workbook.worksheet(i).name)) result << platform_specific_iconv(@workbook.worksheet(i).name) #case RUBY_PLATFORM.downcase #when /darwin/ # result << Iconv.new('utf-8','utf-8').iconv( # @workbook.worksheet(i).name # ) #else # result << Iconv.new('utf-8','unicode').iconv( # @workbook.worksheet(i).name # ) #end # case end end return result end |