Пример #1
0
 /**
  * Apply styles from array
  *
  * <code>
  * $objPHPExcel->getActiveSheet()->getStyle('B2')->applyFromArray(
  *         array(
  *             'font'    => array(
  *                 'name'      => 'Arial',
  *                 'bold'      => true,
  *                 'italic'    => false,
  *                 'underline' => \PHPExcel\Style\Font::UNDERLINE_DOUBLE,
  *                 'strike'    => false,
  *                 'color'     => array(
  *                     'rgb' => '808080'
  *                 )
  *             ),
  *             'borders' => array(
  *                 'bottom'     => array(
  *                     'style' => \PHPExcel\Style\Border::BORDER_DASHDOT,
  *                     'color' => array(
  *                         'rgb' => '808080'
  *                     )
  *                 ),
  *                 'top'     => array(
  *                     'style' => \PHPExcel\Style\Border::BORDER_DASHDOT,
  *                     'color' => array(
  *                         'rgb' => '808080'
  *                     )
  *                 )
  *             ),
  *             'quotePrefix'    => true
  *         )
  * );
  * </code>
  *
  * @param   array    $pStyles    Array containing style information
  * @param   boolean        $pAdvanced    Advanced mode for setting borders.
  * @throws  Exception
  * @return Style
  */
 public function applyFromArray($pStyles = null, $pAdvanced = true)
 {
     if (is_array($pStyles)) {
         if ($this->isSupervisor) {
             $pRange = $this->getSelectedCells();
             // Uppercase coordinate
             $pRange = strtoupper($pRange);
             // Is it a cell range or a single cell?
             if (strpos($pRange, ':') === false) {
                 $rangeA = $pRange;
                 $rangeB = $pRange;
             } else {
                 list($rangeA, $rangeB) = explode(':', $pRange);
             }
             // Calculate range outer borders
             $rangeStart = Cell::coordinateFromString($rangeA);
             $rangeEnd = Cell::coordinateFromString($rangeB);
             // Translate column into index
             $rangeStart[0] = Cell::columnIndexFromString($rangeStart[0]) - 1;
             $rangeEnd[0] = Cell::columnIndexFromString($rangeEnd[0]) - 1;
             // Make sure we can loop upwards on rows and columns
             if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) {
                 $tmp = $rangeStart;
                 $rangeStart = $rangeEnd;
                 $rangeEnd = $tmp;
             }
             // ADVANCED MODE:
             if ($pAdvanced && isset($pStyles['borders'])) {
                 // 'allborders' is a shorthand property for 'outline' and 'inside' and
                 //        it applies to components that have not been set explicitly
                 if (isset($pStyles['borders']['allborders'])) {
                     foreach (array('outline', 'inside') as $component) {
                         if (!isset($pStyles['borders'][$component])) {
                             $pStyles['borders'][$component] = $pStyles['borders']['allborders'];
                         }
                     }
                     unset($pStyles['borders']['allborders']);
                     // not needed any more
                 }
                 // 'outline' is a shorthand property for 'top', 'right', 'bottom', 'left'
                 //        it applies to components that have not been set explicitly
                 if (isset($pStyles['borders']['outline'])) {
                     foreach (array('top', 'right', 'bottom', 'left') as $component) {
                         if (!isset($pStyles['borders'][$component])) {
                             $pStyles['borders'][$component] = $pStyles['borders']['outline'];
                         }
                     }
                     unset($pStyles['borders']['outline']);
                     // not needed any more
                 }
                 // 'inside' is a shorthand property for 'vertical' and 'horizontal'
                 //        it applies to components that have not been set explicitly
                 if (isset($pStyles['borders']['inside'])) {
                     foreach (array('vertical', 'horizontal') as $component) {
                         if (!isset($pStyles['borders'][$component])) {
                             $pStyles['borders'][$component] = $pStyles['borders']['inside'];
                         }
                     }
                     unset($pStyles['borders']['inside']);
                     // not needed any more
                 }
                 // width and height characteristics of selection, 1, 2, or 3 (for 3 or more)
                 $xMax = min($rangeEnd[0] - $rangeStart[0] + 1, 3);
                 $yMax = min($rangeEnd[1] - $rangeStart[1] + 1, 3);
                 // loop through up to 3 x 3 = 9 regions
                 for ($x = 1; $x <= $xMax; ++$x) {
                     // start column index for region
                     $colStart = $x == 3 ? Cell::stringFromColumnIndex($rangeEnd[0]) : Cell::stringFromColumnIndex($rangeStart[0] + $x - 1);
                     // end column index for region
                     $colEnd = $x == 1 ? Cell::stringFromColumnIndex($rangeStart[0]) : Cell::stringFromColumnIndex($rangeEnd[0] - $xMax + $x);
                     for ($y = 1; $y <= $yMax; ++$y) {
                         // which edges are touching the region
                         $edges = array();
                         if ($x == 1) {
                             // are we at left edge
                             $edges[] = 'left';
                         }
                         if ($x == $xMax) {
                             // are we at right edge
                             $edges[] = 'right';
                         }
                         if ($y == 1) {
                             // are we at top edge?
                             $edges[] = 'top';
                         }
                         if ($y == $yMax) {
                             // are we at bottom edge?
                             $edges[] = 'bottom';
                         }
                         // start row index for region
                         $rowStart = $y == 3 ? $rangeEnd[1] : $rangeStart[1] + $y - 1;
                         // end row index for region
                         $rowEnd = $y == 1 ? $rangeStart[1] : $rangeEnd[1] - $yMax + $y;
                         // build range for region
                         $range = $colStart . $rowStart . ':' . $colEnd . $rowEnd;
                         // retrieve relevant style array for region
                         $regionStyles = $pStyles;
                         unset($regionStyles['borders']['inside']);
                         // what are the inner edges of the region when looking at the selection
                         $innerEdges = array_diff(array('top', 'right', 'bottom', 'left'), $edges);
                         // inner edges that are not touching the region should take the 'inside' border properties if they have been set
                         foreach ($innerEdges as $innerEdge) {
                             switch ($innerEdge) {
                                 case 'top':
                                 case 'bottom':
                                     // should pick up 'horizontal' border property if set
                                     if (isset($pStyles['borders']['horizontal'])) {
                                         $regionStyles['borders'][$innerEdge] = $pStyles['borders']['horizontal'];
                                     } else {
                                         unset($regionStyles['borders'][$innerEdge]);
                                     }
                                     break;
                                 case 'left':
                                 case 'right':
                                     // should pick up 'vertical' border property if set
                                     if (isset($pStyles['borders']['vertical'])) {
                                         $regionStyles['borders'][$innerEdge] = $pStyles['borders']['vertical'];
                                     } else {
                                         unset($regionStyles['borders'][$innerEdge]);
                                     }
                                     break;
                             }
                         }
                         // apply region style to region by calling applyFromArray() in simple mode
                         $this->getActiveSheet()->getStyle($range)->applyFromArray($regionStyles, false);
                     }
                 }
                 return $this;
             }
             // SIMPLE MODE:
             // Selection type, inspect
             if (preg_match('/^[A-Z]+1:[A-Z]+1048576$/', $pRange)) {
                 $selectionType = 'COLUMN';
             } elseif (preg_match('/^A[0-9]+:XFD[0-9]+$/', $pRange)) {
                 $selectionType = 'ROW';
             } else {
                 $selectionType = 'CELL';
             }
             // First loop through columns, rows, or cells to find out which styles are affected by this operation
             switch ($selectionType) {
                 case 'COLUMN':
                     $oldXfIndexes = array();
                     for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
                         $oldXfIndexes[$this->getActiveSheet()->getColumnDimensionByColumn($col)->getXfIndex()] = true;
                     }
                     break;
                 case 'ROW':
                     $oldXfIndexes = array();
                     for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
                         if ($this->getActiveSheet()->getRowDimension($row)->getXfIndex() == null) {
                             $oldXfIndexes[0] = true;
                             // row without explicit style should be formatted based on default style
                         } else {
                             $oldXfIndexes[$this->getActiveSheet()->getRowDimension($row)->getXfIndex()] = true;
                         }
                     }
                     break;
                 case 'CELL':
                     $oldXfIndexes = array();
                     for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
                         for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
                             $oldXfIndexes[$this->getActiveSheet()->getCellByColumnAndRow($col, $row)->getXfIndex()] = true;
                         }
                     }
                     break;
             }
             // clone each of the affected styles, apply the style array, and add the new styles to the workbook
             $workbook = $this->getActiveSheet()->getParent();
             foreach ($oldXfIndexes as $oldXfIndex => $dummy) {
                 $style = $workbook->getCellXfByIndex($oldXfIndex);
                 $newStyle = clone $style;
                 $newStyle->applyFromArray($pStyles);
                 if ($existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode())) {
                     // there is already such cell Xf in our collection
                     $newXfIndexes[$oldXfIndex] = $existingStyle->getIndex();
                 } else {
                     // we don't have such a cell Xf, need to add
                     $workbook->addCellXf($newStyle);
                     $newXfIndexes[$oldXfIndex] = $newStyle->getIndex();
                 }
             }
             // Loop through columns, rows, or cells again and update the XF index
             switch ($selectionType) {
                 case 'COLUMN':
                     for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
                         $columnDimension = $this->getActiveSheet()->getColumnDimensionByColumn($col);
                         $oldXfIndex = $columnDimension->getXfIndex();
                         $columnDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
                     }
                     break;
                 case 'ROW':
                     for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
                         $rowDimension = $this->getActiveSheet()->getRowDimension($row);
                         $oldXfIndex = $rowDimension->getXfIndex() === null ? 0 : $rowDimension->getXfIndex();
                         // row without explicit style should be formatted based on default style
                         $rowDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
                     }
                     break;
                 case 'CELL':
                     for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
                         for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
                             $cell = $this->getActiveSheet()->getCellByColumnAndRow($col, $row);
                             $oldXfIndex = $cell->getXfIndex();
                             $cell->setXfIndex($newXfIndexes[$oldXfIndex]);
                         }
                     }
                     break;
             }
         } else {
             // not a supervisor, just apply the style array directly on style object
             if (array_key_exists('fill', $pStyles)) {
                 $this->getFill()->applyFromArray($pStyles['fill']);
             }
             if (array_key_exists('font', $pStyles)) {
                 $this->getFont()->applyFromArray($pStyles['font']);
             }
             if (array_key_exists('borders', $pStyles)) {
                 $this->getBorders()->applyFromArray($pStyles['borders']);
             }
             if (array_key_exists('alignment', $pStyles)) {
                 $this->getAlignment()->applyFromArray($pStyles['alignment']);
             }
             if (array_key_exists('numberformat', $pStyles)) {
                 $this->getNumberFormat()->applyFromArray($pStyles['numberformat']);
             }
             if (array_key_exists('protection', $pStyles)) {
                 $this->getProtection()->applyFromArray($pStyles['protection']);
             }
             if (array_key_exists('quotePrefix', $pStyles)) {
                 $this->quotePrefix = $pStyles['quotePrefix'];
             }
         }
     } else {
         throw new Exception("Invalid style array passed.");
     }
     return $this;
 }
