Class: Xsv::Sheet

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Helpers
Defined in:
lib/xsv/sheet.rb

Overview

Sheet represents a single worksheet from a workbook and is normally accessed through Workbook#sheets

Xsv is designed for worksheets with a single table of data, optionally with a header row. Because sheet implements Enumerable the rows in the worksheet can be iterated over using methods such as ‘#each` and `#map`

By default Sheet will return rows as arrays. But by calling the #parse_headers! method the first row of the sheet will be parsed and Sheet will switch to hash mode, returning each row as a hash with the values from the first row as keys.

If the sheet contains leading data before the first row of data or the header row, this can be skipped by setting the #row_skip attribute.

Constant Summary

Constants included from Helpers

Helpers::A_CODEPOINT, Helpers::BUILT_IN_NUMBER_FORMATS, Helpers::EPOCH, Helpers::HOUR, Helpers::MINUTE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#column_index, #parse_date, #parse_datetime, #parse_number, #parse_number_format, #parse_time

Constructor Details

#initialize(workbook, io, ids) ⇒ Sheet

Create a new instance of Sheet. This is used internally by the Workbook. There is no need to create Sheets from application code.

Parameters:

  • workbook (Workbook)

    The Workbook with shared data such as shared strings and styles

  • io (IO)

    A handle to an open worksheet XML file



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xsv/sheet.rb', line 33

def initialize(workbook, io, ids)
  @workbook = workbook
  @id = ids[:sheetId].to_i
  @io = io
  @name = ids[:name]
  @headers = []
  @mode = :array
  @row_skip = 0
  @hidden = ids[:state] == "hidden"

  @last_row, @column_count = SheetBoundsHandler.get_bounds(@io, @workbook)
end

Instance Attribute Details

#idSymbol (readonly)

Returns the current mode. Call #parse_headers! to switch to ‘:hash` mode

Returns:

  • (Symbol)

    ‘:hash` or `:array`



21
22
23
# File 'lib/xsv/sheet.rb', line 21

def id
  @id
end

#modeSymbol (readonly)

Returns the current mode. Call #parse_headers! to switch to ‘:hash` mode

Returns:

  • (Symbol)

    ‘:hash` or `:array`



21
22
23
# File 'lib/xsv/sheet.rb', line 21

def mode
  @mode
end

#nameSymbol (readonly)

Returns the current mode. Call #parse_headers! to switch to ‘:hash` mode

Returns:

  • (Symbol)

    ‘:hash` or `:array`



21
22
23
# File 'lib/xsv/sheet.rb', line 21

def name
  @name
end

#row_skipObject

Set a number of rows to skip at the top of the sheet (header row offset). For hash mode, do not skip the header row as this will be automatically skipped.



26
27
28
# File 'lib/xsv/sheet.rb', line 26

def row_skip
  @row_skip
end

Instance Method Details

#[](number) ⇒ Object

Get row by number, starting at 0. Returns either a hash or an array based on the current row. If the specified index is out of bounds an empty row is returned.



69
70
71
72
73
74
75
# File 'lib/xsv/sheet.rb', line 69

def [](number)
  each_with_index do |row, i|
    return row if i == number
  end

  empty_row
end

#each_row(&block) ⇒ Object Also known as: each

Iterate over rows, returning either hashes or arrays based on the current mode.



57
58
59
60
61
62
63
# File 'lib/xsv/sheet.rb', line 57

def each_row(&block)
  return to_enum(__method__) unless block

  @io.rewind
  SheetRowsHandler.new(@mode, @headers, empty_row, @workbook, @row_skip, @last_row, &block).parse(@io)
  true
end

#headersObject

Return the headers of the sheet as an array



94
95
96
97
98
99
100
# File 'lib/xsv/sheet.rb', line 94

def headers
  if @headers.any?
    @headers
  else
    parse_headers
  end
end

#hidden?Boolean

Returns true if the worksheet is hidden

Returns:

  • (Boolean)


52
53
54
# File 'lib/xsv/sheet.rb', line 52

def hidden?
  @hidden
end

#inspectString

Returns:

  • (String)


47
48
49
# File 'lib/xsv/sheet.rb', line 47

def inspect
  "#<#{self.class.name}:#{object_id} mode=#{@mode}>"
end

#parse_headers!self

Load headers in the top row of the worksheet. After parsing of headers all methods return hashes instead of arrays

Returns:

  • (self)


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/xsv/sheet.rb', line 80

def parse_headers!
  @headers = parse_headers

  # Check for duplicate headers, but don't care about nil columns
  if (duplicate_header = @headers.detect { |h| @headers.count(h) > 1 })
    raise Xsv::DuplicateHeaders, "Duplicate header '#{duplicate_header}' found, consider parsing this sheet in array mode."
  end

  @mode = :hash

  self
end