Class: Goodsheet::Spreadsheet
- Inherits:
-
Roo::Spreadsheet
- Object
- Roo::Spreadsheet
- Goodsheet::Spreadsheet
- Defined in:
- lib/goodsheet/spreadsheet.rb
Instance Attribute Summary collapse
-
#header_row ⇒ Object
readonly
Returns the value of attribute header_row.
-
#max_errors ⇒ Object
readonly
Returns the value of attribute max_errors.
-
#row_limit ⇒ Object
readonly
Returns the value of attribute row_limit.
-
#skip ⇒ Object
readonly
Returns the value of attribute skip.
-
#time_zone ⇒ Object
readonly
Returns the value of attribute time_zone.
Instance Method Summary collapse
- #get_header ⇒ Object
-
#initialize(filename, options = {}) ⇒ Spreadsheet
constructor
Initialize a Goodsheet object.
-
#name ⇒ Object
Get the currently selected sheet’s name.
-
#read(options = {}, &block) ⇒ Object
Columns must be an hash: labe for values and the column index like => 5.
- #rows_wo_header ⇒ Object (also: #rows)
-
#sheet(idx, options = {}) ⇒ Object
idx can be a number or a string.
- #sheets ⇒ Object
- #total_rows ⇒ Object
- #validate(options = {}, &block) ⇒ Object
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
16 17 18 19 20 |
# File 'lib/goodsheet/spreadsheet.rb', line 16 def initialize(filename, ={}) () @filename = filename @ss = Roo::Spreadsheet.open(filename, ) end |
Instance Attribute Details
#header_row ⇒ Object (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_errors ⇒ Object (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_limit ⇒ Object (readonly)
Returns the value of attribute row_limit.
6 7 8 |
# File 'lib/goodsheet/spreadsheet.rb', line 6 def row_limit @row_limit end |
#skip ⇒ Object (readonly)
Returns the value of attribute skip.
6 7 8 |
# File 'lib/goodsheet/spreadsheet.rb', line 6 def skip @skip end |
#time_zone ⇒ Object (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_header ⇒ Object
35 36 37 |
# File 'lib/goodsheet/spreadsheet.rb', line 35 def get_header @ss.row(@header_row+1) # because roo in 1-based end |
#name ⇒ Object
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(={}, &block) skip = [:skip] || @skip header_row = [:header_row] || @header_row max_errors = [:max_errors] || @max_errors row_limit = [:row_limit] || @row_limit force_nil = [:force_nil] my_class = build_my_class(block) [:my_custom_row_class] = my_class read_result = ReadResult.new(validate(){ 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_header ⇒ Object 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, ={}) () @ss.sheet(idx) check_sheet_exists end |
#sheets ⇒ Object
30 31 32 |
# File 'lib/goodsheet/spreadsheet.rb', line 30 def sheets @ss.sheets end |
#total_rows ⇒ Object
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(={}, &block) skip = [:skip] || @skip header_row = [:header_row] || @header_row max_errors = [:max_errors] || @max_errors row_limit = [:row_limit] || @row_limit validation_errors = ValidationErrors.new my_class = [: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 |