Class: Roo::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/roo/base.rb

Overview

Base class for all other types of spreadsheets

Direct Known Subclasses

CSV, Excelx, OpenOffice

Constant Summary collapse

TEMP_PREFIX =
'roo_'
MAX_ROW_COL =
999_999.freeze
MIN_ROW_COL =
0.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = {}, _file_warning = :error, _tmpdir = nil) ⇒ Base

Returns a new instance of Base.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/roo/base.rb', line 21

def initialize(filename, options = {}, _file_warning = :error, _tmpdir = nil)
  @filename = filename
  @options = options

  @cell = {}
  @cell_type = {}
  @cells_read = {}

  @first_row = {}
  @last_row = {}
  @first_column = {}
  @last_column = {}

  @header_line = 1
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

when a method like spreadsheet.a42 is called convert it to a call of spreadsheet.cell(‘a’,42)



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/roo/base.rb', line 294

def method_missing(m, *args)
  # #aa42 => #cell('aa',42)
  # #aa42('Sheet1')  => #cell('aa',42,'Sheet1')
  if m =~ /^([a-z]+)(\d)$/
    col = ::Roo::Utils.letter_to_number(Regexp.last_match[1])
    row = Regexp.last_match[2].to_i
    if args.empty?
      cell(row, col)
    else
      cell(row, col, args.first)
    end
  else
    super
  end
end

Instance Attribute Details

#header_lineObject

sets the line with attribute names (default: 1)



19
20
21
# File 'lib/roo/base.rb', line 19

def header_line
  @header_line
end

#headersObject (readonly)

Returns the value of attribute headers.



16
17
18
# File 'lib/roo/base.rb', line 16

def headers
  @headers
end

Instance Method Details

#cell_type_by_value(value) ⇒ Object



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

def cell_type_by_value(value)
  case value
  when Fixnum then :float
  when String, Float then :string
  else
    raise ArgumentError, "Type for #{value} not set"
  end
end

#clean_sheet_if_need(options) ⇒ Object



325
326
327
328
329
330
# File 'lib/roo/base.rb', line 325

def clean_sheet_if_need(options)
  return unless options[:clean]
  options.delete(:clean)
  @cleaned ||= {}
  clean_sheet(default_sheet) unless @cleaned[default_sheet]
end

#collect_last_row_col_for_sheet(sheet) ⇒ Object

Collect first/last row/column from sheet



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/roo/base.rb', line 75

def collect_last_row_col_for_sheet(sheet)
  first_row = first_column = MAX_ROW_COL
  last_row = last_column = MIN_ROW_COL
  @cell[sheet].each_pair do|key, value|
    next unless value
    first_row = [first_row, key.first.to_i].min
    last_row = [last_row, key.first.to_i].max
    first_column = [first_column, key.last.to_i].min
    last_column = [last_column, key.last.to_i].max
  end if @cell[sheet]
  {first_row: first_row, first_column: first_column, last_row: last_row, last_column: last_column}
end

#column(column_number, sheet = default_sheet) ⇒ Object

returns all values in this column as an array column numbers are 1,2,3,… like in the spreadsheet



195
196
197
198
199
200
201
202
203
# File 'lib/roo/base.rb', line 195

def column(column_number, sheet = default_sheet)
  if column_number.is_a?(::String)
    column_number = ::Roo::Utils.letter_to_number(column_number)
  end
  read_cells(sheet)
  first_row(sheet).upto(last_row(sheet)).map do |row|
    cell(row, column_number, sheet)
  end
end

#default_sheetObject



37
38
39
# File 'lib/roo/base.rb', line 37

def default_sheet
  @default_sheet ||= sheets.first
end

#default_sheet=(sheet) ⇒ Object

sets the working sheet in the document ‘sheet’ can be a number (1 = first sheet) or the name of a sheet.



43
44
45
46
47
48
# File 'lib/roo/base.rb', line 43

def default_sheet=(sheet)
  validate_sheet!(sheet)
  @default_sheet = sheet
  @first_row[sheet] = @last_row[sheet] = @first_column[sheet] = @last_column[sheet] = nil
  @cells_read[sheet] = false
end

#each(options = {}) ⇒ Object

you can also pass in a :clean => true option to strip the sheet of odd unicode characters and white spaces around columns



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/roo/base.rb', line 366

def each(options = {})
  if options.empty?
    1.upto(last_row) do |line|
      yield row(line)
    end
  else
    clean_sheet_if_need(options)
    search_or_set_header(options)
    headers = @headers ||
              Hash[(first_column..last_column).map do |col|
                [cell(@header_line, col), col]
              end]

    @header_line.upto(last_row) do |line|
      yield(Hash[headers.map { |k, v| [k, cell(line, v)] }])
    end
  end
