Module: Soroban

Defined in:
lib/soroban/cell.rb,
lib/soroban/error.rb,
lib/soroban/sheet.rb,
lib/soroban/parser.rb,
lib/soroban/walker.rb,
lib/soroban/helpers.rb,
lib/soroban/functions.rb

Defined Under Namespace

Classes: Cell, ParseError, RangeError, RecursionError, Sheet, UndefinedError, Walker

Class Method Summary collapse

Class Method Details

.boolean?(data) ⇒ Boolean

Return true if the supplied data is a boolean.

Returns:

  • (Boolean)


16
17
18
# File 'lib/soroban/helpers.rb', line 16

def self.boolean?(data)
  /^(true|false)$/i.match(data.to_s) && true || false
end

.call(sheet, name, *args) ⇒ Object

Call the named function within the context of the specified sheet.



15
16
17
18
19
# File 'lib/soroban/functions.rb', line 15

def self.call(sheet, name, *args)
  function = name.upcase.to_sym
  raise Soroban::UndefinedError, "No such function '#{function}'" unless @@functions[function]
  sheet.instance_exec(*args, &@@functions[function])
end

.define(function_hash) ⇒ Object

Define a new function.



4
5
6
7
# File 'lib/soroban/functions.rb', line 4

def self.define(function_hash)
  @@functions ||= {}
  function_hash.each { |name, callback| @@functions[name] = callback }
end

.formula?(data) ⇒ Boolean

Return true if the supplied data represents a formula.

Returns:

  • (Boolean)


6
7
8
# File 'lib/soroban/helpers.rb', line 6

def self.formula?(data)
  data.to_s.slice(0..0) == '='
end

.functionsObject

Return an array of all defined functions.



10
11
12
# File 'lib/soroban/functions.rb', line 10

def self.functions
  @@functions.keys.map { |f| f.to_s }
end

.getRange(range) ⇒ Object

Return the components of a range.



36
37
38
# File 'lib/soroban/helpers.rb', line 36

def self.getRange(range)
  /^([a-zA-Z]+)([\d]+):([a-zA-Z]+)([\d]+)$/.match(range.to_s).to_a[1..-1]
end

.getValues(binding, *args) ⇒ Object

Return an array of values for the supplied arguments (which may be numbers, labels and ranges).



41
42
43
# File 'lib/soroban/helpers.rb', line 41

def self.getValues(binding, *args)
  args.map { |arg| Soroban::range?(arg) ? Walker.new(arg, binding).map : arg }.flatten
end

.number?(data) ⇒ Boolean

Return true if the supplied data is a number.

Returns:

  • (Boolean)


11
12
13
# File 'lib/soroban/helpers.rb', line 11

def self.number?(data)
  Float(data.to_s) && true rescue false
end

.parserObject

A Treetop parser for Excel formulas that can generate valid Ruby expression via a rewrite operation, and which can build an array of referenced labels.



11
12
13
# File 'lib/soroban/parser.rb', line 11

def self.parser
  @@parser ||= SorobanParser.new
end

.range?(data) ⇒ Boolean

Return true if the supplied data is a range.

Returns:

  • (Boolean)


26
27
28
# File 'lib/soroban/helpers.rb', line 26

def self.range?(data)
  /^([a-zA-Z]+)([\d]+):([a-zA-Z]+)([\d]+)$/.match(data.to_s) && true || false
end

.string?(data) ⇒ Boolean

Return true if the supplied data is a string.

Returns:

  • (Boolean)


21
22
23
# File 'lib/soroban/helpers.rb', line 21

def self.string?(data)
  /^["](\"|[^"])*["]$/.match(data.to_s) && true || /^['][^']*[']$/.match(data.to_s) && true || false
end

.unknown?(data) ⇒ Boolean

Return true if the supplied data is of no recognised format.

Returns:

  • (Boolean)


31
32
33
# File 'lib/soroban/helpers.rb', line 31

def self.unknown?(data)
  !self.formula?(data) && !self.number?(data) && !self.boolean?(data) && !self.string?(data)
end