Module: Utilities

Included in:
Worksheet
Defined in:
lib/surpass/utilities.rb

Constant Summary collapse

RE_CELL_EX =
/^(\$)?([A-I]?[A-Z])(\$?)(\d+)$/i

Instance Method Summary collapse

Instance Method Details

#as_boolean(input) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/surpass/utilities.rb', line 40

def as_boolean(input)
  case input
  when 1, true
    true
  when 0, false
    false
  else
    raise "Can't convert #{input} from excel boolean!"
  end
end

#as_excel_date(date) ⇒ Object



28
29
30
31
32
33
# File 'lib/surpass/utilities.rb', line 28

def as_excel_date(date)
  date = DateTime.parse(date.strftime("%c")) if date.is_a?(Time)
  excel_date = (date - Date.civil(1899, 12, 31)).to_f
  excel_date += 1 if excel_date > 59 # Add a day for Excel's missing leap day in 1900
  excel_date
end

#as_numeric(input) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/surpass/utilities.rb', line 51

def as_numeric(input)
  case input
  when true, 1
    1
  when false, 0
    0
  else
    raise "Can't convert #{input} to excel boolean!"
  end
end

#binary_string_to_hex_array(binary_string) ⇒ Object



8
9
10
# File 'lib/surpass/utilities.rb', line 8

def binary_string_to_hex_array(binary_string)
  binary_string.unpack("H*")
end

#cell_to_packed_rowcol(cell) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/surpass/utilities.rb', line 88

def cell_to_packed_rowcol(cell)
  row, col, row_abs, col_abs = cell_to_rowcol(cell)
  raise "Column #{col} is greater than IV (#{MAX_COL})" if col >= MAX_COL
  raise "Row #{row} is greater than #{MAX_ROW} in #{cell}" if row >= MAX_ROW

  col |= row_abs.to_i << 15
  col |= col_abs.to_i << 14

  [row, col]
end

#cell_to_rowcol(cell) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/surpass/utilities.rb', line 79

def cell_to_rowcol(cell)
  match = RE_CELL_EX.match(cell)
  raise "Ill-formed single cell reference #{cell}" if match.nil?
  col_abs, col, row_abs, row = match.captures
  row = row.to_i - 1
  col = col_by_name(col.upcase)
  [row, col, row_abs.nil?, col_abs.nil?]
end

#col_by_name(column_name) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/surpass/utilities.rb', line 69

def col_by_name(column_name)
  col = 0
  pow = 1
  column_name.reverse.each_byte do |l|
    col += (l - 64) * pow
    pow *= 26
  end
  col - 1
end

#hex(value) ⇒ Object

Mimic python’s “hex” function 0x00



63
64
65
# File 'lib/surpass/utilities.rb', line 63

def hex(value)
  "0x" + value.to_s(16)
end

#hex_array_to_binary_string(array_of_hex_values) ⇒ Object

For ease of comparing with pyExcelerator output values python seems to automatically decode to hex values



4
5
6
# File 'lib/surpass/utilities.rb', line 4

def hex_array_to_binary_string(array_of_hex_values)
  [array_of_hex_values.collect {|h| [sprintf("%02x", h)]}.join].pack('H*')
end

#mock_unicode_string(s) ⇒ Object



35
36
37
# File 'lib/surpass/utilities.rb', line 35

def mock_unicode_string(s)
  [s.length, 0].pack('vC') + s
end

#pixels_to_points(pixels) ⇒ Object



16
17
18
# File 'lib/surpass/utilities.rb', line 16

def pixels_to_points(pixels)
  pixels * (3.0 / 4)
end

#pixels_to_twips(pixels) ⇒ Object



24
25
26
# File 'lib/surpass/utilities.rb', line 24

def pixels_to_twips(pixels)
  pixels * 15.0
end

#points_to_pixels(points) ⇒ Object



12
13
14
# File 'lib/surpass/utilities.rb', line 12

def points_to_pixels(points)
  points*(4.0/3)
end

#twips_to_pixels(twips) ⇒ Object



20
21
22
# File 'lib/surpass/utilities.rb', line 20

def twips_to_pixels(twips)
  twips / 15.0
end