Class: Xsv::SheetBoundsHandler

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

Overview

SheetBoundsHandler scans a sheet looking for the outer bounds of the content within. This is used internally when opening a sheet to deal with worksheets that do not have a correct dimension tag.

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

Class Method Summary collapse

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(trim_empty_rows, &block) ⇒ SheetBoundsHandler

Returns a new instance of SheetBoundsHandler.



28
29
30
31
32
33
34
35
36
# File 'lib/xsv/sheet_bounds_handler.rb', line 28

def initialize(trim_empty_rows, &block)
  @block = block
  @state = nil
  @cell = nil
  @row = nil
  @max_row = 0
  @max_column = 0
  @trim_empty_rows = trim_empty_rows
end

Class Method Details

.get_bounds(sheet, workbook) ⇒ Object



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

def self.get_bounds(sheet, workbook)
  rows = 0
  cols = 0

  handler = new(workbook.trim_empty_rows) do |row, col|
    rows = row
    cols = col.zero? ? 0 : col + 1

    return rows, cols
  end

  sheet.rewind

  handler.parse(sheet)

  [rows, cols]
end

Instance Method Details

#end_element(name) ⇒ Object



65
66
67
# File 'lib/xsv/sheet_bounds_handler.rb', line 65

def end_element(name)
  @block.call(@max_row, @max_column) if name == "sheetData"
end

#start_element(name, attrs) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xsv/sheet_bounds_handler.rb', line 38

def start_element(name, attrs)
  case name
  when "c"
    @state = name
    @cell = attrs[:r]
  when "v"
    col = column_index(@cell)
    @max_column = col if col > @max_column
    @max_row = @row if @row > @max_row
  when "row"
    @state = name
    @row = attrs[:r].to_i
  when "dimension"
    @state = name

    _first_cell, last_cell = attrs[:ref].split(":")

    if last_cell
      @max_column = column_index(last_cell)
      unless @trim_empty_rows
        @max_row = last_cell[/\d+$/].to_i
        @block.call(@max_row, @max_column)
      end
    end
  end
end