Class: Xsv::Sheet
- Inherits:
-
Object
- Object
- Xsv::Sheet
- 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
-
#id ⇒ Symbol
readonly
Returns the current mode.
-
#mode ⇒ Symbol
readonly
Returns the current mode.
-
#name ⇒ Symbol
readonly
Returns the current mode.
-
#row_skip ⇒ Object
Set a number of rows to skip at the top of the sheet (header row offset).
Instance Method Summary collapse
-
#[](number) ⇒ Object
Get row by number, starting at 0.
-
#each_row(&block) ⇒ Object
(also: #each)
Iterate over rows, returning either hashes or arrays based on the current mode.
-
#headers ⇒ Object
Return the headers of the sheet as an array.
-
#hidden? ⇒ Boolean
Returns true if the worksheet is hidden.
-
#initialize(workbook, io, size, ids) ⇒ Sheet
constructor
Create a new instance of Sheet.
- #inspect ⇒ String
-
#parse_headers! ⇒ self
Load headers in the top row of the worksheet.
Methods included from Helpers
#column_index, #parse_date, #parse_datetime, #parse_number, #parse_number_format, #parse_time
Constructor Details
#initialize(workbook, io, size, 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.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 |
Instance Attribute Details
#id ⇒ Symbol (readonly)
Returns the current mode. Call #parse_headers! to switch to ‘:hash` mode
21 22 23 |
# File 'lib/xsv/sheet.rb', line 21 def id @id end |
#mode ⇒ Symbol (readonly)
Returns the current mode. Call #parse_headers! to switch to ‘:hash` mode
21 22 23 |
# File 'lib/xsv/sheet.rb', line 21 def mode @mode end |
#name ⇒ Symbol (readonly)
Returns the current mode. Call #parse_headers! to switch to ‘:hash` mode
21 22 23 |
# File 'lib/xsv/sheet.rb', line 21 def name @name end |
#row_skip ⇒ Object
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.
73 74 75 76 77 78 79 |
# 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_row(&block) ⇒ Object Also known as: each
Iterate over rows, returning either hashes or arrays based on the current mode.
59 60 61 62 63 64 65 66 67 |
# 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 |
#headers ⇒ Object
Return the headers of the sheet as an array
98 99 100 101 102 103 104 |
# File 'lib/xsv/sheet.rb', line 98 def headers if @headers.any? @headers else parse_headers end end |
#hidden? ⇒ Boolean
Returns true if the worksheet is hidden
54 55 56 |
# File 'lib/xsv/sheet.rb', line 54 def hidden? @hidden end |
#inspect ⇒ String
49 50 51 |
# File 'lib/xsv/sheet.rb', line 49 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
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/xsv/sheet.rb', line 84 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 |