Module: Roo::Utils
Constant Summary collapse
- LETTERS =
('A'..'Z').to_a
Instance Method Summary collapse
- #coordinates_in_range(str) ⇒ Object
-
#each_element(path, elements) ⇒ Object
Yield each element of a given type (‘row’, ‘c’, etc.) to caller.
- #extract_coordinate(s) ⇒ Object (also: #ref_to_key)
- #letter_to_number(letters) ⇒ Object
- #load_xml(path) ⇒ Object
-
#num_cells_in_range(str) ⇒ Object
Compute upper bound for cells in a given cell range.
-
#number_to_letter(num) ⇒ Object
convert a number to something like ‘AB’ (1 => ‘A’, 2 => ‘B’, …).
- #split_coord(str) ⇒ Object
- #split_coordinate(str) ⇒ Object
Instance Method Details
#coordinates_in_range(str) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/roo/utils.rb', line 78 def coordinates_in_range(str) return to_enum(:coordinates_in_range, str) unless block_given? coordinates = str.split(":", 2).map! { |s| extract_coordinate s } case coordinates.size when 1 yield coordinates[0] when 2 tl, br = coordinates rows = tl.row..br.row cols = tl.column..br.column rows.each do |row| cols.each do |column| yield Excelx::Coordinate.new(row, column) end end end end |
#each_element(path, elements) ⇒ Object
Yield each element of a given type (‘row’, ‘c’, etc.) to caller
104 105 106 107 108 109 110 |
# File 'lib/roo/utils.rb', line 104 def each_element(path, elements) elements = Array(elements) Nokogiri::XML::Reader(::File.open(path, 'rb'), nil, nil, Nokogiri::XML::ParseOptions::NOBLANKS).each do |node| next unless node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT && elements.include?(node.name) yield Nokogiri::XML(node.outer_xml).root if block_given? end end |
#extract_coordinate(s) ⇒ Object Also known as: ref_to_key
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/roo/utils.rb', line 9 def extract_coordinate(s) num = letter_num = 0 num_only = false s.each_byte do |b| if !num_only && (index = char_index(b)) letter_num *= 26 letter_num += index elsif index = num_index(b) num_only = true num *= 10 num += index else fail ArgumentError end end fail ArgumentError if letter_num == 0 || !num_only Excelx::Coordinate.new(num, letter_num) end |
#letter_to_number(letters) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/roo/utils.rb', line 56 def letter_to_number(letters) @letter_to_number ||= {} @letter_to_number[letters] ||= begin result = 0 # :bytes method returns an enumerator in 1.9.3 and an array in 2.0+ letters.bytes.to_a.map{|b| b > 96 ? b - 96 : b - 64 }.reverse.each_with_index{ |num, i| result += num * 26 ** i } result end end |
#load_xml(path) ⇒ Object
97 98 99 100 101 |
# File 'lib/roo/utils.rb', line 97 def load_xml(path) ::File.open(path, 'rb') do |file| ::Nokogiri::XML(file) end end |
#num_cells_in_range(str) ⇒ Object
Compute upper bound for cells in a given cell range.
69 70 71 72 73 74 75 76 |
# File 'lib/roo/utils.rb', line 69 def num_cells_in_range(str) cells = str.split(':') return 1 if cells.count == 1 raise ArgumentError.new("invalid range string: #{str}. Supported range format 'A1:B2'") if cells.count != 2 x1, y1 = extract_coordinate(cells[0]) x2, y2 = extract_coordinate(cells[1]) (x2 - (x1 - 1)) * (y2 - (y1 - 1)) end |
#number_to_letter(num) ⇒ Object
convert a number to something like ‘AB’ (1 => ‘A’, 2 => ‘B’, …)
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/roo/utils.rb', line 45 def number_to_letter(num) result = +"" until num.zero? num, index = (num - 1).divmod(26) result.prepend(LETTERS[index]) end result end |
#split_coord(str) ⇒ Object
39 40 41 42 |
# File 'lib/roo/utils.rb', line 39 def split_coord(str) coord = extract_coordinate(str) [number_to_letter(coord.column), coord.row] end |
#split_coordinate(str) ⇒ Object
32 33 34 35 |
# File 'lib/roo/utils.rb', line 32 def split_coordinate(str) warn "[DEPRECATION] `Roo::Utils.split_coordinate` is deprecated. Please use `Roo::Utils.extract_coordinate` instead." extract_coordinate(str) end |