end

#each_with_pagenameObject

iterate through all worksheets of a document



318
319
320
321
322
# File 'lib/roo/base.rb', line 318

def each_with_pagename
  sheets.each do |s|
    yield sheet(s, true)
  end
end

#empty?(row, col, sheet = default_sheet) ⇒ Boolean

true if cell is empty

Returns:

  • (Boolean)


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

def empty?(row, col, sheet = default_sheet)
  read_cells(sheet)
  row, col = normalize(row, col)
  contents = cell(row, col, sheet)
  !contents || (celltype(row, col, sheet) == :string && contents.empty?) \
    || (row < first_row(sheet) || row > last_row(sheet) || col < first_column(sheet) || col > last_column(sheet))
end

#find(*args) ⇒ Object

find a row either by row number or a condition Caution: this works only within the default sheet -> set default_sheet before you call this method (experimental. see examples in the test_roo.rb file)



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/roo/base.rb', line 171

def find(*args) # :nodoc
  options = (args.last.is_a?(Hash) ? args.pop : {})

  case args[0]
  when Fixnum
    find_by_row(args[0])
  when :all
    find_by_conditions(options)
  else
    fail ArgumentError, "unexpected arg #{args[0].inspect}, pass a row index or :all"
  end
end

#first_column_as_letter(sheet = default_sheet) ⇒ Object

first non-empty column as a letter



51
52
53
# File 'lib/roo/base.rb', line 51

def first_column_as_letter(sheet = default_sheet)
  ::Roo::Utils.number_to_letter(first_column(sheet))
end

#first_last_row_col_for_sheet(sheet) ⇒ Object

Set first/last row/column for sheet



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/roo/base.rb', line 61

def first_last_row_col_for_sheet(sheet)
  @first_last_row_cols ||= {}
  @first_last_row_cols[sheet] ||= begin
    result = collect_last_row_col_for_sheet(sheet)
    {
      first_row: result[:first_row] == MAX_ROW_COL ? nil : result[:first_row],
      first_column: result[:first_column] == MAX_ROW_COL ? nil : result[:first_column],
      last_row: result[:last_row] == MIN_ROW_COL ? nil : result[:last_row],
      last_column: result[:last_column] == MIN_ROW_COL ? nil : result[:last_column]
    }
  end
end

#infoObject

returns information of the spreadsheet document and all sheets within this document.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/roo/base.rb', line 242

def info
  without_changing_default_sheet do
    result = "File: #{File.basename(@filename)}\n"\
      "Number of sheets: #{sheets.size}\n"\
      "Sheets: #{sheets.join(', ')}\n"
    n = 1
    sheets.each do|sheet|
      self.default_sheet = sheet
      result << 'Sheet ' + n.to_s + ":\n"
      unless first_row
        result << '  - empty -'
      else
        result << "  First row: #{first_row}\n"
        result << "  Last row: #{last_row}\n"
        result << "  First column: #{::Roo::Utils.number_to_letter(first_column)}\n"
        result << "  Last column: #{::Roo::Utils.number_to_letter(last_column)}"
      end
      result << "\n" if sheet != sheets.last
      n += 1
    end
    result
  end
end

#inspectObject

call to_s method defined on subclasses



164
165
166
# File 'lib/roo/base.rb', line 164

def inspect
  to_s
end

#last_column_as_letter(sheet = default_sheet) ⇒ Object

last non-empty column as a letter



56
57
58
# File 'lib/roo/base.rb', line 56

def last_column_as_letter(sheet = default_sheet)
  ::Roo::Utils.number_to_letter(last_column(sheet))
end

#parse(options = {}) ⇒ Object



385
386
387
388
389
390
391
392
# File 'lib/roo/base.rb', line 385

def parse(options = {})
  ary = []
  each(options) do |row|
    yield(row) if block_given?
    ary << row
  end
  ary
end

#reloadObject

reopens and read a spreadsheet document



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

def reload
  ds = default_sheet
  reinitialize
  self.default_sheet = ds
end

#row(row_number, sheet = default_sheet) ⇒ Object

returns all values in this row as an array row numbers are 1,2,3,… like in the spreadsheet



186
187
188
189
190
191
# File 'lib/roo/base.rb', line 186

def row(row_number, sheet = default_sheet)
  read_cells(sheet)
  first_column(sheet).upto(last_column(sheet)).map do |col|
    cell(row_number, col, sheet)
  end
end

#row_with(query, return_headers = false) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/roo/base.rb', line 394

