Class: Excel
- Inherits:
-
Openoffice
- Object
- Openoffice
- Excel
- Defined in:
- lib/roo/excel.rb
Overview
Class Excel is derived from class Openoffice. It implements almost all methods from the Openoffice class in the same way. Parameter packed: :zip - File is a zip-file
Constant Summary collapse
- EXCEL_NO_FORMULAS =
'formulas are not supported for excel spreadsheets'
Instance Attribute Summary
Attributes inherited from Openoffice
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.
-
#column(columnnumber, sheet = nil) ⇒ Object
returns all values in this column as an array column numbers are 1,2,3,…
-
#default_sheet=(n) ⇒ Object
sets the working sheet (1,2,3,..).
-
#empty?(row, col, sheet = nil) ⇒ Boolean
true if a cell is empty.
-
#first_column(sheet = nil) ⇒ Object
returns the first non empty column.
-
#first_column_as_letter(sheet = nil) ⇒ Object
first non-empty column as a letter.
-
#first_row(sheet = nil) ⇒ Object
returns the first non empty row.
- #formula(row, col, sheet = nil) ⇒ Object
- #formula?(row, col, sheet = nil) ⇒ Boolean
- #formulas(sheet = nil) ⇒ Object
-
#initialize(filename, packed = nil) ⇒ Excel
constructor
A new instance of Excel.
-
#last_column(sheet = nil) ⇒ Object
returns the last non empty column.
-
#last_column_as_letter(sheet = nil) ⇒ Object
last non-empty column as a letter.
-
#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
Methods inherited from Openoffice
#create_openoffice, #find, #info, #officeversion, #reload, #remove_tmp, #save, #set, #solve, #to_csv, #to_s, #to_yaml
Constructor Details
#initialize(filename, packed = nil) ⇒ Excel
Returns a new instance of Excel.
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 48 49 50 51 |
# File 'lib/roo/excel.rb', line 22 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 filename[-4..-1] != ".xls" warn "are you sure, this is an excel file?" end @filename = filename 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" FileUtils::rm_r(@tmpdir) #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)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/roo/excel.rb', line 80 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| 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 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
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 |
# File 'lib/roo/excel.rb', line 106 def celltype(row,col,sheet=nil) default_sheet_check if 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 return :date 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
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/roo/excel.rb', line 160 def column(columnnumber,sheet=nil) if columnnumber.class == String columnnumber = Openoffice.letter_to_number(columnnumber) end sheet = @default_sheet unless sheet default_sheet_check 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 |
#default_sheet=(n) ⇒ Object
sets the working sheet (1,2,3,..)
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/roo/excel.rb', line 65 def default_sheet=(n) if n.kind_of?(Fixnum) @default_sheet = n #-1 elsif n.kind_of?(String) raise RangeError if ! self.sheets.include?(n) # parseexcel supports now the name of a sheet @default_sheet = n else raise TypeError, "what are you trying to set as default sheet?" end @first_row[n] = @last_row[n] = @first_column[n] = @last_column[n] = nil @cells_read[n] = false end |
#empty?(row, col, sheet = nil) ⇒ Boolean
true if a cell is empty
224 225 226 227 228 229 230 231 |
# File 'lib/roo/excel.rb', line 224 def empty?(row, col, sheet=nil) sheet = @default_sheet unless sheet row,col = normalize(row,col) return true if row < first_row(sheet) || row > last_row(sheet) || col < first_column(sheet) || col > last_column(sheet) return true unless cell(row, col, sheet) return true if celltype(row, col, sheet) == "string" && cell(row, col, sheet) == "" false end |
#first_column(sheet = nil) ⇒ Object
returns the first non empty column
192 193 194 195 196 197 |
# File 'lib/roo/excel.rb', line 192 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_column_as_letter(sheet = nil) ⇒ Object
first non-empty column as a letter
234 235 236 237 |
# File 'lib/roo/excel.rb', line 234 def first_column_as_letter(sheet=nil) sheet = @default_sheet unless sheet Openoffice.number_to_letter(first_column(sheet)) end |
#first_row(sheet = nil) ⇒ Object
returns the first non empty row
208 209 210 211 212 213 |
# File 'lib/roo/excel.rb', line 208 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
245 246 247 |
# File 'lib/roo/excel.rb', line 245 def formula(row,col,sheet=nil) raise EXCEL_NO_FORMULAS end |
#formula?(row, col, sheet = nil) ⇒ Boolean
248 249 250 |
# File 'lib/roo/excel.rb', line 248 def formula?(row,col,sheet=nil) raise EXCEL_NO_FORMULAS end |
#formulas(sheet = nil) ⇒ Object
251 252 253 |
# File 'lib/roo/excel.rb', line 251 def formulas(sheet=nil) raise EXCEL_NO_FORMULAS end |
#last_column(sheet = nil) ⇒ Object
returns the last non empty column
200 201 202 203 204 205 |
# File 'lib/roo/excel.rb', line 200 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_column_as_letter(sheet = nil) ⇒ Object
last non-empty column as a letter
240 241 242 243 |
# File 'lib/roo/excel.rb', line 240 def last_column_as_letter(sheet=nil) sheet = @default_sheet unless sheet Openoffice.number_to_letter(last_column(sheet)) end |
#last_row(sheet = nil) ⇒ Object
returns the last non empty row
216 217 218 219 220 221 |
# File 'lib/roo/excel.rb', line 216 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
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/roo/excel.rb', line 136 def row(rownumber,sheet=nil) sheet = @default_sheet unless sheet default_sheet_check 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
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/roo/excel.rb', line 53 def sheets result = [] 0.upto(@workbook.sheet_count - 1) do |i| # TODO: is there a better way to do conversion? result << Iconv.new('utf-8','unicode').iconv( @workbook.worksheet(i).name ) end return result end |