Пример #2
0
 /**
  * Write drawings to XML format
  *
  * @param 	Shared_XMLWriter			$objWriter 		XML Writer
  * @param 	Worksheet_BaseDrawing		$pDrawing
  * @param 	int									$pRelationId
  * @throws 	Exception
  */
 public function _writeDrawing(Shared_XMLWriter $objWriter = null, Worksheet_BaseDrawing $pDrawing = null, $pRelationId = -1)
 {
     if ($pRelationId >= 0) {
         // xdr:oneCellAnchor
         $objWriter->startElement('xdr:oneCellAnchor');
         // Image location
         $aCoordinates = Cell::coordinateFromString($pDrawing->getCoordinates());
         $aCoordinates[0] = Cell::columnIndexFromString($aCoordinates[0]);
         // xdr:from
         $objWriter->startElement('xdr:from');
         $objWriter->writeElement('xdr:col', $aCoordinates[0] - 1);
         $objWriter->writeElement('xdr:colOff', Shared_Drawing::pixelsToEMU($pDrawing->getOffsetX()));
         $objWriter->writeElement('xdr:row', $aCoordinates[1] - 1);
         $objWriter->writeElement('xdr:rowOff', Shared_Drawing::pixelsToEMU($pDrawing->getOffsetY()));
         $objWriter->endElement();
         // xdr:ext
         $objWriter->startElement('xdr:ext');
         $objWriter->writeAttribute('cx', Shared_Drawing::pixelsToEMU($pDrawing->getWidth()));
         $objWriter->writeAttribute('cy', Shared_Drawing::pixelsToEMU($pDrawing->getHeight()));
         $objWriter->endElement();
         // xdr:pic
         $objWriter->startElement('xdr:pic');
         // xdr:nvPicPr
         $objWriter->startElement('xdr:nvPicPr');
         // xdr:cNvPr
         $objWriter->startElement('xdr:cNvPr');
         $objWriter->writeAttribute('id', $pRelationId);
         $objWriter->writeAttribute('name', $pDrawing->getName());
         $objWriter->writeAttribute('descr', $pDrawing->getDescription());
         $objWriter->endElement();
         // xdr:cNvPicPr
         $objWriter->startElement('xdr:cNvPicPr');
         // a:picLocks
         $objWriter->startElement('a:picLocks');
         $objWriter->writeAttribute('noChangeAspect', '1');
         $objWriter->endElement();
         $objWriter->endElement();
         $objWriter->endElement();
         // xdr:blipFill
         $objWriter->startElement('xdr:blipFill');
         // a:blip
         $objWriter->startElement('a:blip');
         $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
         $objWriter->writeAttribute('r:embed', 'rId' . $pRelationId);
         $objWriter->endElement();
         // a:stretch
         $objWriter->startElement('a:stretch');
         $objWriter->writeElement('a:fillRect', null);
         $objWriter->endElement();
         $objWriter->endElement();
         // xdr:spPr
         $objWriter->startElement('xdr:spPr');
         // a:xfrm
         $objWriter->startElement('a:xfrm');
         $objWriter->writeAttribute('rot', Shared_Drawing::degreesToAngle($pDrawing->getRotation()));
         $objWriter->endElement();
         // a:prstGeom
         $objWriter->startElement('a:prstGeom');
         $objWriter->writeAttribute('prst', 'rect');
         // a:avLst
         $objWriter->writeElement('a:avLst', null);
         $objWriter->endElement();
         //						// a:solidFill
         //						$objWriter->startElement('a:solidFill');
         //							// a:srgbClr
         //							$objWriter->startElement('a:srgbClr');
         //							$objWriter->writeAttribute('val', 'FFFFFF');
         ///* SHADE
         //								// a:shade
         //								$objWriter->startElement('a:shade');
         //								$objWriter->writeAttribute('val', '85000');
         //								$objWriter->endElement();
         //*/
         //							$objWriter->endElement();
         //						$objWriter->endElement();
         /*
         						// a:ln
         						$objWriter->startElement('a:ln');
         						$objWriter->writeAttribute('w', '88900');
         						$objWriter->writeAttribute('cap', 'sq');
         
         							// a:solidFill
         							$objWriter->startElement('a:solidFill');
         
         								// a:srgbClr
         								$objWriter->startElement('a:srgbClr');
         								$objWriter->writeAttribute('val', 'FFFFFF');
         								$objWriter->endElement();
         
         							$objWriter->endElement();
         
         							// a:miter
         							$objWriter->startElement('a:miter');
         							$objWriter->writeAttribute('lim', '800000');
         							$objWriter->endElement();
         
         						$objWriter->endElement();
         */
         if ($pDrawing->getShadow()->getVisible()) {
             // a:effectLst
             $objWriter->startElement('a:effectLst');
             // a:outerShdw
             $objWriter->startElement('a:outerShdw');
             $objWriter->writeAttribute('blurRad', Shared_Drawing::pixelsToEMU($pDrawing->getShadow()->getBlurRadius()));
             $objWriter->writeAttribute('dist', Shared_Drawing::pixelsToEMU($pDrawing->getShadow()->getDistance()));
             $objWriter->writeAttribute('dir', Shared_Drawing::degreesToAngle($pDrawing->getShadow()->getDirection()));
             $objWriter->writeAttribute('algn', $pDrawing->getShadow()->getAlignment());
             $objWriter->writeAttribute('rotWithShape', '0');
             // a:srgbClr
             $objWriter->startElement('a:srgbClr');
             $objWriter->writeAttribute('val', $pDrawing->getShadow()->getColor()->getRGB());
             // a:alpha
             $objWriter->startElement('a:alpha');
             $objWriter->writeAttribute('val', $pDrawing->getShadow()->getAlpha() * 1000);
             $objWriter->endElement();
             $objWriter->endElement();
             $objWriter->endElement();
             $objWriter->endElement();
         }
         /*
         
         						// a:scene3d
         						$objWriter->startElement('a:scene3d');
         
         							// a:camera
         							$objWriter->startElement('a:camera');
         							$objWriter->writeAttribute('prst', 'orthographicFront');
         							$objWriter->endElement();
         
         							// a:lightRig
         							$objWriter->startElement('a:lightRig');
         							$objWriter->writeAttribute('rig', 'twoPt');
         							$objWriter->writeAttribute('dir', 't');
         
         								// a:rot
         								$objWriter->startElement('a:rot');
         								$objWriter->writeAttribute('lat', '0');
         								$objWriter->writeAttribute('lon', '0');
         								$objWriter->writeAttribute('rev', '0');
         								$objWriter->endElement();
         
         							$objWriter->endElement();
         
         						$objWriter->endElement();
         */
         /*
         						// a:sp3d
         						$objWriter->startElement('a:sp3d');
         
         							// a:bevelT
         							$objWriter->startElement('a:bevelT');
         							$objWriter->writeAttribute('w', '25400');
         							$objWriter->writeAttribute('h', '19050');
         							$objWriter->endElement();
         
         							// a:contourClr
         							$objWriter->startElement('a:contourClr');
         
         								// a:srgbClr
         								$objWriter->startElement('a:srgbClr');
         								$objWriter->writeAttribute('val', 'FFFFFF');
         								$objWriter->endElement();
         
         							$objWriter->endElement();
         
         						$objWriter->endElement();
         */
         $objWriter->endElement();
         $objWriter->endElement();
         // xdr:clientData
         $objWriter->writeElement('xdr:clientData', null);
         $objWriter->endElement();
     } else {
         throw new Exception("Invalid parameters passed.");
     }
 }
Пример #3
0
 /**
  * Write the HORIZONTALPAGEBREAKS and VERTICALPAGEBREAKS BIFF records.
  */
 private function _writeBreaks()
 {
     // initialize
     $vbreaks = array();
     $hbreaks = array();
     foreach ($this->_phpSheet->getBreaks() as $cell => $breakType) {
         // Fetch coordinates
         $coordinates = Cell::coordinateFromString($cell);
         // Decide what to do by the type of break
         switch ($breakType) {
             case Worksheet::BREAK_COLUMN:
                 // Add to list of vertical breaks
                 $vbreaks[] = Cell::columnIndexFromString($coordinates[0]) - 1;
                 break;
             case Worksheet::BREAK_ROW:
                 // Add to list of horizontal breaks
                 $hbreaks[] = $coordinates[1];
                 break;
             case Worksheet::BREAK_NONE:
             default:
                 // Nothing to do
                 break;
         }
     }
     //horizontal page breaks
     if (count($hbreaks) > 0) {
         // Sort and filter array of page breaks
         sort($hbreaks, SORT_NUMERIC);
         if ($hbreaks[0] == 0) {
             // don't use first break if it's 0
             array_shift($hbreaks);
         }
         $record = 0x1b;
         // Record identifier
         $cbrk = count($hbreaks);
         // Number of page breaks
         if ($this->_BIFF_version == 0x600) {
             $length = 2 + 6 * $cbrk;
             // Bytes to follow
         } else {
             $length = 2 + 2 * $cbrk;
             // Bytes to follow
         }
         $header = pack("vv", $record, $length);
         $data = pack("v", $cbrk);
         // Append each page break
         foreach ($hbreaks as $hbreak) {
             if ($this->_BIFF_version == 0x600) {
                 $data .= pack("vvv", $hbreak, 0x0, 0xff);
             } else {
                 $data .= pack("v", $hbreak);
             }
         }
         $this->_append($header . $data);
     }
     // vertical page breaks
     if (count($vbreaks) > 0) {
         // 1000 vertical pagebreaks appears to be an internal Excel 5 limit.
         // It is slightly higher in Excel 97/200, approx. 1026
         $vbreaks = array_slice($vbreaks, 0, 1000);
         // Sort and filter array of page breaks
         sort($vbreaks, SORT_NUMERIC);
         if ($vbreaks[0] == 0) {
             // don't use first break if it's 0
             array_shift($vbreaks);
         }
         $record = 0x1a;
         // Record identifier
         $cbrk = count($vbreaks);
         // Number of page breaks
         if ($this->_BIFF_version == 0x600) {
             $length = 2 + 6 * $cbrk;
             // Bytes to follow
         } else {
             $length = 2 + 2 * $cbrk;
             // Bytes to follow
         }
         $header = pack("vv", $record, $length);
         $data = pack("v", $cbrk);
         // Append each page break
         foreach ($vbreaks as $vbreak) {
             if ($this->_BIFF_version == 0x600) {
                 $data .= pack("vvv", $vbreak, 0x0, 0xffff);
             } else {
                 $data .= pack("v", $vbreak);
             }
         }
         $this->_append($header . $data);
     }
 }
Пример #4
0
 /**
  * Accepts a range, returning it as a range that falls within the current highest row and column of the worksheet
  *
  * @param 	string 	$range
  * @return 	string	Adjusted range value
  */
 public function shrinkRangeToFit($range)
 {
     $maxCol = $this->getHighestColumn();
     $maxRow = $this->getHighestRow();
     $maxCol = Cell::columnIndexFromString($maxCol);
     $rangeBlocks = explode(' ', $range);
     foreach ($rangeBlocks as &$rangeSet) {
         $rangeBoundaries = Cell::getRangeBoundaries($rangeSet);
         if (Cell::columnIndexFromString($rangeBoundaries[0][0]) > $maxCol) {
             $rangeBoundaries[0][0] = Cell::stringFromColumnIndex($maxCol);
         }
         if ($rangeBoundaries[0][1] > $maxRow) {
             $rangeBoundaries[0][1] = $maxRow;
         }
         if (Cell::columnIndexFromString($rangeBoundaries[1][0]) > $maxCol) {
             $rangeBoundaries[1][0] = Cell::stringFromColumnIndex($maxCol);
         }
         if ($rangeBoundaries[1][1] > $maxRow) {
             $rangeBoundaries[1][1] = $maxRow;
         }
         $rangeSet = $rangeBoundaries[0][0] . $rangeBoundaries[0][1] . ':' . $rangeBoundaries[1][0] . $rangeBoundaries[1][1];
     }
     unset($rangeSet);
     $stRange = implode(' ', $rangeBlocks);
     return $stRange;
 }
Пример #5
0
 /**
  * Process the object to be written
  */
 public function close()
 {
     // initialize
     $this->_data = '';
     switch (get_class($this->_object)) {
         case 'Shared_Escher':
             if ($dggContainer = $this->_object->getDggContainer()) {
                 $writer = new Writer_Excel5_Escher($dggContainer);
                 $this->_data = $writer->close();
             } else {
                 if ($dgContainer = $this->_object->getDgContainer()) {
                     $writer = new Writer_Excel5_Escher($dgContainer);
                     $this->_data = $writer->close();
                     $this->_spOffsets = $writer->getSpOffsets();
                 }
             }
             break;
         case 'Shared_Escher_DggContainer':
             // this is a container record
             // initialize
             $innerData = '';
             // write the dgg
             $recVer = 0x0;
             $recInstance = 0x0;
             $recType = 0xf006;
             $recVerInstance = $recVer;
             $recVerInstance |= $recInstance << 4;
             // dgg data
             $dggData = pack('VVVV', $this->_object->getSpIdMax(), $this->_object->getCDgSaved() + 1, $this->_object->getCSpSaved(), $this->_object->getCDgSaved());
             // add file identifier clusters (one per drawing)
             for ($i = 0; $i < $this->_object->getCDgSaved(); ++$i) {
                 $dggData .= pack('VV', 0, 0);
             }
             $header = pack('vvV', $recVerInstance, $recType, strlen($dggData));
             $innerData .= $header . $dggData;
             // write the bstoreContainer
             if ($bstoreContainer = $this->_object->getBstoreContainer()) {
                 $writer = new Writer_Excel5_Escher($bstoreContainer);
                 $innerData .= $writer->close();
             }
             // write the record
             $recVer = 0xf;
             $recInstance = 0x0;
             $recType = 0xf000;
             $length = strlen($innerData);
             $recVerInstance = $recVer;
             $recVerInstance |= $recInstance << 4;
             $header = pack('vvV', $recVerInstance, $recType, $length);
             $this->_data = $header . $innerData;
             break;
         case 'Shared_Escher_DggContainer_BstoreContainer':
             // this is a container record
             // initialize
             $innerData = '';
             // treat the inner data
             if ($BSECollection = $this->_object->getBSECollection()) {
                 foreach ($BSECollection as $BSE) {
                     $writer = new Writer_Excel5_Escher($BSE);
                     $innerData .= $writer->close();
                 }
             }
             // write the record
             $recVer = 0xf;
             $recInstance = count($this->_object->getBSECollection());
             $recType = 0xf001;
             $length = strlen($innerData);
             $recVerInstance = $recVer;
             $recVerInstance |= $recInstance << 4;
             $header = pack('vvV', $recVerInstance, $recType, $length);
             $this->_data = $header . $innerData;
             break;
         case 'Shared_Escher_DggContainer_BstoreContainer_BSE':
             // this is a semi-container record
             // initialize
             $innerData = '';
             // here we treat the inner data
             if ($blip = $this->_object->getBlip()) {
                 $writer = new Writer_Excel5_Escher($blip);
                 $innerData .= $writer->close();
             }
             // initialize
             $data = '';
             $btWin32 = $this->_object->getBlipType();
             $btMacOS = $this->_object->getBlipType();
             $data .= pack('CC', $btWin32, $btMacOS);
             $rgbUid = pack('VVVV', 0, 0, 0, 0);
             // todo
             $data .= $rgbUid;
             $tag = 0;
             $size = strlen($innerData);
             $cRef = 1;
             $foDelay = 0;
             //todo
             $unused1 = 0x0;
             $cbName = 0x0;
             $unused2 = 0x0;
             $unused3 = 0x0;
             $data .= pack('vVVVCCCC', $tag, $size, $cRef, $foDelay, $unused1, $cbName, $unused2, $unused3);
             $data .= $innerData;
             // write the record
             $recVer = 0x2;
             $recInstance = $this->_object->getBlipType();
             $recType = 0xf007;
             $length = strlen($data);
             $recVerInstance = $recVer;
             $recVerInstance |= $recInstance << 4;
             $header = pack('vvV', $recVerInstance, $recType, $length);
             $this->_data = $header;
             $this->_data .= $data;
             break;
         case 'Shared_Escher_DggContainer_BstoreContainer_BSE_Blip':
             // this is an atom record
             // write the record
             switch ($this->_object->getParent()->getBlipType()) {
                 case Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG:
                     // initialize
                     $innerData = '';
                     $rgbUid1 = pack('VVVV', 0, 0, 0, 0);
                     // todo
                     $innerData .= $rgbUid1;
                     $tag = 0xff;
                     // todo
                     $innerData .= pack('C', $tag);
                     $innerData .= $this->_object->getData();
                     $recVer = 0x0;
                     $recInstance = 0x46a;
                     $recType = 0xf01d;
                     $length = strlen($innerData);
                     $recVerInstance = $recVer;
                     $recVerInstance |= $recInstance << 4;
                     $header = pack('vvV', $recVerInstance, $recType, $length);
                     $this->_data = $header;
                     $this->_data .= $innerData;
                     break;
                 case Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG:
                     // initialize
                     $innerData = '';
                     $rgbUid1 = pack('VVVV', 0, 0, 0, 0);
                     // todo
                     $innerData .= $rgbUid1;
                     $tag = 0xff;
                     // todo
                     $innerData .= pack('C', $tag);
                     $innerData .= $this->_object->getData();
                     $recVer = 0x0;
                     $recInstance = 0x6e0;
                     $recType = 0xf01e;
                     $length = strlen($innerData);
                     $recVerInstance = $recVer;
                     $recVerInstance |= $recInstance << 4;
                     $header = pack('vvV', $recVerInstance, $recType, $length);
                     $this->_data = $header;
                     $this->_data .= $innerData;
                     break;
             }
             break;
         case 'Shared_Escher_DgContainer':
             // this is a container record
             // initialize
             $innerData = '';
             // write the dg
             $recVer = 0x0;
             $recInstance = $this->_object->getDgId();
             $recType = 0xf008;
             $length = 8;
             $recVerInstance = $recVer;
             $recVerInstance |= $recInstance << 4;
             $header = pack('vvV', $recVerInstance, $recType, $length);
             // number of shapes in this drawing (including group shape)
             $countShapes = count($this->_object->getSpgrContainer()->getChildren());
             $innerData .= $header . pack('VV', $countShapes, $this->_object->getLastSpId());
             //$innerData .= $header . pack('VV', 0, 0);
             // write the spgrContainer
             if ($spgrContainer = $this->_object->getSpgrContainer()) {
                 $writer = new Writer_Excel5_Escher($spgrContainer);
                 $innerData .= $writer->close();
                 // get the shape offsets relative to the spgrContainer record
                 $spOffsets = $writer->getSpOffsets();
                 // save the shape offsets relative to dgContainer
                 foreach ($spOffsets as &$spOffset) {
                     $spOffset += 24;
                     // add length of dgContainer header data (8 bytes) plus dg data (16 bytes)
                 }
                 $this->_spOffsets = $spOffsets;
             }
             // write the record
             $recVer = 0xf;
             $recInstance = 0x0;
             $recType = 0xf002;
             $length = strlen($innerData);
             $recVerInstance = $recVer;
             $recVerInstance |= $recInstance << 4;
             $header = pack('vvV', $recVerInstance, $recType, $length);
             $this->_data = $header . $innerData;
             break;
         case 'Shared_Escher_DgContainer_SpgrContainer':
             // this is a container record
             // initialize
             $innerData = '';
             // initialize spape offsets
             $totalSize = 8;
             $spOffsets = array();
             // treat the inner data
             foreach ($this->_object->getChildren() as $spContainer) {
                 $writer = new Writer_Excel5_Escher($spContainer);
                 $spData = $writer->close();
                 $innerData .= $spData;
                 // save the shape offsets (where new shape records begin)
                 $totalSize += strlen($spData);
                 $spOffsets[] = $totalSize;
             }
             // write the record
             $recVer = 0xf;
             $recInstance = 0x0;
             $recType = 0xf003;
             $length = strlen($innerData);
             $recVerInstance = $recVer;
             $recVerInstance |= $recInstance << 4;
             $header = pack('vvV', $recVerInstance, $recType, $length);
             $this->_data = $header . $innerData;
             $this->_spOffsets = $spOffsets;
             break;
         case 'Shared_Escher_DgContainer_SpgrContainer_SpContainer':
             // initialize
             $data = '';
             // build the data
             // write group shape record, if necessary?
             if ($this->_object->getSpgr()) {
                 $recVer = 0x1;
                 $recInstance = 0x0;
                 $recType = 0xf009;
                 $length = 0x10;
                 $recVerInstance = $recVer;
                 $recVerInstance |= $recInstance << 4;
                 $header = pack('vvV', $recVerInstance, $recType, $length);
                 $data .= $header . pack('VVVV', 0, 0, 0, 0);
             }
             // write the shape record
             $recVer = 0x2;
             $recInstance = $this->_object->getSpType();
             // shape type
             $recType = 0xf00a;
             $length = 0x8;
             $recVerInstance = $recVer;
             $recVerInstance |= $recInstance << 4;
             $header = pack('vvV', $recVerInstance, $recType, $length);
             $data .= $header . pack('VV', $this->_object->getSpId(), $this->_object->getSpgr() ? 0x5 : 0xa00);
             // the options
             if ($this->_object->getOPTCollection()) {
                 $optData = '';
                 $recVer = 0x3;
                 $recInstance = count($this->_object->getOPTCollection());
                 $recType = 0xf00b;
                 foreach ($this->_object->getOPTCollection() as $property => $value) {
                     $optData .= pack('vV', $property, $value);
                 }
                 $length = strlen($optData);
                 $recVerInstance = $recVer;
                 $recVerInstance |= $recInstance << 4;
                 $header = pack('vvV', $recVerInstance, $recType, $length);
                 $data .= $header . $optData;
             }
             // the client anchor
             if ($this->_object->getStartCoordinates()) {
                 $clientAnchorData = '';
                 $recVer = 0x0;
                 $recInstance = 0x0;
                 $recType = 0xf010;
                 // start coordinates
                 list($column, $row) = Cell::coordinateFromString($this->_object->getStartCoordinates());
                 $c1 = Cell::columnIndexFromString($column) - 1;
                 $r1 = $row - 1;
                 // start offsetX
                 $startOffsetX = $this->_object->getStartOffsetX();
                 // start offsetY
                 $startOffsetY = $this->_object->getStartOffsetY();
                 // end coordinates
                 list($column, $row) = Cell::coordinateFromString($this->_object->getEndCoordinates());
                 $c2 = Cell::columnIndexFromString($column) - 1;
                 $r2 = $row - 1;
                 // end offsetX
                 $endOffsetX = $this->_object->getEndOffsetX();
                 // end offsetY
                 $endOffsetY = $this->_object->getEndOffsetY();
                 $clientAnchorData = pack('vvvvvvvvv', 0x2, $c1, $startOffsetX, $r1, $startOffsetY, $c2, $endOffsetX, $r2, $endOffsetY);
                 $length = strlen($clientAnchorData);
                 $recVerInstance = $recVer;
                 $recVerInstance |= $recInstance << 4;
                 $header = pack('vvV', $recVerInstance, $recType, $length);
                 $data .= $header . $clientAnchorData;
             }
             // the client data, just empty for now
             if (!$this->_object->getSpgr()) {
                 $clientDataData = '';
                 $recVer = 0x0;
                 $recInstance = 0x0;
                 $recType = 0xf011;
                 $length = strlen($clientDataData);
                 $recVerInstance = $recVer;
                 $recVerInstance |= $recInstance << 4;
                 $header = pack('vvV', $recVerInstance, $recType, $length);
                 $data .= $header . $clientDataData;
             }
             // write the record
             $recVer = 0xf;
             $recInstance = 0x0;
             $recType = 0xf004;
             $length = strlen($data);
             $recVerInstance = $recVer;
             $recVerInstance |= $recInstance << 4;
             $header = pack('vvV', $recVerInstance, $recType, $length);
             $this->_data = $header . $data;
             break;
     }
     return $this->_data;
 }
