Class: Xsv::SheetRowsHandler

Inherits:
SaxParser show all
Includes:
Helpers
Defined in:
lib/xsv/sheet_rows_handler.rb

Overview

This is the core worksheet parser, implemented as an Ox::Sax handler. This is used internally to enumerate rows.

Constant Summary

Constants included from Helpers

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

Constants inherited from SaxParser

Xsv::SaxParser::ATTR_REGEX

Instance Method Summary collapse

Methods included from Helpers

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

Methods inherited from SaxParser

#parse

Constructor Details

#initialize(mode, empty_row, workbook, row_skip, last_row, &block) ⇒ SheetRowsHandler

Returns a new instance of SheetRowsHandler.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/xsv/sheet_rows_handler.rb', line 9

def initialize(mode, empty_row, workbook, row_skip, last_row, &block)
  @mode = mode
  @empty_row = empty_row
  @workbook = workbook
  @row_skip = row_skip
  @last_row = last_row - @row_skip
  @block = block

  @store_characters = false

  @row_index = 0
  @col_index = 0
  @current_row = {}
  @current_row_number = 0
  @current_cell = {}
  @current_value = +""

  @headers = @empty_row.keys if @mode == :hash
end

Instance Method Details

#characters(value) ⇒ Object



42
43
44
# File 'lib/xsv/sheet_rows_handler.rb', line 42

def characters(value)
  @current_value << value if @store_characters
end

#end_element(name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/xsv/sheet_rows_handler.rb', line 46

def end_element(name)
  case name
  when "v", "is", "t"
    @store_characters = false
  when "c"
    col_index = @current_cell[:r] ? column_index(@current_cell[:r]) : @col_index

    if @mode == :array
      @current_row[col_index] = format_cell
    else
      @current_row[@headers[col_index]] = format_cell
    end

    @col_index += 1
  when "row"
    return if @current_row_number <= @row_skip

    adjusted_row_number = @current_row_number - @row_skip

    @row_index += 1
    @col_index = 0

    # Skip first row if we're in hash mode
    return if adjusted_row_number == 1 && @mode == :hash

    # Pad empty rows
    while @row_index < adjusted_row_number
      @block.call(@empty_row)
      @row_index += 1
      next
    end

    # Do not return empty trailing rows
    return if @row_index > @last_row

    # Add trailing empty columns
    if @mode == :array && @current_row.length < @empty_row.length
      @block.call(@current_row + @empty_row[@current_row.length..])
    else
      @block.call(@current_row)
    end
  end
end

#start_element(name, attrs) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/xsv/sheet_rows_handler.rb', line 29

def start_element(name, attrs)
  case name
  when "c"
    @current_cell = attrs
    @current_value.clear
  when "v", "is", "t"
    @store_characters = true
  when "row"
    @current_row = @mode == :array ? [] : @empty_row.dup
    @current_row_number = attrs[:r].to_i
  end
end