Class: OoxmlParser::Coordinates

Inherits:
Object
  • Object
show all
Defined in:
lib/ooxml_parser/common_parser/common_data/coordinates.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row = nil, column = nil, list = nil) ⇒ Coordinates

Returns a new instance of Coordinates.



5
6
7
8
9
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 5

def initialize(row = nil, column = nil, list = nil)
  @row = row.nil? ? row : row.to_i
  @column = column
  @list = list
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



3
4
5
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 3

def column
  @column
end

#listObject

Returns the value of attribute list.



3
4
5
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 3

def list
  @list
end

#rowObject

Returns the value of attribute row.



3
4
5
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 3

def row
  @row
end

Class Method Details

.coordinates?(string) ⇒ Boolean

This method check is argument contains coordinate

Parameters:

  • string (String)

Returns:

  • (Boolean)


91
92
93
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 91

def coordinates?(string)
  !/^([A-Z]+)(\d+)$/.match(string).nil?
end

.get_column_name(number) ⇒ String

This method takes string like ‘12’ or ‘45’ etc. and converts into spreadsheet column’s name

StringHelper.get_column_name('12')  #=> "L"
StringHelper.get_column_name('45')  #=> "AS"
StringHelper.get_column_name('287')  #=> "KA"

Parameters:

  • number (String or Fixnum)

    to convert

Returns:

  • (String)

    column’s name



102
103
104
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 102

def get_column_name(number)
  (number.to_i > 0 ? ('A'..'Z').to_a[(number.to_i - 1) % 26] + get_column_name((number.to_i - 1) / 26).reverse : '').reverse
end

.parse_coordinates_array(arguments_string) ⇒ Array

Parse array of coordinates

Parameters:

  • arguments_string (String)

    string

Returns:

  • (Array)

    result



80
81
82
83
84
85
86
87
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 80

def parse_coordinates_array(arguments_string)
  result = []
  coord_array = arguments_string.split(',')
  coord_array.each do |current_coord|
    result << parser_coordinates_range(current_coord)
  end
  result
end

.parse_coordinates_from_string(string) ⇒ Coordinates

Parameters:

  • string (String)

    to parse

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 14

def parse_coordinates_from_string(string)
  coordinates = Coordinates.new
  begin
    coordinates.list = string.match(/'\w+'/)[0].delete("''")
  rescue StandardError
    'Raise Exception'
  end
  string = string.split('!').last
  if coordinates?(string)
    coordinates.row = string.scan(/[0-9]/).join('').to_i
    coordinates.column = string.scan(/[A-Z]/).join('')
  end
  coordinates
end

.parser_coordinates_range(arguments_string) ⇒ Object



29
30
31
32
33
34
35
36
37
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
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 29

def parser_coordinates_range(arguments_string)
  return parse_coordinates_array(arguments_string) if arguments_string.include?(',')
  return warn "Formulas with # is unsupported: #{arguments_string}" if arguments_string.include?('#')
  return warn 'Formulas consists from `!` only' if arguments_string == '!'
  sheet_name = 'Sheet1'

  if arguments_string.include?('!')
    sheet_name = arguments_string.match(/.*!/).to_s
    arguments_string = arguments_string.sub(sheet_name, '')
  end

  range = arguments_string.split(':')

  difference = []
  symbols_from = range.first.scan(/[a-zA-z]/).join
  symbols_to = range.last.scan(/[a-zA-z]/).join
  digits_from = range.first.scan(/[0-9]/).join
  digits_to = range.last.scan(/[0-9]/).join

  difference[0] = [symbols_from, symbols_to] unless symbols_from == symbols_to
  difference[1] = [digits_from, digits_to] unless digits_from == digits_to
  arguments_array = []
  case difference.length
  when 0
    arguments_array << Coordinates.new(digits_from, symbols_from)
  when 1
    (difference.first.first..difference.first.last).each do |symbol|
      arguments_array << Coordinates.new(digits_from, symbol, sheet_name)
    end
  when 2
    case difference.first
    when nil
      (difference.last.first..difference.last.last).each do |digit|
        arguments_array << Coordinates.new(digit, symbols_from, sheet_name)
      end
    else
      (difference.first.first..difference.first.last).each do |symbol|
        (difference.last.first..difference.last.last).each do |digit|
          arguments_array << Coordinates.new(digit, symbol, sheet_name)
        end
      end
    end
  else
    raise 'Wrong arguments format'
  end
  arguments_array
end

Instance Method Details

#==(other) ⇒ Object



139
140
141
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 139

def ==(other)
  other.is_a?(Coordinates) ? (@row == other.row && @column == other.column) : false
end

#column_greater_that_other?(other_cell) ⇒ true, false

Compares columns of two cells

Parameters:

Returns:

  • (true, false)

    true, if column greater, than other row



127
128
129
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 127

def column_greater_that_other?(other_cell)
  get_column_number > other_cell.get_column_number
end

#get_column_numberInteger

get_column_number -> integer This method takes @column string and converts into integer

Returns:

  • (Integer)

    number of column



111
112
113
114
115
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 111

def get_column_number
  @column.reverse.each_char.reduce(0) do |result, char|
    result + (char.downcase.ord - 96) * (26**@column.reverse.index(char))
  end
end

#nil?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 135

def nil?
  @column.nil? && @list.nil? && @row.nil?
end

#row_greater_that_other?(other_cell) ⇒ true, false

Compares rows of two cells

Parameters:

Returns:

  • (true, false)

    true, if row greater, than other row



120
121
122
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 120

def row_greater_that_other?(other_cell)
  @row > other_cell.row
end

#to_sObject



131
132
133
# File 'lib/ooxml_parser/common_parser/common_data/coordinates.rb', line 131

def to_s
  "#{@column}#{@row} #{@list ? "list: #{@list}" : ''}"
end