Пример #6
0
 private function _castToFormula($c, $r, &$cellDataType, &$value, &$calculatedValue, &$sharedFormulas, $castBaseType)
 {
     //		echo '<font color="darkgreen">Formula</font><br />';
     //		echo '$c->f is '.$c->f.'<br />';
     $cellDataType = 'f';
     $value = "={$c->f}";
     $calculatedValue = $this->{$castBaseType}($c);
     // Shared formula?
     if (isset($c->f['t']) && strtolower((string) $c->f['t']) == 'shared') {
         //			echo '<font color="darkgreen">SHARED FORMULA</font><br />';
         $instance = (string) $c->f['si'];
         //			echo 'Instance ID = '.$instance.'<br />';
         //
         //			echo 'Shared Formula Array:<pre>';
         //			print_r($sharedFormulas);
         //			echo '</pre>';
         if (!isset($sharedFormulas[(string) $c->f['si']])) {
             //				echo '<font color="darkgreen">SETTING NEW SHARED FORMULA</font><br />';
             //				echo 'Master is '.$r.'<br />';
             //				echo 'Formula is '.$value.'<br />';
             $sharedFormulas[$instance] = array('master' => $r, 'formula' => $value);
             //				echo 'New Shared Formula Array:<pre>';
             //				print_r($sharedFormulas);
             //				echo '</pre>';
         } else {
             //				echo '<font color="darkgreen">GETTING SHARED FORMULA</font><br />';
             //				echo 'Master is '.$sharedFormulas[$instance]['master'].'<br />';
             //				echo 'Formula is '.$sharedFormulas[$instance]['formula'].'<br />';
             $master = Cell::coordinateFromString($sharedFormulas[$instance]['master']);
             $current = Cell::coordinateFromString($r);
             $difference = array(0, 0);
             $difference[0] = Cell::columnIndexFromString($current[0]) - Cell::columnIndexFromString($master[0]);
             $difference[1] = $current[1] - $master[1];
             $helper = ReferenceHelper::getInstance();
             $value = $helper->updateFormulaReferences($sharedFormulas[$instance]['formula'], 'A1', $difference[0], $difference[1]);
             //				echo 'Adjusted Formula is '.$value.'<br />';
         }
     }
 }
Пример #7
0
 /**
  * Write SheetData
  *
  * @param	Shared_XMLWriter		$objWriter		XML Writer
  * @param	Worksheet				$pSheet			Worksheet
  * @param	string[]						$pStringTable	String table
  * @throws	Exception
  */
 private function _writeSheetData(Shared_XMLWriter $objWriter = null, Worksheet $pSheet = null, $pStringTable = null)
 {
     if (is_array($pStringTable)) {
         // Flipped stringtable, for faster index searching
         $aFlippedStringTable = $this->getParentWriter()->getWriterPart('stringtable')->flipStringTable($pStringTable);
         // sheetData
         $objWriter->startElement('sheetData');
         // Get column count
         $colCount = Cell::columnIndexFromString($pSheet->getHighestColumn());
         // Highest row number
         $highestRow = $pSheet->getHighestRow();
         // Loop through cells
         $cellsByRow = array();
         foreach ($pSheet->getCellCollection() as $cellID) {
             $cellAddress = Cell::coordinateFromString($cellID);
             $cellsByRow[$cellAddress[1]][] = $cellID;
         }
         for ($currentRow = 1; $currentRow <= $highestRow; ++$currentRow) {
             // Get row dimension
             $rowDimension = $pSheet->getRowDimension($currentRow);
             // Write current row?
             $writeCurrentRow = isset($cellsByRow[$currentRow]) || $rowDimension->getRowHeight() >= 0 || $rowDimension->getVisible() == false || $rowDimension->getCollapsed() == true || $rowDimension->getOutlineLevel() > 0 || $rowDimension->getXfIndex() !== null;
             if ($writeCurrentRow) {
                 // Start a new row
                 $objWriter->startElement('row');
                 $objWriter->writeAttribute('r', $currentRow);
                 $objWriter->writeAttribute('spans', '1:' . $colCount);
                 // Row dimensions
                 if ($rowDimension->getRowHeight() >= 0) {
                     $objWriter->writeAttribute('customHeight', '1');
                     $objWriter->writeAttribute('ht', Shared_String::FormatNumber($rowDimension->getRowHeight()));
                 }
                 // Row visibility
                 if ($rowDimension->getVisible() == false) {
                     $objWriter->writeAttribute('hidden', 'true');
                 }
                 // Collapsed
                 if ($rowDimension->getCollapsed() == true) {
                     $objWriter->writeAttribute('collapsed', 'true');
                 }
                 // Outline level
                 if ($rowDimension->getOutlineLevel() > 0) {
                     $objWriter->writeAttribute('outlineLevel', $rowDimension->getOutlineLevel());
                 }
                 // Style
                 if ($rowDimension->getXfIndex() !== null) {
                     $objWriter->writeAttribute('s', $rowDimension->getXfIndex());
                     $objWriter->writeAttribute('customFormat', '1');
                 }
                 // Write cells
                 if (isset($cellsByRow[$currentRow])) {
                     foreach ($cellsByRow[$currentRow] as $cellAddress) {
                         // Write cell
                         $this->_writeCell($objWriter, $pSheet, $cellAddress, $pStringTable, $aFlippedStringTable);
                     }
                 }
                 // End row
                 $objWriter->endElement();
             }
         }
         $objWriter->endElement();
     } else {
         throw new Exception("Invalid parameters passed.");
     }
 }
Пример #8
0
 /**
  * Convert 1-cell anchor coordinates to 2-cell anchor coordinates
  * This function is ported from PEAR Spreadsheet_Writer_Excel with small modifications
  *
  * Calculate the vertices that define the position of the image as required by
  * the OBJ record.
  *
  *		 +------------+------------+
  *		 |	 A	  |	  B	 |
  *   +-----+------------+------------+
  *   |	 |(x1,y1)	 |			|
  *   |  1  |(A1)._______|______	  |
  *   |	 |	|			  |	 |
  *   |	 |	|			  |	 |
  *   +-----+----|	BITMAP	|-----+
  *   |	 |	|			  |	 |
  *   |  2  |	|______________.	 |
  *   |	 |			|		(B2)|
  *   |	 |			|	 (x2,y2)|
  *   +---- +------------+------------+
  *
  * Example of a bitmap that covers some of the area from cell A1 to cell B2.
  *
  * Based on the width and height of the bitmap we need to calculate 8 vars:
  *	 $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
  * The width and height of the cells are also variable and have to be taken into
  * account.
  * The values of $col_start and $row_start are passed in from the calling
  * function. The values of $col_end and $row_end are calculated by subtracting
  * the width and height of the bitmap from the width and height of the
  * underlying cells.
  * The vertices are expressed as a percentage of the underlying cell width as
  * follows (rhs values are in pixels):
  *
  *	   x1 = X / W *1024
  *	   y1 = Y / H *256
  *	   x2 = (X-1) / W *1024
  *	   y2 = (Y-1) / H *256
  *
  *	   Where:  X is distance from the left side of the underlying cell
  *			   Y is distance from the top of the underlying cell
  *			   W is the width of the cell
  *			   H is the height of the cell
  *
  * @param Worksheet $sheet
  * @param string $coordinates E.g. 'A1'
  * @param integer $offsetX Horizontal offset in pixels
  * @param integer $offsetY Vertical offset in pixels
  * @param integer $width Width in pixels
  * @param integer $height Height in pixels
  * @return array
  */
 public static function oneAnchor2twoAnchor($sheet, $coordinates, $offsetX, $offsetY, $width, $height)
 {
     list($column, $row) = Cell::coordinateFromString($coordinates);
     $col_start = Cell::columnIndexFromString($column) - 1;
     $row_start = $row - 1;
     $x1 = $offsetX;
     $y1 = $offsetY;
     // Initialise end cell to the same as the start cell
     $col_end = $col_start;
     // Col containing lower right corner of object
     $row_end = $row_start;
     // Row containing bottom right corner of object
     // Zero the specified offset if greater than the cell dimensions
     if ($x1 >= self::sizeCol($sheet, Cell::stringFromColumnIndex($col_start))) {
         $x1 = 0;
     }
     if ($y1 >= self::sizeRow($sheet, $row_start + 1)) {
         $y1 = 0;
     }
     $width = $width + $x1 - 1;
     $height = $height + $y1 - 1;
     // Subtract the underlying cell widths to find the end cell of the image
     while ($width >= self::sizeCol($sheet, Cell::stringFromColumnIndex($col_end))) {
         $width -= self::sizeCol($sheet, Cell::stringFromColumnIndex($col_end));
         ++$col_end;
     }
     // Subtract the underlying cell heights to find the end cell of the image
     while ($height >= self::sizeRow($sheet, $row_end + 1)) {
         $height -= self::sizeRow($sheet, $row_end + 1);
         ++$row_end;
     }
     // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
     // with zero height or width.
     if (self::sizeCol($sheet, Cell::stringFromColumnIndex($col_start)) == 0) {
         return;
     }
     if (self::sizeCol($sheet, Cell::stringFromColumnIndex($col_end)) == 0) {
         return;
     }
     if (self::sizeRow($sheet, $row_start + 1) == 0) {
         return;
     }
     if (self::sizeRow($sheet, $row_end + 1) == 0) {
         return;
     }
     // Convert the pixel values to the percentage value expected by Excel
     $x1 = $x1 / self::sizeCol($sheet, Cell::stringFromColumnIndex($col_start)) * 1024;
     $y1 = $y1 / self::sizeRow($sheet, $row_start + 1) * 256;
     $x2 = ($width + 1) / self::sizeCol($sheet, Cell::stringFromColumnIndex($col_end)) * 1024;
     // Distance to right side of object
     $y2 = ($height + 1) / self::sizeRow($sheet, $row_end + 1) * 256;
     // Distance to bottom of object
     $startCoordinates = Cell::stringFromColumnIndex($col_start) . ($row_start + 1);
     $endCoordinates = Cell::stringFromColumnIndex($col_end) . ($row_end + 1);
     $twoAnchor = array('startCoordinates' => $startCoordinates, 'startOffsetX' => $x1, 'startOffsetY' => $y1, 'endCoordinates' => $endCoordinates, 'endOffsetX' => $x2, 'endOffsetY' => $y2);
     return $twoAnchor;
 }
