Class: Goodsheet::Spreadsheet

Inherits:
Roo::Spreadsheet
  • Object
show all
Defined in:
lib/goodsheet/spreadsheet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = {}) ⇒ Spreadsheet

Initialize a Goodsheet object

Valid options:

:skip       : number of rows to skip (default: 1)
:header_row : header's row index (0 based, default: 0)
:time_zone  : time zone string

Parameters:

  • filename (String)

    the spreadsheet filename you want to read

  • filename (String)

    the spreadsheet filename you want to read



16
17
18
19
20
# File 'lib/goodsheet/spreadsheet.rb', line 16

def initialize(filename, options={})
  set_options(options)
  @filename = filename
  @ss = Roo::Spreadsheet.open(filename, options)
end

Instance Attribute Details

#header_rowObject (readonly)

Returns the value of attribute header_row.



6
7
8
# File 'lib/goodsheet/spreadsheet.rb', line 6

def header_row
  @header_row
end

#max_errorsObject (readonly)

Returns the value of attribute max_errors.



6
7
8
# File 'lib/goodsheet/spreadsheet.rb', line 6

def max_errors
  @max_errors
end

#row_limitObject (readonly)

Returns the value of attribute row_limit.



6
7
8
# File 'lib/goodsheet/spreadsheet.rb', line 6

def row_limit
  @row_limit
end

#skipObject (readonly)

Returns the value of attribute skip.



6
7
8
# File 'lib/goodsheet/spreadsheet.rb', line 6

def skip
  @skip
end

#time_zoneObject (readonly)

Returns the value of attribute time_zone.



6
7
8
# File 'lib/goodsheet/spreadsheet.rb', line 6

def time_zone
  @time_zone
end

Instance Method Details

#get_headerObject



35
36
37
# File 'lib/goodsheet/spreadsheet.rb', line 35

def get_header
  @ss.row(@header_row+1) # because roo in 1-based
end

#nameObject

Get the currently selected sheet’s name



40
41
42
# File 'lib/goodsheet/spreadsheet.rb', line 40

def name
  @ss.default_sheet
end

#read(options = {}, &block) ⇒ Object

Columns must be an hash: labe for values and the column index like => 5



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/goodsheet/spreadsheet.rb', line 80

def read(options={}, &block)
  skip = options[:skip] || @skip
  header_row = options[:header_row] || @header_row
  max_errors = options[:max_errors] || @max_errors
  row_limit = options[:row_limit] || @row_limit
  force_nil = options[:force_nil]

  my_class = build_my_class(block)
  options[:my_custom_row_class] = my_class
  read_result = ReadResult.new(validate(options){ block })
  return read_result if read_result.invalid?
    
  line = skip # 0-based, from the top
  @ss.parse[skip..-1].each do |row| # row is an array of elements
    my_class.row_attributes.each do |attribute|
      read_result.add(attribute, my_class.new(row), force_nil)
    end
    break if row_limit && row_limit>0 && line>=(row_limit + skip - 1)
    line +=1
  end
  read_result
end

#rows_wo_headerObject Also known as: rows



48
49
50
# File 'lib/goodsheet/spreadsheet.rb', line 48

def rows_wo_header
  @ss.parse.size - @skip
end

#sheet(idx, options = {}) ⇒ Object

idx can be a number or a string



24
25
26
27
28
# File 'lib/goodsheet/spreadsheet.rb', line 24

def sheet(idx, options={})
  set_options(options)
  @ss.sheet(idx)
  check_sheet_exists
end

#sheetsObject



30
31
32
# File 'lib/goodsheet/spreadsheet.rb', line 30

def sheets
  @ss.sheets
end

#total_rowsObject



44
45
46
# File 'lib/goodsheet/spreadsheet.rb', line 44

def total_rows
  @ss.parse.size
end

#validate(options = {}, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/goodsheet/spreadsheet.rb', line 58

def validate(options={}, &block)
  skip = options[:skip] || @skip
  header_row = options[:header_row] || @header_row
  max_errors = options[:max_errors] || @max_errors
  row_limit = options[:row_limit] || @row_limit

  validation_errors = ValidationErrors.new

  my_class = options[:my_custom_row_class] || build_my_class(block)

  line = skip # 0-based, from the top
  @ss.parse[skip..-1].each do |row| # row is an array of elements
    validation_errors.add(line, my_class.new(row))
    break if max_errors>0 && validation_errors.size >= max_errors
    break if row_limit && row_limit>0 && line>=(row_limit+skip-1)
    line +=1
  end
  validation_errors
end