You are here

public function PHPExcel_Worksheet::rangeToArray in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Worksheet.php \PHPExcel_Worksheet::rangeToArray()

Create array from a range of cells

Parameters

string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1"):

mixed $nullValue Value returned in the array entry if a cell doesn't exist:

boolean $calculateFormulas Should formulas be calculated?:

boolean $formatData Should formatting be applied to cell values?:

boolean $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero: True - Return rows and columns indexed by their actual row and column IDs

Return value

array

1 call to PHPExcel_Worksheet::rangeToArray()
PHPExcel_Worksheet::toArray in vendor/phpoffice/phpexcel/Classes/PHPExcel/Worksheet.php
Create array from worksheet

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Worksheet.php, line 2451

Class

PHPExcel_Worksheet
PHPExcel_Worksheet

Code

public function rangeToArray($pRange = 'A1', $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false) {

  // Returnvalue
  $returnValue = array();

  //    Identify the range that we need to extract from the worksheet
  list($rangeStart, $rangeEnd) = PHPExcel_Cell::rangeBoundaries($pRange);
  $minCol = PHPExcel_Cell::stringFromColumnIndex($rangeStart[0] - 1);
  $minRow = $rangeStart[1];
  $maxCol = PHPExcel_Cell::stringFromColumnIndex($rangeEnd[0] - 1);
  $maxRow = $rangeEnd[1];
  $maxCol++;

  // Loop through rows
  $r = -1;
  for ($row = $minRow; $row <= $maxRow; ++$row) {
    $rRef = $returnCellRef ? $row : ++$r;
    $c = -1;

    // Loop through columns in the current row
    for ($col = $minCol; $col != $maxCol; ++$col) {
      $cRef = $returnCellRef ? $col : ++$c;

      //    Using getCell() will create a new cell if it doesn't already exist. We don't want that to happen
      //        so we test and retrieve directly against _cellCollection
      if ($this->_cellCollection
        ->isDataSet($col . $row)) {

        // Cell exists
        $cell = $this->_cellCollection
          ->getCacheData($col . $row);
        if ($cell
          ->getValue() !== null) {
          if ($cell
            ->getValue() instanceof PHPExcel_RichText) {
            $returnValue[$rRef][$cRef] = $cell
              ->getValue()
              ->getPlainText();
          }
          else {
            if ($calculateFormulas) {
              $returnValue[$rRef][$cRef] = $cell
                ->getCalculatedValue();
            }
            else {
              $returnValue[$rRef][$cRef] = $cell
                ->getValue();
            }
          }
          if ($formatData) {
            $style = $this->_parent
              ->getCellXfByIndex($cell
              ->getXfIndex());
            $returnValue[$rRef][$cRef] = PHPExcel_Style_NumberFormat::toFormattedString($returnValue[$rRef][$cRef], $style && $style
              ->getNumberFormat() ? $style
              ->getNumberFormat()
              ->getFormatCode() : PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
          }
        }
        else {

          // Cell holds a NULL
          $returnValue[$rRef][$cRef] = $nullValue;
        }
      }
      else {

        // Cell doesn't exist
        $returnValue[$rRef][$cRef] = $nullValue;
      }
    }
  }

  // Return
  return $returnValue;
}