Пример #9
0
 /**
  * More Cell instances available?
  *
  * @return boolean
  */
 public function valid()
 {
     // columnIndexFromString() returns an index based at one,
     // treat it as a count when comparing it to the base zero
     // position.
     $columnCount = Cell::columnIndexFromString($this->_subject->getHighestColumn());
     if ($this->_onlyExistingCells) {
         // If we aren't looking at an existing cell, either
         // because the first column doesn't exist or next() has
         // been called onto a nonexistent cell, then loop until we
         // find one, or pass the last column.
         while ($this->_position < $columnCount && !$this->_subject->cellExistsByColumnAndRow($this->_position, $this->_rowIndex)) {
             ++$this->_position;
         }
     }
     return $this->_position < $columnCount;
 }
Пример #10
0
 /**
  * Calculate information about HTML colspan and rowspan which is not always the same as Excel's
  */
 private function _calculateSpans()
 {
     // Identify all cells that should be omitted in HTML due to cell merge.
     // In HTML only the upper-left cell should be written and it should have
     //   appropriate rowspan / colspan attribute
     $sheetIndexes = $this->_sheetIndex !== null ? array($this->_sheetIndex) : range(0, $this->_phpExcel->getSheetCount() - 1);
     foreach ($sheetIndexes as $sheetIndex) {
         $sheet = $this->_phpExcel->getSheet($sheetIndex);
         $candidateSpannedRow = array();
         // loop through all Excel merged cells
         foreach ($sheet->getMergeCells() as $cells) {
             list($cells, ) = Cell::splitRange($cells);
             $first = $cells[0];
             $last = $cells[1];
             list($fc, $fr) = Cell::coordinateFromString($first);
             $fc = Cell::columnIndexFromString($fc) - 1;
             list($lc, $lr) = Cell::coordinateFromString($last);
             $lc = Cell::columnIndexFromString($lc) - 1;
             // loop through the individual cells in the individual merge
             for ($r = $fr; $r <= $lr; ++$r) {
                 // also, flag this row as a HTML row that is candidate to be omitted
                 $candidateSpannedRow[$r] = $r;
                 for ($c = $fc; $c <= $lc; ++$c) {
                     if (!($c == $fc && $r == $fr)) {
                         // not the upper-left cell (should not be written in HTML)
                         $this->_isSpannedCell[$sheetIndex][$r][$c] = array('baseCell' => array($fr, $fc));
                     } else {
                         // upper-left is the base cell that should hold the colspan/rowspan attribute
                         $this->_isBaseCell[$sheetIndex][$r][$c] = array('xlrowspan' => $lr - $fr + 1, 'rowspan' => $lr - $fr + 1, 'xlcolspan' => $lc - $fc + 1, 'colspan' => $lc - $fc + 1);
                     }
                 }
             }
         }
         // Identify which rows should be omitted in HTML. These are the rows where all the cells
         //   participate in a merge and the where base cells are somewhere above.
         $countColumns = Cell::columnIndexFromString($sheet->getHighestColumn());
         foreach ($candidateSpannedRow as $rowIndex) {
             if (isset($this->_isSpannedCell[$sheetIndex][$rowIndex])) {
                 if (count($this->_isSpannedCell[$sheetIndex][$rowIndex]) == $countColumns) {
                     $this->_isSpannedRow[$sheetIndex][$rowIndex] = $rowIndex;
                 }
             }
         }
         // For each of the omitted rows we found above, the affected rowspans should be subtracted by 1
         if (isset($this->_isSpannedRow[$sheetIndex])) {
             foreach ($this->_isSpannedRow[$sheetIndex] as $rowIndex) {
                 $adjustedBaseCells = array();
                 for ($c = 0; $c < $countColumns; ++$c) {
                     $baseCell = $this->_isSpannedCell[$sheetIndex][$rowIndex][$c]['baseCell'];
                     if (!in_array($baseCell, $adjustedBaseCells)) {
                         // subtract rowspan by 1
                         --$this->_isBaseCell[$sheetIndex][$baseCell[0]][$baseCell[1]]['rowspan'];
                         $adjustedBaseCells[] = $baseCell;
                     }
                 }
             }
         }
         // TODO: Same for columns
     }
     // We have calculated the spans
     $this->_spansAreCalculated = true;
 }
Пример #11
0
 function setExcelSchedule($file)
 {
     $this->load->library('PHPExcel');
     $this->load->library('PHPExcel/IOFactory');
     if (substr(strrchr($file['orig_name'], '.'), 1) == 'xlsx') {
         $excel = IOFactory::load($file['full_path']);
         $num = 0;
         foreach ($excel->getWorksheetIterator() as $worksheet) {
             if ($num == 0) {
                 // first worksheet only
                 $letterColumn = $worksheet->getHighestColumn();
                 $numColumn = Cell::columnIndexFromString($letterColumn);
                 $numRow = $worksheet->getHighestRow();
                 $classes = array();
                 for ($row = 2; $row <= $numRow; $row++) {
                     //start at second row
                     $rowData = array();
                     for ($col = 0; $col < $numColumn; $col++) {
                         $cell = $worksheet->getCellByColumnAndRow($col, $row);
                         $val = $cell->getValue();
                         array_push($rowData, $val);
                     }
                     array_push($classes, $rowData);
                 }
             }
             $num++;
         }
         $post = $this->input->post();
         $schedule = array('year' => $post['year'], 'semester' => $post['semester'], 'classes' => $classes);
         $this->setSchedule($schedule);
     }
 }
Пример #12
0
 /**
  *	OFFSET
  *
  *	Returns a reference to a range that is a specified number of rows and columns from a cell or range of cells.
  *	The reference that is returned can be a single cell or a range of cells. You can specify the number of rows and
  *	the number of columns to be returned.
  *
  *	@param	cellAddress		The reference from which you want to base the offset. Reference must refer to a cell or
  *								range of adjacent cells; otherwise, OFFSET returns the #VALUE! error value.
  *	@param	rows			The number of rows, up or down, that you want the upper-left cell to refer to.
  *								Using 5 as the rows argument specifies that the upper-left cell in the reference is
  *								five rows below reference. Rows can be positive (which means below the starting reference)
  *								or negative (which means above the starting reference).
  *	@param	cols			The number of columns, to the left or right, that you want the upper-left cell of the result
  *								to refer to. Using 5 as the cols argument specifies that the upper-left cell in the
  *								reference is five columns to the right of reference. Cols can be positive (which means
  *								to the right of the starting reference) or negative (which means to the left of the
  *								starting reference).
  *	@param	height			The height, in number of rows, that you want the returned reference to be. Height must be a positive number.
  *	@param	width			The width, in number of columns, that you want the returned reference to be. Width must be a positive number.
  *	@return	string			A reference to a cell or range of cells
  */
 public static function OFFSET($cellAddress = Null, $rows = 0, $columns = 0, $height = null, $width = null)
 {
     $rows = self::flattenSingleValue($rows);
     $columns = self::flattenSingleValue($columns);
     $height = self::flattenSingleValue($height);
     $width = self::flattenSingleValue($width);
     if ($cellAddress == Null) {
         return 0;
     }
     $args = func_get_args();
     $pCell = array_pop($args);
     if (!is_object($pCell)) {
         return self::$_errorCodes['reference'];
     }
     $sheetName = null;
     if (strpos($cellAddress, "!")) {
         list($sheetName, $cellAddress) = explode("!", $cellAddress);
     }
     if (strpos($cellAddress, ":")) {
         list($startCell, $endCell) = explode(":", $cellAddress);
     } else {
         $startCell = $endCell = $cellAddress;
     }
     list($startCellColumn, $startCellRow) = Cell::coordinateFromString($startCell);
     list($endCellColumn, $endCellRow) = Cell::coordinateFromString($endCell);
     $startCellRow += $rows;
     $startCellColumn = Cell::columnIndexFromString($startCellColumn) - 1;
     $startCellColumn += $columns;
     if ($startCellRow <= 0 || $startCellColumn < 0) {
         return self::$_errorCodes['reference'];
     }
     $endCellColumn = Cell::columnIndexFromString($endCellColumn) - 1;
     if ($width != null && !is_object($width)) {
         $endCellColumn = $startCellColumn + $width - 1;
     } else {
         $endCellColumn += $columns;
     }
     $startCellColumn = Cell::stringFromColumnIndex($startCellColumn);
     if ($height != null && !is_object($height)) {
         $endCellRow = $startCellRow + $height - 1;
     } else {
         $endCellRow += $rows;
     }
     if ($endCellRow <= 0 || $endCellColumn < 0) {
         return self::$_errorCodes['reference'];
     }
     $endCellColumn = Cell::stringFromColumnIndex($endCellColumn);
     $cellAddress = $startCellColumn . $startCellRow;
     if ($startCellColumn != $endCellColumn || $startCellRow != $endCellRow) {
         $cellAddress .= ':' . $endCellColumn . $endCellRow;
     }
     if ($sheetName !== null) {
         $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName);
     } else {
         $pSheet = $pCell->getParent();
     }
     return Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, False);
 }