def row_with(query, return_headers = false)
  line_no = 0
  each do |row|
    line_no += 1
    headers = query.map { |q| row.grep(q)[0] }.compact

    if headers.length == query.length
      @header_line = line_no
      return return_headers ? headers : line_no
    elsif line_no > 100
      fail "Couldn't find header row."
    end
  end
  fail "Couldn't find header row."
end

#search_or_set_header(options) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
# File 'lib/roo/base.rb', line 332

def search_or_set_header(options)
  if options[:header_search]
    @headers = nil
    @header_line = row_with(options[:header_search])
  elsif [:first_row, true].include?(options[:headers])
    @headers = []
    row(first_row).each_with_index { |x, i| @headers << [x, i + 1] }
  else
    set_headers(options)
  end
end

#set(row, col, value, sheet = default_sheet) ⇒ Object

set a cell to a certain value (this will not be saved back to the spreadsheet file!)



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

def set(row, col, value, sheet = default_sheet) #:nodoc:
  read_cells(sheet)
  row, col = normalize(row, col)
  cell_type = cell_type_by_value(value)
  set_value(row, col, value, sheet)
  set_type(row, col, cell_type , sheet)
end

#sheet(index, name = false) ⇒ Object

access different worksheets by calling spreadsheet.sheet(1) or spreadsheet.sheet(‘SHEETNAME’)



312
313
314
315
# File 'lib/roo/base.rb', line 312

def sheet(index, name = false)
  self.default_sheet = String === index ? index : sheets[index]
  name ? [default_sheet, self] : self
end

#to_csv(filename = nil, separator = ',', sheet = default_sheet) ⇒ Object

write the current spreadsheet to stdout or into a file



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/roo/base.rb', line 131

def to_csv(filename = nil, separator = ',', sheet = default_sheet)
  if filename
    File.open(filename, 'w') do |file|
      write_csv_content(file, sheet, separator)
    end
    true
  else
    sio = ::StringIO.new
    write_csv_content(sio, sheet, separator)
    sio.rewind
    sio.read
  end
end

#to_matrix(from_row = nil, from_column = nil, to_row = nil, to_column = nil, sheet = default_sheet) ⇒ Object

returns a matrix object from the whole sheet or a rectangular area of a sheet



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/roo/base.rb', line 146

def to_matrix(from_row = nil, from_column = nil, to_row = nil, to_column = nil, sheet = default_sheet)
  require 'matrix'

  return Matrix.empty unless first_row

  from_row ||= first_row(sheet)
  to_row ||= last_row(sheet)
  from_column ||= first_column(sheet)
  to_column ||= last_column(sheet)

  Matrix.rows(from_row.upto(to_row).map do |row|
    from_column.upto(to_column).map do |col|
      cell(row, col, sheet)
    end
  end)
end

#to_xmlObject

returns an XML representation of all sheets of a spreadsheet file



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/roo/base.rb', line 267

def to_xml
  Nokogiri::XML::Builder.new do |xml|
    xml.spreadsheet do
      sheets.each do |sheet|
        self.default_sheet = sheet
        xml.sheet(name: sheet) do |x|
          if first_row && last_row && first_column && last_column
            # sonst gibt es Fehler bei leeren Blaettern
            first_row.upto(last_row) do |row|
              first_column.upto(last_column) do |col|
                unless empty?(row, col)
                  x.cell(cell(row, col),
                         row: row,
                         column: col,
                         type: celltype(row, col))
                end
              end
            end
          end
        end
      end
    end
  end.to_xml
end

#to_yaml(prefix = {}, from_row = nil, from_column = nil, to_row = nil, to_column = nil, sheet = default_sheet) ⇒ Object

returns a rectangular area (default: all cells) as yaml-output you can add additional attributes with the prefix parameter like: oo.to_yaml(“sheet” => “1”)



100
101
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
# File 'lib/roo/base.rb', line 100

def to_yaml(prefix = {}, from_row = nil, from_column = nil, to_row = nil, to_column = nil, sheet = default_sheet)
  return '' unless first_row # empty result if there is no first_row in a sheet

  from_row ||= first_row(sheet)
  to_row ||= last_row(sheet)
  from_column ||= first_column(sheet)
  to_column ||= last_column(sheet)

  result = "--- \n"
  from_row.upto(to_row) do |row|
    from_column.upto(to_column) do |col|
      unless empty?(row, col, sheet)
        result << "cell_#{row}_#{col}: \n"
        prefix.each do|k, v|
          result << "  #{k}: #{v} \n"
        end
        result << "  row: #{row} \n"
        result << "  col: #{col} \n"
        result << "  celltype: #{celltype(row, col, sheet)} \n"
        value = cell(row, col, sheet)
        if celltype(row, col, sheet) == :time
          value = integer_to_timestring(value)
        end
        result << "  value: #{value} \n"
      end
    end
  end
  result
end