You are here

public static function PHPExcel_Calculation_LookupRef::INDEX in Loft Data Grids 7.2

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

* INDEX * * Uses an index to choose a value from a reference or array * * Excel Function: * =INDEX(range_array, row_num, [column_num]) * *

Parameters

range_array A range of cells or an array constant: * @param row_num The row in array from which to return a value. If row_num is omitted, column_num is required. * @param column_num The column in array from which to return a value. If column_num is omitted, row_num is required. * @return mixed the value of a specified cell or array of cells

File

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

Class

PHPExcel_Calculation_LookupRef
PHPExcel_Calculation_LookupRef

Code

public static function INDEX($arrayValues, $rowNum = 0, $columnNum = 0) {
  if ($rowNum < 0 || $columnNum < 0) {
    return PHPExcel_Calculation_Functions::VALUE();
  }
  if (!is_array($arrayValues)) {
    return PHPExcel_Calculation_Functions::REF();
  }
  $rowKeys = array_keys($arrayValues);
  $columnKeys = @array_keys($arrayValues[$rowKeys[0]]);
  if ($columnNum > count($columnKeys)) {
    return PHPExcel_Calculation_Functions::VALUE();
  }
  elseif ($columnNum == 0) {
    if ($rowNum == 0) {
      return $arrayValues;
    }
    $rowNum = $rowKeys[--$rowNum];
    $returnArray = array();
    foreach ($arrayValues as $arrayColumn) {
      if (is_array($arrayColumn)) {
        if (isset($arrayColumn[$rowNum])) {
          $returnArray[] = $arrayColumn[$rowNum];
        }
        else {
          return $arrayValues[$rowNum];
        }
      }
      else {
        return $arrayValues[$rowNum];
      }
    }
    return $returnArray;
  }
  $columnNum = $columnKeys[--$columnNum];
  if ($rowNum > count($rowKeys)) {
    return PHPExcel_Calculation_Functions::VALUE();
  }
  elseif ($rowNum == 0) {
    return $arrayValues[$columnNum];
  }
  $rowNum = $rowKeys[--$rowNum];
  return $arrayValues[$rowNum][$columnNum];
}