Пример #13
0
 /**
  * Loads PHPExcel from file into PHPExcel instance
  *
  * @param 	string 		$pFilename
  * @param	PHPExcel	$objPHPExcel
  * @return 	PHPExcel
  * @throws 	Exception
  */
 public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
 {
     $fromFormats = array('\\-', '\\ ');
     $toFormats = array('-', ' ');
     $underlineStyles = array(Style_Font::UNDERLINE_NONE, Style_Font::UNDERLINE_DOUBLE, Style_Font::UNDERLINE_DOUBLEACCOUNTING, Style_Font::UNDERLINE_SINGLE, Style_Font::UNDERLINE_SINGLEACCOUNTING);
     $verticalAlignmentStyles = array(Style_Alignment::VERTICAL_BOTTOM, Style_Alignment::VERTICAL_TOP, Style_Alignment::VERTICAL_CENTER, Style_Alignment::VERTICAL_JUSTIFY);
     $horizontalAlignmentStyles = array(Style_Alignment::HORIZONTAL_GENERAL, Style_Alignment::HORIZONTAL_LEFT, Style_Alignment::HORIZONTAL_RIGHT, Style_Alignment::HORIZONTAL_CENTER, Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS, Style_Alignment::HORIZONTAL_JUSTIFY);
     // Check if file exists
     if (!file_exists($pFilename)) {
         throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
     }
     $xml = simplexml_load_file($pFilename);
     $namespaces = $xml->getNamespaces(true);
     //		echo '<pre>';
     //		print_r($namespaces);
     //		echo '</pre><hr />';
     //
     //		echo '<pre>';
     //		print_r($xml);
     //		echo '</pre><hr />';
     //
     $docProps = $objPHPExcel->getProperties();
     foreach ($xml->DocumentProperties[0] as $propertyName => $propertyValue) {
         switch ($propertyName) {
             case 'Title':
                 $docProps->setTitle($propertyValue);
                 break;
             case 'Subject':
                 $docProps->setSubject($propertyValue);
                 break;
             case 'Author':
                 $docProps->setCreator($propertyValue);
                 break;
             case 'Created':
                 $creationDate = strtotime($propertyValue);
                 $docProps->setCreated($creationDate);
                 break;
             case 'LastAuthor':
                 $docProps->setLastModifiedBy($propertyValue);
                 break;
             case 'Company':
                 $docProps->setCompany($propertyValue);
                 break;
             case 'Category':
                 $docProps->setCategory($propertyValue);
                 break;
             case 'Keywords':
                 $docProps->setKeywords($propertyValue);
                 break;
             case 'Description':
                 $docProps->setDescription($propertyValue);
                 break;
         }
     }
     foreach ($xml->Styles[0] as $style) {
         $style_ss = $style->attributes($namespaces['ss']);
         $styleID = (string) $style_ss['ID'];
         //			echo 'Style ID = '.$styleID.'<br />';
         if ($styleID == 'Default') {
             $this->_styles['Default'] = array();
         } else {
             $this->_styles[$styleID] = $this->_styles['Default'];
         }
         foreach ($style as $styleType => $styleData) {
             $styleAttributes = $styleData->attributes($namespaces['ss']);
             //				echo $styleType.'<br />';
             switch ($styleType) {
                 case 'Alignment':
                     foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
                         //								echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
                         $styleAttributeValue = (string) $styleAttributeValue;
                         switch ($styleAttributeKey) {
                             case 'Vertical':
                                 if (self::identifyFixedStyleValue($verticalAlignmentStyles, $styleAttributeValue)) {
                                     $this->_styles[$styleID]['alignment']['vertical'] = $styleAttributeValue;
                                 }
                                 break;
                             case 'Horizontal':
                                 if (self::identifyFixedStyleValue($horizontalAlignmentStyles, $styleAttributeValue)) {
                                     $this->_styles[$styleID]['alignment']['horizontal'] = $styleAttributeValue;
                                 }
                                 break;
                             case 'WrapText':
                                 $this->_styles[$styleID]['alignment']['wrap'] = true;
                                 break;
                         }
                     }
                     break;
                 case 'Borders':
                     foreach ($styleData->Border as $borderStyle) {
                         $borderAttributes = $borderStyle->attributes($namespaces['ss']);
                         $thisBorder = array();
                         foreach ($borderAttributes as $borderStyleKey => $borderStyleValue) {
                             //									echo $borderStyleKey.' = '.$borderStyleValue.'<br />';
                             switch ($borderStyleKey) {
                                 case 'LineStyle':
                                     $thisBorder['style'] = Style_Border::BORDER_MEDIUM;
                                     //												$thisBorder['style'] = $borderStyleValue;
                                     break;
                                 case 'Weight':
                                     //												$thisBorder['style'] = $borderStyleValue;
                                     break;
                                 case 'Position':
                                     $borderPosition = strtolower($borderStyleValue);
                                     break;
                                 case 'Color':
                                     $borderColour = substr($borderStyleValue, 1);
                                     $thisBorder['color']['rgb'] = $borderColour;
                                     break;
                             }
                         }
                         if (count($thisBorder) > 0) {
                             if ($borderPosition == 'left' || $borderPosition == 'right' || $borderPosition == 'top' || $borderPosition == 'bottom') {
                                 $this->_styles[$styleID]['borders'][$borderPosition] = $thisBorder;
                             }
                         }
                     }
                     break;
                 case 'Font':
                     foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
                         //								echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
                         $styleAttributeValue = (string) $styleAttributeValue;
                         switch ($styleAttributeKey) {
                             case 'FontName':
                                 $this->_styles[$styleID]['font']['name'] = $styleAttributeValue;
                                 break;
                             case 'Size':
                                 $this->_styles[$styleID]['font']['size'] = $styleAttributeValue;
                                 break;
                             case 'Color':
                                 $this->_styles[$styleID]['font']['color']['rgb'] = substr($styleAttributeValue, 1);
                                 break;
                             case 'Bold':
                                 $this->_styles[$styleID]['font']['bold'] = true;
                                 break;
                             case 'Italic':
                                 $this->_styles[$styleID]['font']['italic'] = true;
                                 break;
                             case 'Underline':
                                 if (self::identifyFixedStyleValue($underlineStyles, $styleAttributeValue)) {
                                     $this->_styles[$styleID]['font']['underline'] = $styleAttributeValue;
                                 }
                                 break;
                         }
                     }
                     break;
                 case 'Interior':
                     foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
                         //								echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
                         switch ($styleAttributeKey) {
                             case 'Color':
                                 $this->_styles[$styleID]['fill']['color']['rgb'] = substr($styleAttributeValue, 1);
                                 break;
                         }
                     }
                     break;
                 case 'NumberFormat':
                     foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
                         //								echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
                         $styleAttributeValue = str_replace($fromFormats, $toFormats, $styleAttributeValue);
                         switch ($styleAttributeValue) {
                             case 'Short Date':
                                 $styleAttributeValue = 'dd/mm/yyyy';
                                 break;
                         }
                         if ($styleAttributeValue > '') {
                             $this->_styles[$styleID]['numberformat']['code'] = $styleAttributeValue;
                         }
                     }
                     break;
                 case 'Protection':
                     foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
                         //								echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
                     }
                     break;
             }
         }
         //			print_r($this->_styles[$styleID]);
         //			echo '<hr />';
     }
     //		echo '<hr />';
     $worksheetID = 0;
     foreach ($xml->Worksheet as $worksheet) {
         $worksheet_ss = $worksheet->attributes($namespaces['ss']);
         if (isset($this->_loadSheetsOnly) && isset($worksheet_ss['Name']) && !in_array($worksheet_ss['Name'], $this->_loadSheetsOnly)) {
             continue;
         }
         // Create new Worksheet
         $objPHPExcel->createSheet();
         $objPHPExcel->setActiveSheetIndex($worksheetID);
         if (isset($worksheet_ss['Name'])) {
             $worksheetName = (string) $worksheet_ss['Name'];
             $objPHPExcel->getActiveSheet()->setTitle($worksheetName);
         }
         $columnID = 'A';
         foreach ($worksheet->Table->Column as $columnData) {
             $columnData_ss = $columnData->attributes($namespaces['ss']);
             if (isset($columnData_ss['Index'])) {
                 $columnID = Cell::stringFromColumnIndex($columnData_ss['Index'] - 1);
             }
             if (isset($columnData_ss['Width'])) {
                 $columnWidth = $columnData_ss['Width'];
                 //					echo '<b>Setting column width for '.$columnID.' to '.$columnWidth.'</b><br />';
                 $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setWidth($columnWidth / 5.4);
             }
             ++$columnID;
         }
         $rowID = 1;
         foreach ($worksheet->Table->Row as $rowData) {
             $row_ss = $rowData->attributes($namespaces['ss']);
             if (isset($row_ss['Index'])) {
                 $rowID = (int) $row_ss['Index'];
             }
             //				echo '<b>Row '.$rowID.'</b><br />';
             if (isset($row_ss['StyleID'])) {
                 $rowStyle = $row_ss['StyleID'];
             }
             if (isset($row_ss['Height'])) {
                 $rowHeight = $row_ss['Height'];
                 //					echo '<b>Setting row height to '.$rowHeight.'</b><br />';
                 $objPHPExcel->getActiveSheet()->getRowDimension($rowID)->setRowHeight($rowHeight);
             }
             $columnID = 'A';
             foreach ($rowData->Cell as $cell) {
                 $cell_ss = $cell->attributes($namespaces['ss']);
                 if (isset($cell_ss['Index'])) {
                     $columnID = Cell::stringFromColumnIndex($cell_ss['Index'] - 1);
                 }
                 $cellRange = $columnID . $rowID;
                 if (isset($cell_ss['MergeAcross']) || isset($cell_ss['MergeDown'])) {
                     $columnTo = $columnID;
                     if (isset($cell_ss['MergeAcross'])) {
                         $columnTo = Cell::stringFromColumnIndex(Cell::columnIndexFromString($columnID) + $cell_ss['MergeAcross'] - 1);
                     }
                     $rowTo = $rowID;
                     if (isset($cell_ss['MergeDown'])) {
                         $rowTo = $rowTo + $cell_ss['MergeDown'];
                     }
                     $cellRange .= ':' . $columnTo . $rowTo;
                     $objPHPExcel->getActiveSheet()->mergeCells($cellRange);
                 }
                 $hasCalculatedValue = false;
                 $cellDataFormula = '';
                 if (isset($cell_ss['Formula'])) {
                     $cellDataFormula = $cell_ss['Formula'];
                     $hasCalculatedValue = true;
                 }
                 if (isset($cell->Data)) {
                     $cellValue = $cellData = $cell->Data;
                     $type = Cell_DataType::TYPE_NULL;
                     $cellData_ss = $cellData->attributes($namespaces['ss']);
                     if (isset($cellData_ss['Type'])) {
                         $cellDataType = $cellData_ss['Type'];
                         switch ($cellDataType) {
                             /*
                             const TYPE_STRING		= 's';
                             const TYPE_FORMULA		= 'f';
                             const TYPE_NUMERIC		= 'n';
                             const TYPE_BOOL			= 'b';
                             							    const TYPE_NULL			= 's';
                             							    const TYPE_INLINE		= 'inlineStr';
                             const TYPE_ERROR		= 'e';
                             */
                             case 'String':
                                 $type = Cell_DataType::TYPE_STRING;
                                 break;
                             case 'Number':
                                 $type = Cell_DataType::TYPE_NUMERIC;
                                 $cellValue = (double) $cellValue;
                                 if (floor($cellValue) == $cellValue) {
                                     $cellValue = (int) $cellValue;
                                 }
                                 break;
                             case 'Boolean':
                                 $type = Cell_DataType::TYPE_BOOL;
                                 $cellValue = $cellValue != 0;
                                 break;
                             case 'DateTime':
                                 $type = Cell_DataType::TYPE_NUMERIC;
                                 $cellValue = Shared_Date::PHPToExcel(strtotime($cellValue));
                                 break;
                             case 'Error':
                                 $type = Cell_DataType::TYPE_ERROR;
                                 break;
                         }
                     }
                     if ($hasCalculatedValue) {
                         $type = Cell_DataType::TYPE_FORMULA;
                         $columnNumber = Cell::columnIndexFromString($columnID);
                         //	Convert R1C1 style references to A1 style references (but only when not quoted)
                         $temp = explode('"', $cellDataFormula);
                         foreach ($temp as $key => &$value) {
                             //	Only replace in alternate array entries (i.e. non-quoted blocks)
                             if ($key % 2 == 0) {
                                 preg_match_all('/(R(\\[?-?\\d*\\]?))(C(\\[?-?\\d*\\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
                                 //	Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
                                 //		through the formula from left to right. Reversing means that we work right to left.through
                                 //		the formula
                                 $cellReferences = array_reverse($cellReferences);
                                 //	Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
                                 //		then modify the formula to use that new reference
                                 foreach ($cellReferences as $cellReference) {
                                     $rowReference = $cellReference[2][0];
                                     //	Empty R reference is the current row
                                     if ($rowReference == '') {
                                         $rowReference = $rowID;
                                     }
                                     //	Bracketed R references are relative to the current row
                                     if ($rowReference[0] == '[') {
                                         $rowReference = $rowID + trim($rowReference, '[]');
                                     }
                                     $columnReference = $cellReference[4][0];
                                     //	Empty C reference is the current column
                                     if ($columnReference == '') {
                                         $columnReference = $columnNumber;
                                     }
                                     //	Bracketed C references are relative to the current column
                                     if ($columnReference[0] == '[') {
                                         $columnReference = $columnNumber + trim($columnReference, '[]');
                                     }
                                     $A1CellReference = Cell::stringFromColumnIndex($columnReference - 1) . $rowReference;
                                     $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0]));
                                 }
                             }
                         }
                         unset($value);
                         //	Then rebuild the formula string
                         $cellDataFormula = implode('"', $temp);
                     }
                     //						echo 'Cell '.$columnID.$rowID.' is a '.$type.' with a value of '.(($hasCalculatedValue) ? $cellDataFormula : $cellValue).'<br />';
                     //
                     $objPHPExcel->getActiveSheet()->getCell($columnID . $rowID)->setValueExplicit($hasCalculatedValue ? $cellDataFormula : $cellValue, $type);
                     if ($hasCalculatedValue) {
                         //							echo 'Forumla result is '.$cellValue.'<br />';
                         $objPHPExcel->getActiveSheet()->getCell($columnID . $rowID)->setCalculatedValue($cellValue);
                     }
                 }
                 if (isset($cell_ss['StyleID'])) {
                     $style = (string) $cell_ss['StyleID'];
                     //						echo 'Cell style for '.$columnID.$rowID.' is '.$style.'<br />';
                     if (isset($this->_styles[$style]) && count($this->_styles[$style]) > 0) {
                         //							echo 'Cell '.$columnID.$rowID.'<br />';
                         //							print_r($this->_styles[$style]);
                         //							echo '<br />';
                         if (!$objPHPExcel->getActiveSheet()->cellExists($columnID . $rowID)) {
                             $objPHPExcel->getActiveSheet()->getCell($columnID . $rowID)->setValue(NULL);
                         }
                         $objPHPExcel->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->_styles[$style]);
                     }
                 }
                 ++$columnID;
             }
             ++$rowID;
         }
         ++$worksheetID;
     }
     // Return
     return $objPHPExcel;
 }
Пример #14
0
 /**
  * Writes all the DEFINEDNAME records (BIFF8).
  * So far this is only used for repeating rows/columns (print titles) and print areas
  */
 private function _writeAllDefinedNamesBiff8()
 {
     $chunk = '';
     // Named ranges
     if (count($this->_phpExcel->getNamedRanges()) > 0) {
         // Loop named ranges
         $namedRanges = $this->_phpExcel->getNamedRanges();
         foreach ($namedRanges as $namedRange) {
             // Create absolute coordinate
             $range = Cell::splitRange($namedRange->getRange());
             for ($i = 0; $i < count($range); $i++) {
                 $range[$i][0] = '\'' . str_replace("'", "''", $namedRange->getWorksheet()->getTitle()) . '\'!' . Cell::absoluteCoordinate($range[$i][0]);
                 if (isset($range[$i][1])) {
                     $range[$i][1] = Cell::absoluteCoordinate($range[$i][1]);
                 }
             }
             $range = Cell::buildRange($range);
             // e.g. Sheet1!$A$1:$B$2
             // parse formula
             try {
                 $error = $this->_parser->parse($range);
                 $formulaData = $this->_parser->toReversePolish();
                 // make sure tRef3d is of type tRef3dR (0x3A)
                 if (isset($formulaData[0]) and ($formulaData[0] == "z" or $formulaData[0] == "Z")) {
                     $formulaData = ":" . substr($formulaData, 1);
                 }
                 if ($namedRange->getLocalOnly()) {
                     // local scope
                     $scope = $this->_phpExcel->getIndex($namedRange->getScope()) + 1;
                 } else {
                     // global scope
                     $scope = 0;
                 }
                 $chunk .= $this->writeData($this->_writeDefinedNameBiff8($namedRange->getName(), $formulaData, $scope, false));
             } catch (Exception $e) {
                 // do nothing
             }
         }
     }
     // total number of sheets
     $total_worksheets = $this->_phpExcel->getSheetCount();
     // write the print titles (repeating rows, columns), if any
     for ($i = 0; $i < $total_worksheets; ++$i) {
         $sheetSetup = $this->_phpExcel->getSheet($i)->getPageSetup();
         // simultaneous repeatColumns repeatRows
         if ($sheetSetup->isColumnsToRepeatAtLeftSet() && $sheetSetup->isRowsToRepeatAtTopSet()) {
             $repeat = $sheetSetup->getColumnsToRepeatAtLeft();
             $colmin = Cell::columnIndexFromString($repeat[0]) - 1;
             $colmax = Cell::columnIndexFromString($repeat[1]) - 1;
             $repeat = $sheetSetup->getRowsToRepeatAtTop();
             $rowmin = $repeat[0] - 1;
             $rowmax = $repeat[1] - 1;
             // construct formula data manually
             $formulaData = pack('Cv', 0x29, 0x17);
             // tMemFunc
             $formulaData .= pack('Cvvvvv', 0x3b, $i, 0, 65535, $colmin, $colmax);
             // tArea3d
             $formulaData .= pack('Cvvvvv', 0x3b, $i, $rowmin, $rowmax, 0, 255);
             // tArea3d
             $formulaData .= pack('C', 0x10);
             // tList
             // store the DEFINEDNAME record
             $chunk .= $this->writeData($this->_writeDefinedNameBiff8(pack('C', 0x7), $formulaData, $i + 1, true));
             // (exclusive) either repeatColumns or repeatRows
         } else {
             if ($sheetSetup->isColumnsToRepeatAtLeftSet() || $sheetSetup->isRowsToRepeatAtTopSet()) {
                 // Columns to repeat
                 if ($sheetSetup->isColumnsToRepeatAtLeftSet()) {
                     $repeat = $sheetSetup->getColumnsToRepeatAtLeft();
                     $colmin = Cell::columnIndexFromString($repeat[0]) - 1;
                     $colmax = Cell::columnIndexFromString($repeat[1]) - 1;
                 } else {
                     $colmin = 0;
                     $colmax = 255;
                 }
                 // Rows to repeat
                 if ($sheetSetup->isRowsToRepeatAtTopSet()) {
                     $repeat = $sheetSetup->getRowsToRepeatAtTop();
                     $rowmin = $repeat[0] - 1;
                     $rowmax = $repeat[1] - 1;
                 } else {
                     $rowmin = 0;
                     $rowmax = 65535;
                 }
                 // construct formula data manually because parser does not recognize absolute 3d cell references
                 $formulaData = pack('Cvvvvv', 0x3b, $i, $rowmin, $rowmax, $colmin, $colmax);
                 // store the DEFINEDNAME record
                 $chunk .= $this->writeData($this->_writeDefinedNameBiff8(pack('C', 0x7), $formulaData, $i + 1, true));
             }
         }
     }
     // write the print areas, if any
     for ($i = 0; $i < $total_worksheets; ++$i) {
         $sheetSetup = $this->_phpExcel->getSheet($i)->getPageSetup();
         if ($sheetSetup->isPrintAreaSet()) {
             // Print area, e.g. A3:J6,H1:X20
             $printArea = Cell::splitRange($sheetSetup->getPrintArea());
             $countPrintArea = count($printArea);
             $formulaData = '';
             for ($j = 0; $j < $countPrintArea; ++$j) {
                 $printAreaRect = $printArea[$j];
                 // e.g. A3:J6
                 $printAreaRect[0] = Cell::coordinateFromString($printAreaRect[0]);
                 $printAreaRect[1] = Cell::coordinateFromString($printAreaRect[1]);
                 $print_rowmin = $printAreaRect[0][1] - 1;
                 $print_rowmax = $printAreaRect[1][1] - 1;
                 $print_colmin = Cell::columnIndexFromString($printAreaRect[0][0]) - 1;
                 $print_colmax = Cell::columnIndexFromString($printAreaRect[1][0]) - 1;
                 // construct formula data manually because parser does not recognize absolute 3d cell references
                 $formulaData .= pack('Cvvvvv', 0x3b, $i, $print_rowmin, $print_rowmax, $print_colmin, $print_colmax);
                 if ($j > 0) {
                     $formulaData .= pack('C', 0x10);
                     // list operator token ','
                 }
             }
             // store the DEFINEDNAME record
             $chunk .= $this->writeData($this->_writeDefinedNameBiff8(pack('C', 0x6), $formulaData, $i + 1, true));
         }
     }
     return $chunk;
 }
