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. The first sheet will be selected.

Parameters:

  • filename (String)

    The spreadsheet filename you want to read

  • options (Hash) (defaults to: {})

    Options to define the behaviour on reading and validation the sheets. These options are applied to all sheets, but can be overwritten by similar options when selecting the sheet or calling read or validate method

Options Hash (options):

  • :skip (Fixnum) — default: 1

    Number of rows to skip

  • :header_row (Fixnum) — default: 0

    The header row index (0-based)

  • :max_errors (Fixnum) — default: 0

    Max number of error until stop validation

  • :row_limit (Fixnum) — default: 0

    Max number of row to read

  • :force_nil (Object) — default: nil

    Force nils found to this value



18
19
20
21
22
23
24
25
26
# File 'lib/goodsheet/spreadsheet.rb', line 18

def initialize(filename, options={})
  # set_book_options(options)
  @filename = filename
  @ss = Roo::Spreadsheet.open(filename, options)
  @s_opts = Array.new(size, {})
  size.times do |i|
    set_sheet_options(i, options)
  end
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

#s_optsObject (readonly)

Returns the value of attribute s_opts.



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

def s_opts
  @s_opts
end

#skipObject (readonly)

Returns the value of attribute skip.



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

def skip
  @skip
end

#ssObject (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_headerArray<Object>

Get the header row of the currently selected sheet

Returns:

  • (Array<Object>)

    An array cell content objects (String, Float, …)



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

#indexFixnum

Get the index of current (default) sheet

Returns:

  • (Fixnum)

    The sheet index



83
84
85
# File 'lib/goodsheet/spreadsheet.rb', line 83

def index
  @ss.sheets.index(@ss.default_sheet)
end

#nameString

Get the name of current (default) sheet

Returns:

  • (String)

    The sheet name



76
77
78
# File 'lib/goodsheet/spreadsheet.rb', line 76

def name
  @ss.default_sheet
end

#optionsFixnum

Get the options of current sheet

Returns:

  • (Fixnum)

    Number of sheets.



61
62
63
# File 'lib/goodsheet/spreadsheet.rb', line 61

def options
  @s_opts[index]
end

#read(options = {}) { ... } ⇒ ReadResult

Validate and, if successful, read the current sheet.

Parameters:

  • options (Hash) (defaults to: {})

    Reading and validation options for the current sheet.

Options Hash (options):

  • :skip (Fixnum) — default: 1

    Number of rows to skip

  • :header_row (Fixnum) — default: 0

    The header row index (0-based)

  • :max_errors (Fixnum) — default: 0

    Max number of error until stop validation

  • :row_limit (Fixnum) — default: 0

    Max number of row to read

  • :force_nil (Object) — default: nil

    Force nils found to this value

Yields:

  • Column settings and validation rules

Returns:



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/goodsheet/spreadsheet.rb', line 135

def read(options={}, &block)
  set_variables(options)
  row_class = Row.extend_with(block)
  read_result = ReadResult.new(row_class.attributes, @max_errors, options[: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_skippedFixnum Also known as: rows

Get the number of all rows minus the skipped ones (of the currently selected sheet)

Returns:

  • (Fixnum)

    The number of rows



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.

Parameters:

  • idx (Fixnum, String)

    The index (0-based) or the name of the sheet to select

  • options (Hash) (defaults to: {})

    Options to define the behaviour on reading and validation the sheet. These options are applied only to the current sheet, but can be overwritten by similar options when calling read or validate method

Options Hash (options):

  • :skip (Fixnum) — default: 1

    Number of rows to skip

  • :header_row (Fixnum) — default: 0

    The header row index (0-based)

  • :max_errors (Fixnum) — default: 0

    Max number of error until stop validation

  • :row_limit (Fixnum) — default: 0

    Max number of row to read

  • :force_nil (Object) — default: nil

    Force nils found to this value



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

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

#sheetsArray<String>

Get the sheet names list

Returns:

  • (Array<String>)

    An array with sheet names.



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

def sheets
  @ss.sheets
end

#sizeFixnum

Get the number of sheets

Returns:

  • (Fixnum)

    Number of sheets.



54
55
56
# File 'lib/goodsheet/spreadsheet.rb', line 54

def size
  @ss.sheets.size
end

#total_rowsFixnum

Get the total number of rows (of the currently selected sheet)

Returns:

  • (Fixnum)

    The number of rows



90
91
92
# File 'lib/goodsheet/spreadsheet.rb', line 90

def total_rows
  @ss.parse.size
end

#validate(options = {}) { ... } ⇒ ValidationErrors

Validate the current sheet.

Parameters:

  • options (Hash) (defaults to: {})

    Validation options for the current sheet.

Options Hash (options):

  • :skip (Fixnum) — default: 1

    Number of rows to skip

  • :header_row (Fixnum) — default: 0

    The header row index (0-based)

  • :max_errors (Fixnum) — default: 0

    Max number of error until stop validation

  • :row_limit (Fixnum) — default: 0

    Max number of row to read

  • :force_nil (Object) — default: nil

    Force nils found to this value

Yields:

  • Column settings and validation rules

Returns:



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/goodsheet/spreadsheet.rb', line 112

def validate(options={}, &block)
  set_variables(options)
  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