Class: Excel

Inherits:
GenericSpreadsheet show all
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

#default_sheet, #header_line

Instance Method Summary collapse

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_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
# 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
  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)



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/roo/excel.rb', line 99

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/roo/excel.rb', line 125

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
        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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/roo/excel.rb', line 177

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



208
209
210
211
212
213
# File 'lib/roo/excel.rb', line 208

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



224
225
226
227
228
229
# File 'lib/roo/excel.rb', line 224

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

Raises:



240
241
242
# File 'lib/roo/excel.rb', line 240

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

Returns:

  • (Boolean)

Raises:



245
246
247
# File 'lib/roo/excel.rb', line 245

def formula?(row,col,sheet=nil)
  raise EXCEL_NO_FORMULAS
end

#formulas(sheet = nil) ⇒ Object

returns NO formulas in excel spreadsheets

Raises:



250
251
252
# File 'lib/roo/excel.rb', line 250

def formulas(sheet=nil)
  raise EXCEL_NO_FORMULAS
end

#inject_null_characters(str) ⇒ Object

TODO: testing only



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/roo/excel.rb', line 86

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



216
217
218
219
220
221
# File 'lib/roo/excel.rb', line 216

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



232
233
234
235
236
237
# File 'lib/roo/excel.rb', line 232

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/roo/excel.rb', line 154

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

#sheetsObject

returns an array of sheet names in the spreadsheet



55
56
57
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
# File 'lib/roo/excel.rb', line 55

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