Пример #15
0
 /**
  * Update single cell reference
  *
  * @param    string    $pCellReference        Single cell reference
  * @param    int        $pBefore            Insert before this one
  * @param    int        $pNumCols            Number of columns to increment
  * @param    int        $pNumRows            Number of rows to increment
  * @return    string    Updated cell reference
  * @throws    Exception
  */
 private function updateSingleCellReference($pCellReference = 'A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0)
 {
     if (strpos($pCellReference, ':') === false && strpos($pCellReference, ',') === false) {
         // Get coordinates of $pBefore
         list($beforeColumn, $beforeRow) = Cell::coordinateFromString($pBefore);
         // Get coordinates of $pCellReference
         list($newColumn, $newRow) = Cell::coordinateFromString($pCellReference);
         // Verify which parts should be updated
         $updateColumn = $newColumn[0] != '$' && $beforeColumn[0] != '$' && Cell::columnIndexFromString($newColumn) >= Cell::columnIndexFromString($beforeColumn);
         $updateRow = $newRow[0] != '$' && $beforeRow[0] != '$' && $newRow >= $beforeRow;
         // Create new column reference
         if ($updateColumn) {
             $newColumn = Cell::stringFromColumnIndex(Cell::columnIndexFromString($newColumn) - 1 + $pNumCols);
         }
         // Create new row reference
         if ($updateRow) {
             $newRow = $newRow + $pNumRows;
         }
         // Return new reference
         return $newColumn . $newRow;
     } else {
         throw new Exception("Only single cell references may be passed to this method.");
     }
 }
Пример #16
0
 /**
  * Write VML comment to XML format
  *
  * @param 	Shared_XMLWriter		$objWriter 			XML Writer
  * @param	string							$pCellReference		Cell reference
  * @param 	Comment				$pComment			Comment
  * @throws 	Exception
  */
 public function _writeVMLComment(Shared_XMLWriter $objWriter = null, $pCellReference = 'A1', Comment $pComment = null)
 {
     // Metadata
     list($column, $row) = Cell::coordinateFromString($pCellReference);
     $column = Cell::columnIndexFromString($column);
     $id = 1024 + $column + $row;
     $id = substr($id, 0, 4);
     // v:shape
     $objWriter->startElement('v:shape');
     $objWriter->writeAttribute('id', '_x0000_s' . $id);
     $objWriter->writeAttribute('type', '#_x0000_t202');
     $objWriter->writeAttribute('style', 'position:absolute;margin-left:' . $pComment->getMarginLeft() . ';margin-top:' . $pComment->getMarginTop() . ';width:' . $pComment->getWidth() . ';height:' . $pComment->getHeight() . ';z-index:1;visibility:' . ($pComment->getVisible() ? 'visible' : 'hidden'));
     $objWriter->writeAttribute('fillcolor', '#' . $pComment->getFillColor()->getRGB());
     $objWriter->writeAttribute('o:insetmode', 'auto');
     // v:fill
     $objWriter->startElement('v:fill');
     $objWriter->writeAttribute('color2', '#' . $pComment->getFillColor()->getRGB());
     $objWriter->endElement();
     // v:shadow
     $objWriter->startElement('v:shadow');
     $objWriter->writeAttribute('on', 't');
     $objWriter->writeAttribute('color', 'black');
     $objWriter->writeAttribute('obscured', 't');
     $objWriter->endElement();
     // v:path
     $objWriter->startElement('v:path');
     $objWriter->writeAttribute('o:connecttype', 'none');
     $objWriter->endElement();
     // v:textbox
     $objWriter->startElement('v:textbox');
     $objWriter->writeAttribute('style', 'mso-direction-alt:auto');
     // div
     $objWriter->startElement('div');
     $objWriter->writeAttribute('style', 'text-align:left');
     $objWriter->endElement();
     $objWriter->endElement();
     // x:ClientData
     $objWriter->startElement('x:ClientData');
     $objWriter->writeAttribute('ObjectType', 'Note');
     // x:MoveWithCells
     $objWriter->writeElement('x:MoveWithCells', '');
     // x:SizeWithCells
     $objWriter->writeElement('x:SizeWithCells', '');
     // x:Anchor
     //$objWriter->writeElement('x:Anchor', $column . ', 15, ' . ($row - 2) . ', 10, ' . ($column + 4) . ', 15, ' . ($row + 5) . ', 18');
     // x:AutoFill
     $objWriter->writeElement('x:AutoFill', 'False');
     // x:Row
     $objWriter->writeElement('x:Row', $row - 1);
     // x:Column
     $objWriter->writeElement('x:Column', $column - 1);
     $objWriter->endElement();
     $objWriter->endElement();
 }
Пример #17
0
 /**
  * Reads a cell range address in BIFF8 for shared formulas. Uses positive and negative values for row and column
  * to indicate offsets from a base cell
  * section 3.3.4
  *
  * @param string $subData
  * @param string $baseCell Base cell
  * @return string Cell range address
  */
 private function _readBIFF8CellRangeAddressB($subData, $baseCell = 'A1')
 {
     list($baseCol, $baseRow) = Cell::coordinateFromString($baseCell);
     $baseCol = Cell::columnIndexFromString($baseCol) - 1;
     // TODO: if cell range is just a single cell, should this funciton
     // not just return e.g. 'A1' and not 'A1:A1' ?
     // offset: 0; size: 2; first row
     $frIndex = $this->_GetInt2d($subData, 0);
     // adjust below
     // offset: 2; size: 2; relative index to first row (0... 65535) should be treated as offset (-32768... 32767)
     $lrIndex = $this->_GetInt2d($subData, 2);
     // adjust below
     // offset: 4; size: 2; first column with relative/absolute flags
     // bit: 7-0; mask 0x00FF; column index
     $fcIndex = 0xff & $this->_GetInt2d($subData, 4);
     // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
     if (!(0x4000 & $this->_GetInt2d($subData, 4))) {
         // absolute column index
         $fc = Cell::stringFromColumnIndex($fcIndex);
         $fc = '$' . $fc;
     } else {
         // column offset
         $fcIndex = $fcIndex <= 127 ? $fcIndex : $fcIndex - 256;
         $fc = Cell::stringFromColumnIndex($baseCol + $fcIndex);
     }
     // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
     if (!(0x8000 & $this->_GetInt2d($subData, 4))) {
         // absolute row index
         $fr = $frIndex + 1;
         $fr = '$' . $fr;
     } else {
         // row offset
         $frIndex = $frIndex <= 32767 ? $frIndex : $frIndex - 65536;
         $fr = $baseRow + $frIndex;
     }
     // offset: 6; size: 2; last column with relative/absolute flags
     // bit: 7-0; mask 0x00FF; column index
     $lcIndex = 0xff & $this->_GetInt2d($subData, 6);
     $lcIndex = $lcIndex <= 127 ? $lcIndex : $lcIndex - 256;
     $lc = Cell::stringFromColumnIndex($baseCol + $lcIndex);
     // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
     if (!(0x4000 & $this->_GetInt2d($subData, 6))) {
         // absolute column index
         $lc = Cell::stringFromColumnIndex($lcIndex);
         $lc = '$' . $lc;
     } else {
         // column offset
         $lcIndex = $lcIndex <= 127 ? $lcIndex : $lcIndex - 256;
         $lc = Cell::stringFromColumnIndex($baseCol + $lcIndex);
     }
     // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
     if (!(0x8000 & $this->_GetInt2d($subData, 6))) {
         // absolute row index
         $lr = $lrIndex + 1;
         $lr = '$' . $lr;
     } else {
         // row offset
         $lrIndex = $lrIndex <= 32767 ? $lrIndex : $lrIndex - 65536;
         $lr = $baseRow + $lrIndex;
     }
     return "{$fc}{$fr}:{$lc}{$lr}";
 }
Пример #18
0
 /**
  * Compare 2 cells
  *
  * @param 	Cell	$a	Cell a
  * @param 	Cell	$a	Cell b
  * @return 	int		Result of comparison (always -1 or 1, never zero!)
  */
 public static function compareCells(Cell $a, Cell $b)
 {
     if ($a->_row < $b->_row) {
         return -1;
     } elseif ($a->_row > $b->_row) {
         return 1;
     } elseif (Cell::columnIndexFromString($a->_column) < Cell::columnIndexFromString($b->_column)) {
         return -1;
     } else {
         return 1;
     }
 }
Пример #19
0
 private function processTokenStack($tokens, $cellID = null, Cell $pCell = null)
 {
     if ($tokens == false) {
         return false;
     }
     //    If we're using cell caching, then $pCell may well be flushed back to the cache (which detaches the parent cell collection),
     //        so we store the parent cell collection so that we can re-attach it when necessary
     $pCellWorksheet = $pCell !== null ? $pCell->getWorksheet() : null;
     $pCellParent = $pCell !== null ? $pCell->getParent() : null;
     $stack = new Calculation\Token\Stack();
     //    Loop through each token in turn
     foreach ($tokens as $tokenData) {
         //            print_r($tokenData);
         //            echo '<br />';
         $token = $tokenData['value'];
         //            echo '<b>Token is '.$token.'</b><br />';
         // if the token is a binary operator, pop the top two values off the stack, do the operation, and push the result back on the stack
         if (isset(self::$binaryOperators[$token])) {
             //                echo 'Token is a binary operator<br />';
             //    We must have two operands, error if we don't
             if (($operand2Data = $stack->pop()) === null) {
                 return $this->raiseFormulaError('Internal error - Operand value missing from stack');
             }
             if (($operand1Data = $stack->pop()) === null) {
                 return $this->raiseFormulaError('Internal error - Operand value missing from stack');
             }
             $operand1 = self::dataTestReference($operand1Data);
             $operand2 = self::dataTestReference($operand2Data);
             //    Log what we're doing
             if ($token == ':') {
                 $this->_debugLog->writeDebugLog('Evaluating Range ', $this->showValue($operand1Data['reference']), ' ', $token, ' ', $this->showValue($operand2Data['reference']));
             } else {
                 $this->_debugLog->writeDebugLog('Evaluating ', $this->showValue($operand1), ' ', $token, ' ', $this->showValue($operand2));
             }
             //    Process the operation in the appropriate manner
             switch ($token) {
                 //    Comparison (Boolean) Operators
                 case '>':
                     //    Greater than
                 //    Greater than
                 case '<':
                     //    Less than
                 //    Less than
                 case '>=':
                     //    Greater than or Equal to
                 //    Greater than or Equal to
                 case '<=':
                     //    Less than or Equal to
                 //    Less than or Equal to
                 case '=':
                     //    Equality
                 //    Equality
                 case '<>':
                     //    Inequality
                     $this->executeBinaryComparisonOperation($cellID, $operand1, $operand2, $token, $stack);
                     break;
                     //    Binary Operators
                 //    Binary Operators
                 case ':':
                     //    Range
                     $sheet1 = $sheet2 = '';
                     if (strpos($operand1Data['reference'], '!') !== false) {
                         list($sheet1, $operand1Data['reference']) = explode('!', $operand1Data['reference']);
                     } else {
                         $sheet1 = $pCellParent !== null ? $pCellWorksheet->getTitle() : '';
                     }
                     if (strpos($operand2Data['reference'], '!') !== false) {
                         list($sheet2, $operand2Data['reference']) = explode('!', $operand2Data['reference']);
                     } else {
                         $sheet2 = $sheet1;
                     }
                     if ($sheet1 == $sheet2) {
                         if ($operand1Data['reference'] === null) {
                             if (trim($operand1Data['value']) != '' && is_numeric($operand1Data['value'])) {
                                 $operand1Data['reference'] = $pCell->getColumn() . $operand1Data['value'];
                             } elseif (trim($operand1Data['reference']) == '') {
                                 $operand1Data['reference'] = $pCell->getCoordinate();
                             } else {
                                 $operand1Data['reference'] = $operand1Data['value'] . $pCell->getRow();
                             }
                         }
                         if ($operand2Data['reference'] === null) {
                             if (trim($operand2Data['value']) != '' && is_numeric($operand2Data['value'])) {
                                 $operand2Data['reference'] = $pCell->getColumn() . $operand2Data['value'];
                             } elseif (trim($operand2Data['reference']) == '') {
                                 $operand2Data['reference'] = $pCell->getCoordinate();
                             } else {
                                 $operand2Data['reference'] = $operand2Data['value'] . $pCell->getRow();
                             }
                         }
                         $oData = array_merge(explode(':', $operand1Data['reference']), explode(':', $operand2Data['reference']));
                         $oCol = $oRow = array();
                         foreach ($oData as $oDatum) {
                             $oCR = Cell::coordinateFromString($oDatum);
                             $oCol[] = Cell::columnIndexFromString($oCR[0]) - 1;
                             $oRow[] = $oCR[1];
                         }
                         $cellRef = Cell::stringFromColumnIndex(min($oCol)) . min($oRow) . ':' . Cell::stringFromColumnIndex(max($oCol)) . max($oRow);
                         if ($pCellParent !== null) {
                             $cellValue = $this->extractCellRange($cellRef, $this->spreadsheet->getSheetByName($sheet1), false);
                         } else {
                             return $this->raiseFormulaError('Unable to access Cell Reference');
                         }
                         $stack->push('Cell Reference', $cellValue, $cellRef);
                     } else {
                         $stack->push('Error', Calculation\Functions::REF(), null);
                     }
                     break;
                 case '+':
                     //    Addition
                     $this->executeNumericBinaryOperation($cellID, $operand1, $operand2, $token, 'plusEquals', $stack);
                     break;
                 case '-':
                     //    Subtraction
                     $this->executeNumericBinaryOperation($cellID, $operand1, $operand2, $token, 'minusEquals', $stack);
                     break;
                 case '*':
                     //    Multiplication
                     $this->executeNumericBinaryOperation($cellID, $operand1, $operand2, $token, 'arrayTimesEquals', $stack);
                     break;
                 case '/':
                     //    Division
                     $this->executeNumericBinaryOperation($cellID, $operand1, $operand2, $token, 'arrayRightDivide', $stack);
                     break;
                 case '^':
                     //    Exponential
                     $this->executeNumericBinaryOperation($cellID, $operand1, $operand2, $token, 'power', $stack);
                     break;
                 case '&':
                     //    Concatenation
                     //    If either of the operands is a matrix, we need to treat them both as matrices
                     //        (converting the other operand to a matrix if need be); then perform the required
                     //        matrix operation
                     if (is_bool($operand1)) {
                         $operand1 = $operand1 ? self::$localeBoolean['TRUE'] : self::$localeBoolean['FALSE'];
                     }
                     if (is_bool($operand2)) {
                         $operand2 = $operand2 ? self::$localeBoolean['TRUE'] : self::$localeBoolean['FALSE'];
                     }
                     if (is_array($operand1) || is_array($operand2)) {
                         //    Ensure that both operands are arrays/matrices
                         self::checkMatrixOperands($operand1, $operand2, 2);
                         try {
                             //    Convert operand 1 from a PHP array to a matrix
                             $matrix = new Shared\JAMA\Matrix($operand1);
                             //    Perform the required operation against the operand 1 matrix, passing in operand 2
                             $matrixResult = $matrix->concat($operand2);
                             $result = $matrixResult->getArray();
                         } catch (Exception $ex) {
                             $this->_debugLog->writeDebugLog('JAMA Matrix Exception: ', $ex->getMessage());
                             $result = '#VALUE!';
                         }
                     } else {
                         $result = '"' . str_replace('""', '"', self::unwrapResult($operand1, '"') . self::unwrapResult($operand2, '"')) . '"';
                     }
                     $this->_debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($result));
                     $stack->push('Value', $result);
                     break;
                 case '|':
                     //    Intersect
                     $rowIntersect = array_intersect_key($operand1, $operand2);
                     $cellIntersect = $oCol = $oRow = array();
                     foreach (array_keys($rowIntersect) as $row) {
                         $oRow[] = $row;
                         foreach ($rowIntersect[$row] as $col => $data) {
                             $oCol[] = Cell::columnIndexFromString($col) - 1;
                             $cellIntersect[$row] = array_intersect_key($operand1[$row], $operand2[$row]);
                         }
                     }
                     $cellRef = Cell::stringFromColumnIndex(min($oCol)) . min($oRow) . ':' . Cell::stringFromColumnIndex(max($oCol)) . max($oRow);
                     $this->_debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($cellIntersect));
                     $stack->push('Value', $cellIntersect, $cellRef);
                     break;
             }
             // if the token is a unary operator, pop one value off the stack, do the operation, and push it back on
         } elseif ($token === '~' || $token === '%') {
             //                echo 'Token is a unary operator<br />';
             if (($arg = $stack->pop()) === null) {
                 return $this->raiseFormulaError('Internal error - Operand value missing from stack');
             }
             $arg = $arg['value'];
             if ($token === '~') {
                 //                    echo 'Token is a negation operator<br />';
                 $this->_debugLog->writeDebugLog('Evaluating Negation of ', $this->showValue($arg));
                 $multiplier = -1;
             } else {
                 //                    echo 'Token is a percentile operator<br />';
                 $this->_debugLog->writeDebugLog('Evaluating Percentile of ', $this->showValue($arg));
                 $multiplier = 0.01;
             }
             if (is_array($arg)) {
                 self::checkMatrixOperands($arg, $multiplier, 2);
                 try {
                     $matrix1 = new Shared\JAMA\Matrix($arg);
                     $matrixResult = $matrix1->arrayTimesEquals($multiplier);
                     $result = $matrixResult->getArray();
                 } catch (Exception $ex) {
                     $this->_debugLog->writeDebugLog('JAMA Matrix Exception: ', $ex->getMessage());
                     $result = '#VALUE!';
                 }
                 $this->_debugLog->writeDebugLog('Evaluation Result is ', $this->showTypeDetails($result));
                 $stack->push('Value', $result);
             } else {
                 $this->executeNumericBinaryOperation($cellID, $multiplier, $arg, '*', 'arrayTimesEquals', $stack);
             }
         } elseif (preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '$/i', $token, $matches)) {
             $cellRef = null;
             //                echo 'Element '.$token.' is a Cell reference<br />';
             if (isset($matches[8])) {
                 //                    echo 'Reference is a Range of cells<br />';
                 if ($pCell === null) {
                     //                        We can't access the range, so return a REF error
                     $cellValue = Calculation\Functions::REF();
                 } else {
                     $cellRef = $matches[6] . $matches[7] . ':' . $matches[9] . $matches[10];
                     if ($matches[2] > '') {
                         $matches[2] = trim($matches[2], "\"'");
                         if (strpos($matches[2], '[') !== false || strpos($matches[2], ']') !== false) {
                             //    It's a Reference to an external spreadsheet (not currently supported)
                             return $this->raiseFormulaError('Unable to access External Workbook');
                         }
                         $matches[2] = trim($matches[2], "\"'");
                         //                            echo '$cellRef='.$cellRef.' in worksheet '.$matches[2].'<br />';
                         $this->_debugLog->writeDebugLog('Evaluating Cell Range ', $cellRef, ' in worksheet ', $matches[2]);
                         if ($pCellParent !== null) {
                             $cellValue = $this->extractCellRange($cellRef, $this->spreadsheet->getSheetByName($matches[2]), false);
                         } else {
                             return $this->raiseFormulaError('Unable to access Cell Reference');
                         }
                         $this->_debugLog->writeDebugLog('Evaluation Result for cells ', $cellRef, ' in worksheet ', $matches[2], ' is ', $this->showTypeDetails($cellValue));
                         //                            $cellRef = $matches[2].'!'.$cellRef;
                     } else {
                         //                            echo '$cellRef='.$cellRef.' in current worksheet<br />';
                         $this->_debugLog->writeDebugLog('Evaluating Cell Range ', $cellRef, ' in current worksheet');
                         if ($pCellParent !== null) {
                             $cellValue = $this->extractCellRange($cellRef, $pCellWorksheet, false);
                         } else {
                             return $this->raiseFormulaError('Unable to access Cell Reference');
                         }
                         $this->_debugLog->writeDebugLog('Evaluation Result for cells ', $cellRef, ' is ', $this->showTypeDetails($cellValue));
                     }
                 }
             } else {
                 //                    echo 'Reference is a single Cell<br />';
                 if ($pCell === null) {
                     //                        We can't access the cell, so return a REF error
                     $cellValue = Calculation\Functions::REF();
                 } else {
                     $cellRef = $matches[6] . $matches[7];
                     if ($matches[2] > '') {
                         $matches[2] = trim($matches[2], "\"'");
                         if (strpos($matches[2], '[') !== false || strpos($matches[2], ']') !== false) {
                             //    It's a Reference to an external spreadsheet (not currently supported)
                             return $this->raiseFormulaError('Unable to access External Workbook');
                         }
                         //                            echo '$cellRef='.$cellRef.' in worksheet '.$matches[2].'<br />';
                         $this->_debugLog->writeDebugLog('Evaluating Cell ', $cellRef, ' in worksheet ', $matches[2]);
                         if ($pCellParent !== null) {
                             $cellSheet = $this->spreadsheet->getSheetByName($matches[2]);
                             if ($cellSheet && $cellSheet->cellExists($cellRef)) {
                                 $cellValue = $this->extractCellRange($cellRef, $this->spreadsheet->getSheetByName($matches[2]), false);
                                 $pCell->attach($pCellParent);
                             } else {
                                 $cellValue = null;
                             }
                         } else {
                             return $this->raiseFormulaError('Unable to access Cell Reference');
                         }
                         $this->_debugLog->writeDebugLog('Evaluation Result for cell ', $cellRef, ' in worksheet ', $matches[2], ' is ', $this->showTypeDetails($cellValue));
                         //                            $cellRef = $matches[2].'!'.$cellRef;
                     } else {
                         //                            echo '$cellRef='.$cellRef.' in current worksheet<br />';
                         $this->_debugLog->writeDebugLog('Evaluating Cell ', $cellRef, ' in current worksheet');
                         if ($pCellParent->isDataSet($cellRef)) {
                             $cellValue = $this->extractCellRange($cellRef, $pCellWorksheet, false);
                             $pCell->attach($pCellParent);
                         } else {
                             $cellValue = null;
                         }
                         $this->_debugLog->writeDebugLog('Evaluation Result for cell ', $cellRef, ' is ', $this->showTypeDetails($cellValue));
                     }
                 }
             }
             $stack->push('Value', $cellValue, $cellRef);
             // if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on
         } elseif (preg_match('/^' . self::CALCULATION_REGEXP_FUNCTION . '$/i', $token, $matches)) {
             //                echo 'Token is a function<br />';
             $functionName = $matches[1];
             $argCount = $stack->pop();
             $argCount = $argCount['value'];
             if ($functionName != 'MKMATRIX') {
                 $this->_debugLog->writeDebugLog('Evaluating Function ', self::localeFunc($functionName), '() with ', $argCount == 0 ? 'no' : $argCount, ' argument', $argCount == 1 ? '' : 's');
             }
             if (isset(self::$PHPExcelFunctions[$functionName]) || isset(self::$controlFunctions[$functionName])) {
                 // function
                 if (isset(self::$PHPExcelFunctions[$functionName])) {
                     $functionCall = self::$PHPExcelFunctions[$functionName]['functionCall'];
                     $passByReference = isset(self::$PHPExcelFunctions[$functionName]['passByReference']);
                     $passCellReference = isset(self::$PHPExcelFunctions[$functionName]['passCellReference']);
                 } elseif (isset(self::$controlFunctions[$functionName])) {
                     $functionCall = self::$controlFunctions[$functionName]['functionCall'];
                     $passByReference = isset(self::$controlFunctions[$functionName]['passByReference']);
                     $passCellReference = isset(self::$controlFunctions[$functionName]['passCellReference']);
                 }
                 // get the arguments for this function
                 //                    echo 'Function '.$functionName.' expects '.$argCount.' arguments<br />';
                 $args = $argArrayVals = array();
                 for ($i = 0; $i < $argCount; ++$i) {
                     $arg = $stack->pop();
                     $a = $argCount - $i - 1;
                     if ($passByReference && isset(self::$PHPExcelFunctions[$functionName]['passByReference'][$a]) && self::$PHPExcelFunctions[$functionName]['passByReference'][$a]) {
                         if ($arg['reference'] === null) {
                             $args[] = $cellID;
                             if ($functionName != 'MKMATRIX') {
                                 $argArrayVals[] = $this->showValue($cellID);
                             }
                         } else {
                             $args[] = $arg['reference'];
                             if ($functionName != 'MKMATRIX') {
                                 $argArrayVals[] = $this->showValue($arg['reference']);
                             }
                         }
                     } else {
                         $args[] = self::unwrapResult($arg['value']);
                         if ($functionName != 'MKMATRIX') {
                             $argArrayVals[] = $this->showValue($arg['value']);
                         }
                     }
                 }
                 //    Reverse the order of the arguments
                 krsort($args);
                 if ($passByReference && $argCount == 0) {
                     $args[] = $cellID;
                     $argArrayVals[] = $this->showValue($cellID);
                 }
                 //                    echo 'Arguments are: ';
                 //                    print_r($args);
                 //                    echo '<br />';
                 if ($functionName != 'MKMATRIX') {
                     if ($this->_debugLog->getWriteDebugLog()) {
                         krsort($argArrayVals);
                         $this->_debugLog->writeDebugLog('Evaluating ', self::localeFunc($functionName), '( ', implode(self::$localeArgumentSeparator . ' ', Calculation\Functions::flattenArray($argArrayVals)), ' )');
                     }
                 }
                 //    Process each argument in turn, building the return value as an array
                 //                    if (($argCount == 1) && (is_array($args[1])) && ($functionName != 'MKMATRIX')) {
                 //                        $operand1 = $args[1];
                 //                        $this->_debugLog->writeDebugLog('Argument is a matrix: ', $this->showValue($operand1));
                 //                        $result = array();
                 //                        $row = 0;
                 //                        foreach($operand1 as $args) {
                 //                            if (is_array($args)) {
                 //                                foreach($args as $arg) {
                 //                                    $this->_debugLog->writeDebugLog('Evaluating ', self::localeFunc($functionName), '( ', $this->showValue($arg), ' )');
                 //                                    $r = call_user_func_array($functionCall, $arg);
                 //                                    $this->_debugLog->writeDebugLog('Evaluation Result for ', self::localeFunc($functionName), '() function call is ', $this->showTypeDetails($r));
                 //                                    $result[$row][] = $r;
                 //                                }
                 //                                ++$row;
                 //                            } else {
                 //                                $this->_debugLog->writeDebugLog('Evaluating ', self::localeFunc($functionName), '( ', $this->showValue($args), ' )');
                 //                                $r = call_user_func_array($functionCall, $args);
                 //                                $this->_debugLog->writeDebugLog('Evaluation Result for ', self::localeFunc($functionName), '() function call is ', $this->showTypeDetails($r));
                 //                                $result[] = $r;
                 //                            }
                 //                        }
                 //                    } else {
                 //    Process the argument with the appropriate function call
                 if ($passCellReference) {
                     $args[] = $pCell;
                 }
                 if (strpos($functionCall, '::') !== false) {
                     $result = call_user_func_array(explode('::', $functionCall), $args);
                 } else {
                     foreach ($args as &$arg) {
                         $arg = Calculation\Functions::flattenSingleValue($arg);
                     }
                     unset($arg);
                     $result = call_user_func_array($functionCall, $args);
                 }
                 if ($functionName != 'MKMATRIX') {
                     $this->_debugLog->writeDebugLog('Evaluation Result for ', self::localeFunc($functionName), '() function call is ', $this->showTypeDetails($result));
                 }
                 $stack->push('Value', self::wrapResult($result));
             }
         } else {
             // if the token is a number, boolean, string or an Excel error, push it onto the stack
             if (isset(self::$excelConstants[strtoupper($token)])) {
                 $excelConstant = strtoupper($token);
                 //                    echo 'Token is a PHPExcel constant: '.$excelConstant.'<br />';
                 $stack->push('Constant Value', self::$excelConstants[$excelConstant]);
                 $this->_debugLog->writeDebugLog('Evaluating Constant ', $excelConstant, ' as ', $this->showTypeDetails(self::$excelConstants[$excelConstant]));
             } elseif (is_numeric($token) || $token === null || is_bool($token) || $token == '' || $token[0] == '"' || $token[0] == '#') {
                 //                    echo 'Token is a number, boolean, string, null or an Excel error<br />';
                 $stack->push('Value', $token);
                 // if the token is a named range, push the named range name onto the stack
             } elseif (preg_match('/^' . self::CALCULATION_REGEXP_NAMEDRANGE . '$/i', $token, $matches)) {
                 //                    echo 'Token is a named range<br />';
                 $namedRange = $matches[6];
                 //                    echo 'Named Range is '.$namedRange.'<br />';
                 $this->_debugLog->writeDebugLog('Evaluating Named Range ', $namedRange);
                 $cellValue = $this->extractNamedRange($namedRange, null !== $pCell ? $pCellWorksheet : null, false);
                 $pCell->attach($pCellParent);
                 $this->_debugLog->writeDebugLog('Evaluation Result for named range ', $namedRange, ' is ', $this->showTypeDetails($cellValue));
                 $stack->push('Named Range', $cellValue, $namedRange);
             } else {
                 return $this->raiseFormulaError("undefined variable '{$token}'");
             }
         }
     }
     // when we're out of tokens, the stack should have a single element, the final result
     if ($stack->count() != 1) {
         return $this->raiseFormulaError("internal error");
     }
     $output = $stack->pop();
     $output = $output['value'];
     //        if ((is_array($output)) && (self::$returnArrayAsType != self::RETURN_ARRAY_AS_ARRAY)) {
     //            return array_shift(Calculation\Functions::flattenArray($output));
     //        }
     return $output;
 }
