Пример #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 = Worksheet::extractSheetTitle($pCoordinate, true);
         return $this->getParent()->getSheetByName($worksheetReference[0])->cellExists($worksheetReference[1]);
     }
     // Named range?
     if (!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $pCoordinate, $matches) && preg_match('/^' . Calculation::CALCULATION_REGEXP_NAMEDRANGE . '$/i', $pCoordinate, $matches)) {
         $namedRange = 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 = Cell::coordinateFromString($pCoordinate);
         // Cell exists?
         return $this->_cellCollection->isDataSet($pCoordinate);
     }
 }
Пример #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) = Cell::rangeBoundaries($pRange);
     $minCol = Cell::stringFromColumnIndex($rangeStart[0] - 1);
     $minRow = $rangeStart[1];
     $maxCol = 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 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] = Style\NumberFormat::toFormattedString($returnValue[$rRef][$cRef], $style && $style->getNumberFormat() ? $style->getNumberFormat()->getFormatCode() : 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;
 }