Exemple #1
0
 /**
  * Cell at a specific coordinate exists?
  *
  * @param 	string 			$pCoordinate	Coordinate of the cell
  * @throws 	Exception
  * @return 	boolean
  */
 public function cellExists($pCoordinate = 'A1')
 {
     // Worksheet reference?
     if (strpos($pCoordinate, '!') !== false) {
         $worksheetReference = PHPExcel_Worksheet::extractSheetTitle($pCoordinate, true);
         return $this->getParent()->getSheetByName($worksheetReference[0])->cellExists($worksheetReference[1]);
     }
     // Named range?
     if (!preg_match('/^' . PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $pCoordinate, $matches) && preg_match('/^' . PHPExcel_Calculation::CALCULATION_REGEXP_NAMEDRANGE . '$/i', $pCoordinate, $matches)) {
         $namedRange = PHPExcel_NamedRange::resolveRange($pCoordinate, $this);
         if (!is_null($namedRange)) {
             $pCoordinate = $namedRange->getRange();
             if ($this->getHashCode() != $namedRange->getWorksheet()->getHashCode()) {
                 if (!$namedRange->getLocalOnly()) {
                     return $namedRange->getWorksheet()->cellExists($pCoordinate);
                 } else {
                     throw new Exception('Named range ' . $namedRange->getName() . ' is not accessible from within sheet ' . $this->getTitle());
                 }
             }
         }
     }
     // Uppercase coordinate
     $pCoordinate = strtoupper($pCoordinate);
     if (strpos($pCoordinate, ':') !== false || strpos($pCoordinate, ',') !== false) {
         throw new Exception('Cell coordinate can not be a range of cells.');
     } elseif (strpos($pCoordinate, '$') !== false) {
         throw new Exception('Cell coordinate must not be absolute.');
     } else {
         // Coordinates
         $aCoordinates = PHPExcel_Cell::coordinateFromString($pCoordinate);
         // Cell exists?
         return $this->_cellCollection->isDataSet($pCoordinate);
     }
 }
Exemple #2
0
    /**
     * Create array from a range of cells
     *
     * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
     * @param mixed $nullValue Value returned in the array entry if a cell doesn't exist
     * @param boolean $calculateFormulas Should formulas be calculated?
     * @param boolean $formatData Should formatting be applied to cell values?
     * @param 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 array
     */
	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;
    }