class Xsv::Sheet

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.

Attributes

id[R]

Returns the current mode. Call {#parse_headers!} to switch to `:hash` mode @return [Symbol] `:hash` or `:array`

mode[R]

Returns the current mode. Call {#parse_headers!} to switch to `:hash` mode @return [Symbol] `:hash` or `:array`

name[R]

Returns the current mode. Call {#parse_headers!} to switch to `:hash` mode @return [Symbol] `:hash` or `:array`

row_skip[RW]

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.

Public Class Methods

new(workbook, io, size, ids) click to toggle source

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

@param workbook [Workbook] The Workbook with shared data such as shared strings and styles @param io [IO] A handle to an open worksheet XML file @param size [Number] size of the XML file

# File lib/xsv/sheet.rb, line 34
def initialize(workbook, io, size, ids)
  @workbook = workbook
  @id = ids[:sheetId].to_i
  @io = io
  @name = ids[:name]
  @size = size
  @headers = []
  @mode = :array
  @row_skip = 0
  @hidden = ids[:state] == 'hidden'

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

Public Instance Methods

[](number) click to toggle source

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.

# File lib/xsv/sheet.rb, line 73
def [](number)
  each_with_index do |row, i|
    return row if i == number
  end

  empty_row
end
each(&block)
Alias for: each_row
each_row(&block) click to toggle source

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

# File lib/xsv/sheet.rb, line 59
def each_row(&block)
  @io.rewind

  handler = SheetRowsHandler.new(@mode, empty_row, @workbook, @row_skip, @last_row, &block)

  handler.parse(@io)

  true
end
Also aliased as: each
headers() click to toggle source

Return the headers of the sheet as an array

# File lib/xsv/sheet.rb, line 92
def headers
  if @headers.any?
    @headers
  else
    parse_headers
  end
end
hidden?() click to toggle source

Returns true if the worksheet is hidden

# File lib/xsv/sheet.rb, line 54
def hidden?
  @hidden
end
inspect() click to toggle source

@return [String]

# File lib/xsv/sheet.rb, line 49
def inspect
  "#<#{self.class.name}:#{object_id}>"
end
parse_headers!() click to toggle source

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

# File lib/xsv/sheet.rb, line 84
def parse_headers!
  @headers = parse_headers
  @mode = :hash

  self
end

Private Instance Methods

empty_row() click to toggle source
# File lib/xsv/sheet.rb, line 112
def empty_row
  case @mode
  when :array
    [nil] * @column_count
  when :hash
    @headers.zip([]).to_h
  end
end
parse_headers() click to toggle source
# File lib/xsv/sheet.rb, line 102
def parse_headers
  case @mode
  when :array
    first
  when :hash
    @mode = :array
    headers.tap { @mode = :hash }
  end || []
end