Class: 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.
-
#s_opts ⇒ Object
readonly
Returns the value of attribute s_opts.
-
#skip ⇒ Object
readonly
Returns the value of attribute skip.
-
#ss ⇒ Object
readonly
Returns the value of attribute ss.
Instance Method Summary collapse
-
#get_header ⇒ Array<Object>
Get the header row of the currently selected sheet.
-
#index ⇒ Fixnum
Get the index of current (default) sheet.
-
#initialize(filename, options = {}) ⇒ Spreadsheet
constructor
Initialize a Goodsheet object.
-
#name ⇒ String
Get the name of current (default) sheet.
-
#options ⇒ Fixnum
Get the options of current sheet.
-
#read(options = {}) { ... } ⇒ ReadResult
Validate and, if successful, read the current sheet.
-
#rows_wo_skipped ⇒ Fixnum
(also: #rows)
Get the number of all rows minus the skipped ones (of the currently selected sheet).
-
#sheet(idx, options = {}) ⇒ Object
Select the desidered sheet.
-
#sheets ⇒ Array<String>
Get the sheet names list.
-
#size ⇒ Fixnum
Get the number of sheets.
-
#total_rows ⇒ Fixnum
Get the total number of rows (of the currently selected sheet).
-
#validate(options = {}) { ... } ⇒ ValidationErrors
Validate the current sheet.
Constructor Details
#initialize(filename, options = {}) ⇒ Spreadsheet
Initialize a Goodsheet object. The first sheet will be selected.
18 19 20 21 22 23 24 25 26 |
# File 'lib/goodsheet/spreadsheet.rb', line 18 def initialize(filename, ={}) # set_book_options(options) @filename = filename @ss = Roo::Spreadsheet.open(filename, ) @s_opts = Array.new(size, {}) size.times do |i| (i, ) end 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 |
#s_opts ⇒ Object (readonly)
Returns the value of attribute s_opts.
7 8 9 |
# File 'lib/goodsheet/spreadsheet.rb', line 7 def s_opts @s_opts end |
#skip ⇒ Object (readonly)
Returns the value of attribute skip.
6 7 8 |
# File 'lib/goodsheet/spreadsheet.rb', line 6 def skip @skip end |
#ss ⇒ Object (readonly)
Returns the value of attribute ss.
7 8 9 |
# File 'lib/goodsheet/spreadsheet.rb', line 7 def ss @ss end |
Instance Method Details
#get_header ⇒ Array<Object>
Get the header row of the currently selected sheet
69 70 71 |
# File 'lib/goodsheet/spreadsheet.rb', line 69 def get_header @ss.row(@s_opts[index][:header_row]+1) # because roo in 1-based end |
#index ⇒ Fixnum
Get the index of current (default) sheet
83 84 85 |
# File 'lib/goodsheet/spreadsheet.rb', line 83 def index @ss.sheets.index(@ss.default_sheet) end |
#name ⇒ String
Get the name of current (default) sheet
76 77 78 |
# File 'lib/goodsheet/spreadsheet.rb', line 76 def name @ss.default_sheet end |
#options ⇒ Fixnum
Get the options of current sheet
61 62 63 |
# File 'lib/goodsheet/spreadsheet.rb', line 61 def @s_opts[index] end |
#read(options = {}) { ... } ⇒ ReadResult
Validate and, if successful, read the current sheet.
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/goodsheet/spreadsheet.rb', line 135 def read(={}, &block) set_variables() row_class = Row.extend_with(block) read_result = ReadResult.new(row_class.attributes, @max_errors, [:collector]||:a_arr) last_row = @row_limit.zero? ? @ss.last_row : min(@ss.last_row, @row_limit+@skip) (@skip+1).upto(last_row) do |r| break unless read_result.add(r, row_class.new(@ss.row(r), @force_nil)) end read_result end |
#rows_wo_skipped ⇒ Fixnum Also known as: rows
Get the number of all rows minus the skipped ones (of the currently selected sheet)
97 98 99 |
# File 'lib/goodsheet/spreadsheet.rb', line 97 def rows_wo_skipped @ss.parse.size - @s_opts[index][:skip] end |
#sheet(idx, options = {}) ⇒ Object
Select the desidered sheet.
38 39 40 41 42 |
# File 'lib/goodsheet/spreadsheet.rb', line 38 def sheet(idx, ={}) check_sheet_exists(idx) @ss.sheet(idx) (idx, ) end |
#sheets ⇒ Array<String>
Get the sheet names list
47 48 49 |
# File 'lib/goodsheet/spreadsheet.rb', line 47 def sheets @ss.sheets end |
#size ⇒ Fixnum
Get the number of sheets
54 55 56 |
# File 'lib/goodsheet/spreadsheet.rb', line 54 def size @ss.sheets.size end |
#total_rows ⇒ Fixnum
Get the total number of rows (of the currently selected sheet)
90 91 92 |
# File 'lib/goodsheet/spreadsheet.rb', line 90 def total_rows @ss.parse.size end |
#validate(options = {}) { ... } ⇒ ValidationErrors
Validate the current sheet.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/goodsheet/spreadsheet.rb', line 112 def validate(={}, &block) set_variables() errors = ValidationErrors.new(@max_errors) row_class = Row.extend_with(block) last_row = @row_limit.zero? ? @ss.last_row : min(@ss.last_row, @row_limit+@skip) (@skip+1).upto(last_row) do |r| break unless errors.add(r, row_class.new(@ss.row(r), @force_nil)) end errors end |