You are here

public static function PHPExcel_Calculation_LookupRef::VLOOKUP in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/LookupRef.php \PHPExcel_Calculation_LookupRef::VLOOKUP()

* VLOOKUP * The VLOOKUP function searches for value in the left-most column of lookup_array and returns the value in the same row based on the index_number. *

Parameters

lookup_value The value that you want to match in lookup_array: * @param lookup_array The range of cells being searched * @param index_number The column number in table_array from which the matching value must be returned. The first column is 1. * @param not_exact_match Determines if you are looking for an exact match based on lookup_value. * @return mixed The value of the found cell

1 call to PHPExcel_Calculation_LookupRef::VLOOKUP()
PHPExcel_Calculation_LookupRef::LOOKUP in vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/LookupRef.php
* LOOKUP * The LOOKUP function searches for value either from a one-row or one-column range or from an array. *

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/LookupRef.php, line 693

Class

PHPExcel_Calculation_LookupRef
PHPExcel_Calculation_LookupRef

Code

public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match = true) {
  $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
  $index_number = PHPExcel_Calculation_Functions::flattenSingleValue($index_number);
  $not_exact_match = PHPExcel_Calculation_Functions::flattenSingleValue($not_exact_match);

  // index_number must be greater than or equal to 1
  if ($index_number < 1) {
    return PHPExcel_Calculation_Functions::VALUE();
  }

  // index_number must be less than or equal to the number of columns in lookup_array
  if (!is_array($lookup_array) || empty($lookup_array)) {
    return PHPExcel_Calculation_Functions::REF();
  }
  else {
    $f = array_keys($lookup_array);
    $firstRow = array_pop($f);
    if (!is_array($lookup_array[$firstRow]) || $index_number > count($lookup_array[$firstRow])) {
      return PHPExcel_Calculation_Functions::REF();
    }
    else {
      $columnKeys = array_keys($lookup_array[$firstRow]);
      $returnColumn = $columnKeys[--$index_number];
      $firstColumn = array_shift($columnKeys);
    }
  }
  if (!$not_exact_match) {
    uasort($lookup_array, array(
      'self',
      '_vlookupSort',
    ));
  }
  $rowNumber = $rowValue = False;
  foreach ($lookup_array as $rowKey => $rowData) {
    if (is_numeric($lookup_value) && is_numeric($rowData[$firstColumn]) && $rowData[$firstColumn] > $lookup_value || !is_numeric($lookup_value) && !is_numeric($rowData[$firstColumn]) && strtolower($rowData[$firstColumn]) > strtolower($lookup_value)) {
      break;
    }
    $rowNumber = $rowKey;
    $rowValue = $rowData[$firstColumn];
  }
  if ($rowNumber !== false) {
    if (!$not_exact_match && $rowValue != $lookup_value) {

      //	if an exact match is required, we have what we need to return an appropriate response
      return PHPExcel_Calculation_Functions::NA();
    }
    else {

      //	otherwise return the appropriate value
      return $lookup_array[$rowNumber][$returnColumn];
    }
  }
  return PHPExcel_Calculation_Functions::NA();
}