Sha256: 7a864f52c80985083f980aac3299fa92c08def3b8e660e5ac7aae46884151700

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

module TableTransform

  # Help functions to create formulas
  module FormulaHelper
    # Reference a table
    def self.table(name)
      "#{name}[]"
    end

    # Reference a column in same table
    def self.column(name)
      "[#{name}]"
    end

    # Quotes text to be used inside formulas
    def self.text(txt)
      "\"#{txt}\""
    end

    # vlookup helper, search for a value in another table with return column specified by name
    # Use other help functions to create an excel expression
    #
    # @param [excel expression] search_value, value to lookup
    # @param [string]           table_name, name of the table to search in
    # @param [string]           return_col_name, name of the return column in given table
    # @param [excel expression] default, value if nothing was found, otherwise Excel will show N/A
    def self.vlookup(search_value, table_name, return_col_name, default = nil)
      vlookup = "VLOOKUP(#{search_value},#{table(table_name)},COLUMN(#{table_name}[[#Headers],#{column(return_col_name)}]),FALSE)"

      # Workaround
      # Should be possible to write "IFNA(#{vlookup},#{default})"
      # but Excel will error with #Name? until formula is updated by hand
      default.nil? ? vlookup : "IF(ISNA(#{vlookup}),#{default},#{vlookup})"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
table_transform-0.6.2 lib/table_transform/formula_helper.rb
table_transform-0.6.1 lib/table_transform/formula_helper.rb
table_transform-0.6.0 lib/table_transform/formula_helper.rb
table_transform-0.5.0 lib/table_transform/formula_helper.rb
table_transform-0.4.0 lib/table_transform/formula_helper.rb