Пример #20
0
 /**
  * Loads PHPExcel from file into PHPExcel instance
  *
  * @param 	string 		$pFilename
  * @param	PHPExcel	$objPHPExcel
  * @return 	PHPExcel
  * @throws 	Exception
  */
 public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
 {
     // Check if file exists
     if (!file_exists($pFilename)) {
         throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
     }
     $zip = new ZipArchive();
     if ($zip->open($pFilename) === true) {
         //			echo '<h1>Meta Information</h1>';
         $xml = simplexml_load_string($zip->getFromName("meta.xml"));
         $namespacesMeta = $xml->getNamespaces(true);
         //			echo '<pre>';
         //			print_r($namespacesMeta);
         //			echo '</pre><hr />';
         $docProps = $objPHPExcel->getProperties();
         $officeProperty = $xml->children($namespacesMeta['office']);
         foreach ($officeProperty as $officePropertyData) {
             $officePropertyDC = array();
             if (isset($namespacesMeta['dc'])) {
                 $officePropertyDC = $officePropertyData->children($namespacesMeta['dc']);
             }
             foreach ($officePropertyDC as $propertyName => $propertyValue) {
                 //					echo $propertyName.' = '.$propertyValue.'<hr />';
                 switch ($propertyName) {
                     case 'title':
                         $docProps->setTitle($propertyValue);
                         break;
                     case 'subject':
                         $docProps->setSubject($propertyValue);
                         break;
                     case 'creator':
                         $docProps->setCreator($propertyValue);
                         break;
                     case 'date':
                         $creationDate = strtotime($propertyValue);
                         $docProps->setCreated($creationDate);
                         break;
                     case 'description':
                         $docProps->setDescription($propertyValue);
                         break;
                 }
             }
             $officePropertyMeta = array();
             if (isset($namespacesMeta['dc'])) {
                 $officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
             }
             foreach ($officePropertyMeta as $propertyName => $propertyValue) {
                 $propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta']);
                 //					echo $propertyName.' = '.$propertyValue.'<br />';
                 //					foreach ($propertyValueAttributes as $key => $value) {
                 //						echo $key.' = '.$value.'<br />';
                 //					}
                 //					echo '<hr />';
                 //
                 switch ($propertyName) {
                     case 'keyword':
                         $docProps->setKeywords($propertyValue);
                         break;
                 }
             }
         }
         //			echo '<h1>Workbook Content</h1>';
         $xml = simplexml_load_string($zip->getFromName("content.xml"));
         $namespacesContent = $xml->getNamespaces(true);
         //			echo '<pre>';
         //			print_r($namespacesContent);
         //			echo '</pre><hr />';
         $workbook = $xml->children($namespacesContent['office']);
         foreach ($workbook->body->spreadsheet as $workbookData) {
             $workbookData = $workbookData->children($namespacesContent['table']);
             $worksheetID = 0;
             foreach ($workbookData->table as $worksheetDataSet) {
                 $worksheetData = $worksheetDataSet->children($namespacesContent['table']);
                 //					print_r($worksheetData);
                 //					echo '<br />';
                 $worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
                 //					print_r($worksheetDataAttributes);
                 //					echo '<br />';
                 if (isset($this->_loadSheetsOnly) && isset($worksheetDataAttributes['name']) && !in_array($worksheetDataAttributes['name'], $this->_loadSheetsOnly)) {
                     continue;
                 }
                 //					echo '<h2>Worksheet '.$worksheetDataAttributes['name'].'</h2>';
                 // Create new Worksheet
                 $objPHPExcel->createSheet();
                 $objPHPExcel->setActiveSheetIndex($worksheetID);
                 if (isset($worksheetDataAttributes['name'])) {
                     $worksheetName = (string) $worksheetDataAttributes['name'];
                     $objPHPExcel->getActiveSheet()->setTitle($worksheetName);
                 }
                 $rowID = 1;
                 foreach ($worksheetData as $key => $rowData) {
                     //						echo '<b>'.$key.'</b><br />';
                     switch ($key) {
                         case 'table-row':
                             $columnID = 'A';
                             foreach ($rowData as $key => $cellData) {
                                 //									echo '<b>'.$columnID.$rowID.'</b><br />';
                                 $cellDataText = $cellData->children($namespacesContent['text']);
                                 $cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
                                 $cellDataTableAttributes = $cellData->attributes($namespacesContent['table']);
                                 //									echo 'Office Attributes: ';
                                 //									print_r($cellDataOfficeAttributes);
                                 //									echo '<br />Table Attributes: ';
                                 //									print_r($cellDataTableAttributes);
                                 //									echo '<br />Cell Data Text';
                                 //									print_r($cellDataText);
                                 //									echo '<br />';
                                 //
                                 $type = $formatting = $hyperlink = null;
                                 $hasCalculatedValue = false;
                                 $cellDataFormula = '';
                                 if (isset($cellDataTableAttributes['formula'])) {
                                     $cellDataFormula = $cellDataTableAttributes['formula'];
                                     $hasCalculatedValue = true;
                                 }
                                 if (isset($cellDataText->p)) {
                                     //										echo 'Value Type is '.$cellDataOfficeAttributes['value-type'].'<br />';
                                     switch ($cellDataOfficeAttributes['value-type']) {
                                         case 'string':
                                             $type = Cell_DataType::TYPE_STRING;
                                             $dataValue = $cellDataText->p;
                                             if (isset($dataValue->a)) {
                                                 $dataValue = $dataValue->a;
                                                 $cellXLinkAttributes = $dataValue->attributes($namespacesContent['xlink']);
                                                 $hyperlink = $cellXLinkAttributes['href'];
                                             }
                                             break;
                                         case 'boolean':
                                             $type = Cell_DataType::TYPE_BOOL;
                                             $dataValue = $cellDataText->p == 'TRUE' ? True : False;
                                             break;
                                         case 'float':
                                             $type = Cell_DataType::TYPE_NUMERIC;
                                             $dataValue = (double) $cellDataOfficeAttributes['value'];
                                             if (floor($dataValue) == $dataValue) {
                                                 $dataValue = (int) $dataValue;
                                             }
                                             break;
                                         case 'date':
                                             $type = Cell_DataType::TYPE_NUMERIC;
                                             $dateObj = date_create($cellDataOfficeAttributes['date-value']);
                                             list($year, $month, $day, $hour, $minute, $second) = explode(' ', $dateObj->format('Y m d H i s'));
                                             $dataValue = Shared_Date::FormattedPHPToExcel($year, $month, $day, $hour, $minute, $second);
                                             if ($dataValue != floor($dataValue)) {
                                                 $formatting = Style_NumberFormat::FORMAT_DATE_XLSX15 . ' ' . Style_NumberFormat::FORMAT_DATE_TIME4;
                                             } else {
                                                 $formatting = Style_NumberFormat::FORMAT_DATE_XLSX15;
                                             }
                                             break;
                                         case 'time':
                                             $type = Cell_DataType::TYPE_NUMERIC;
                                             $dataValue = Shared_Date::PHPToExcel(strtotime('01-01-1970 ' . implode(':', sscanf($cellDataOfficeAttributes['time-value'], 'PT%dH%dM%dS'))));
                                             $formatting = Style_NumberFormat::FORMAT_DATE_TIME4;
                                             break;
                                     }
                                     //										echo 'Data value is '.$dataValue.'<br />';
                                     //										if (!is_null($hyperlink)) {
                                     //											echo 'Hyperlink is '.$hyperlink.'<br />';
                                     //										}
                                 }
                                 if ($hasCalculatedValue) {
                                     $type = Cell_DataType::TYPE_FORMULA;
                                     //										echo 'Formula: '.$cellDataFormula.'<br />';
                                     $cellDataFormula = substr($cellDataFormula, strpos($cellDataFormula, ':=') + 1);
                                     $temp = explode('"', $cellDataFormula);
                                     foreach ($temp as $key => &$value) {
                                         //	Only replace in alternate array entries (i.e. non-quoted blocks)
                                         if ($key % 2 == 0) {
                                             $value = preg_replace('/\\[\\.(.*):\\.(.*)\\]/Ui', '$1:$2', $value);
                                             $value = preg_replace('/\\[\\.(.*)\\]/Ui', '$1', $value);
                                             $value = Calculation::_translateSeparator(';', ',', $value);
                                         }
                                     }
                                     unset($value);
                                     //	Then rebuild the formula string
                                     $cellDataFormula = implode('"', $temp);
                                     //										echo 'Adjusted Formula: '.$cellDataFormula.'<br />';
                                 }
                                 if (!is_null($type)) {
                                     $objPHPExcel->getActiveSheet()->getCell($columnID . $rowID)->setValueExplicit($hasCalculatedValue ? $cellDataFormula : $dataValue, $type);
                                     if ($hasCalculatedValue) {
                                         //											echo 'Forumla result is '.$dataValue.'<br />';
                                         $objPHPExcel->getActiveSheet()->getCell($columnID . $rowID)->setCalculatedValue($dataValue);
                                     }
                                     if ($cellDataOfficeAttributes['value-type'] == 'date' || $cellDataOfficeAttributes['value-type'] == 'time') {
                                         $objPHPExcel->getActiveSheet()->getStyle($columnID . $rowID)->getNumberFormat()->setFormatCode($formatting);
                                     }
                                     if (!is_null($hyperlink)) {
                                         $objPHPExcel->getActiveSheet()->getCell($columnID . $rowID)->getHyperlink()->setUrl($hyperlink);
                                     }
                                 }
                                 //	Merged cells
                                 if (isset($cellDataTableAttributes['number-columns-spanned']) || isset($cellDataTableAttributes['number-rows-spanned'])) {
                                     $columnTo = $columnID;
                                     if (isset($cellDataTableAttributes['number-columns-spanned'])) {
                                         $columnTo = Cell::stringFromColumnIndex(Cell::columnIndexFromString($columnID) + $cellDataTableAttributes['number-columns-spanned'] - 2);
                                     }
                                     $rowTo = $rowID;
                                     if (isset($cellDataTableAttributes['number-rows-spanned'])) {
                                         $rowTo = $rowTo + $cellDataTableAttributes['number-rows-spanned'] - 1;
                                     }
                                     $cellRange = $columnID . $rowID . ':' . $columnTo . $rowTo;
                                     $objPHPExcel->getActiveSheet()->mergeCells($cellRange);
                                 }
                                 if (isset($cellDataTableAttributes['number-columns-repeated'])) {
                                     //										echo 'Repeated '.$cellDataTableAttributes['number-columns-repeated'].' times<br />';
                                     $columnID = Cell::stringFromColumnIndex(Cell::columnIndexFromString($columnID) + $cellDataTableAttributes['number-columns-repeated'] - 2);
                                 }
                                 ++$columnID;
                             }
                             ++$rowID;
                             break;
                     }
                 }
                 ++$worksheetID;
             }
         }
     }
     // Return
     return $objPHPExcel;
 }