[ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * PHPExcel 4 * 5 * Copyright (c) 2006 - 2012 PHPExcel 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 * @category PHPExcel 22 * @package PHPExcel 23 * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel) 24 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL 25 * @version 1.7.7, 2012-05-19 26 */ 27 28 29 /** PHPExcel root directory */ 30 if (!defined('PHPEXCEL_ROOT')) { 31 define('PHPEXCEL_ROOT', dirname(__FILE__) . '/'); 32 require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); 33 } 34 35 36 /** 37 * PHPExcel 38 * 39 * @category PHPExcel 40 * @package PHPExcel 41 * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel) 42 */ 43 class PHPExcel 44 { 45 /** 46 * Document properties 47 * 48 * @var PHPExcel_DocumentProperties 49 */ 50 private $_properties; 51 52 /** 53 * Document security 54 * 55 * @var PHPExcel_DocumentSecurity 56 */ 57 private $_security; 58 59 /** 60 * Collection of Worksheet objects 61 * 62 * @var PHPExcel_Worksheet[] 63 */ 64 private $_workSheetCollection = array(); 65 66 /** 67 * Active sheet index 68 * 69 * @var int 70 */ 71 private $_activeSheetIndex = 0; 72 73 /** 74 * Named ranges 75 * 76 * @var PHPExcel_NamedRange[] 77 */ 78 private $_namedRanges = array(); 79 80 /** 81 * CellXf supervisor 82 * 83 * @var PHPExcel_Style 84 */ 85 private $_cellXfSupervisor; 86 87 /** 88 * CellXf collection 89 * 90 * @var PHPExcel_Style[] 91 */ 92 private $_cellXfCollection = array(); 93 94 /** 95 * CellStyleXf collection 96 * 97 * @var PHPExcel_Style[] 98 */ 99 private $_cellStyleXfCollection = array(); 100 101 /** 102 * Create a new PHPExcel with one Worksheet 103 */ 104 public function __construct() 105 { 106 // Initialise worksheet collection and add one worksheet 107 $this->_workSheetCollection = array(); 108 $this->_workSheetCollection[] = new PHPExcel_Worksheet($this); 109 $this->_activeSheetIndex = 0; 110 111 // Create document properties 112 $this->_properties = new PHPExcel_DocumentProperties(); 113 114 // Create document security 115 $this->_security = new PHPExcel_DocumentSecurity(); 116 117 // Set named ranges 118 $this->_namedRanges = array(); 119 120 // Create the cellXf supervisor 121 $this->_cellXfSupervisor = new PHPExcel_Style(true); 122 $this->_cellXfSupervisor->bindParent($this); 123 124 // Create the default style 125 $this->addCellXf(new PHPExcel_Style); 126 $this->addCellStyleXf(new PHPExcel_Style); 127 } 128 129 130 /** 131 * Disconnect all worksheets from this PHPExcel workbook object, 132 * typically so that the PHPExcel object can be unset 133 * 134 */ 135 public function disconnectWorksheets() { 136 foreach($this->_workSheetCollection as $k => &$worksheet) { 137 $worksheet->disconnectCells(); 138 $this->_workSheetCollection[$k] = null; 139 } 140 unset($worksheet); 141 $this->_workSheetCollection = array(); 142 } 143 144 /** 145 * Get properties 146 * 147 * @return PHPExcel_DocumentProperties 148 */ 149 public function getProperties() 150 { 151 return $this->_properties; 152 } 153 154 /** 155 * Set properties 156 * 157 * @param PHPExcel_DocumentProperties $pValue 158 */ 159 public function setProperties(PHPExcel_DocumentProperties $pValue) 160 { 161 $this->_properties = $pValue; 162 } 163 164 /** 165 * Get security 166 * 167 * @return PHPExcel_DocumentSecurity 168 */ 169 public function getSecurity() 170 { 171 return $this->_security; 172 } 173 174 /** 175 * Set security 176 * 177 * @param PHPExcel_DocumentSecurity $pValue 178 */ 179 public function setSecurity(PHPExcel_DocumentSecurity $pValue) 180 { 181 $this->_security = $pValue; 182 } 183 184 /** 185 * Get active sheet 186 * 187 * @return PHPExcel_Worksheet 188 */ 189 public function getActiveSheet() 190 { 191 return $this->_workSheetCollection[$this->_activeSheetIndex]; 192 } 193 194 /** 195 * Create sheet and add it to this workbook 196 * 197 * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last) 198 * @return PHPExcel_Worksheet 199 * @throws Exception 200 */ 201 public function createSheet($iSheetIndex = null) 202 { 203 $newSheet = new PHPExcel_Worksheet($this); 204 $this->addSheet($newSheet, $iSheetIndex); 205 return $newSheet; 206 } 207 208 /** 209 * Add sheet 210 * 211 * @param PHPExcel_Worksheet $pSheet 212 * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last) 213 * @return PHPExcel_Worksheet 214 * @throws Exception 215 */ 216 public function addSheet(PHPExcel_Worksheet $pSheet = null, $iSheetIndex = null) 217 { 218 if($iSheetIndex === NULL) { 219 $this->_workSheetCollection[] = $pSheet; 220 } else { 221 // Insert the sheet at the requested index 222 array_splice( 223 $this->_workSheetCollection, 224 $iSheetIndex, 225 0, 226 array($pSheet) 227 ); 228 229 // Adjust active sheet index if necessary 230 if ($this->_activeSheetIndex >= $iSheetIndex) { 231 ++$this->_activeSheetIndex; 232 } 233 } 234 return $pSheet; 235 } 236 237 /** 238 * Remove sheet by index 239 * 240 * @param int $pIndex Active sheet index 241 * @throws Exception 242 */ 243 public function removeSheetByIndex($pIndex = 0) 244 { 245 if ($pIndex > count($this->_workSheetCollection) - 1) { 246 throw new Exception("Sheet index is out of bounds."); 247 } else { 248 array_splice($this->_workSheetCollection, $pIndex, 1); 249 } 250 // Adjust active sheet index if necessary 251 if (($this->_activeSheetIndex >= $pIndex) && 252 ($pIndex > count($this->_workSheetCollection) - 1)) { 253 --$this->_activeSheetIndex; 254 } 255 256 } 257 258 /** 259 * Get sheet by index 260 * 261 * @param int $pIndex Sheet index 262 * @return PHPExcel_Worksheet 263 * @throws Exception 264 */ 265 public function getSheet($pIndex = 0) 266 { 267 if ($pIndex > count($this->_workSheetCollection) - 1) { 268 throw new Exception("Sheet index is out of bounds."); 269 } else { 270 return $this->_workSheetCollection[$pIndex]; 271 } 272 } 273 274 /** 275 * Get all sheets 276 * 277 * @return PHPExcel_Worksheet[] 278 */ 279 public function getAllSheets() 280 { 281 return $this->_workSheetCollection; 282 } 283 284 /** 285 * Get sheet by name 286 * 287 * @param string $pName Sheet name 288 * @return PHPExcel_Worksheet 289 * @throws Exception 290 */ 291 public function getSheetByName($pName = '') 292 { 293 $worksheetCount = count($this->_workSheetCollection); 294 for ($i = 0; $i < $worksheetCount; ++$i) { 295 if ($this->_workSheetCollection[$i]->getTitle() == $pName) { 296 return $this->_workSheetCollection[$i]; 297 } 298 } 299 300 return null; 301 } 302 303 /** 304 * Get index for sheet 305 * 306 * @param PHPExcel_Worksheet $pSheet 307 * @return Sheet index 308 * @throws Exception 309 */ 310 public function getIndex(PHPExcel_Worksheet $pSheet) 311 { 312 foreach ($this->_workSheetCollection as $key => $value) { 313 if ($value->getHashCode() == $pSheet->getHashCode()) { 314 return $key; 315 } 316 } 317 } 318 319 /** 320 * Set index for sheet by sheet name. 321 * 322 * @param string $sheetName Sheet name to modify index for 323 * @param int $newIndex New index for the sheet 324 * @return New sheet index 325 * @throws Exception 326 */ 327 public function setIndexByName($sheetName, $newIndex) 328 { 329 $oldIndex = $this->getIndex($this->getSheetByName($sheetName)); 330 $pSheet = array_splice( 331 $this->_workSheetCollection, 332 $oldIndex, 333 1 334 ); 335 array_splice( 336 $this->_workSheetCollection, 337 $newIndex, 338 0, 339 $pSheet 340 ); 341 return $newIndex; 342 } 343 344 /** 345 * Get sheet count 346 * 347 * @return int 348 */ 349 public function getSheetCount() 350 { 351 return count($this->_workSheetCollection); 352 } 353 354 /** 355 * Get active sheet index 356 * 357 * @return int Active sheet index 358 */ 359 public function getActiveSheetIndex() 360 { 361 return $this->_activeSheetIndex; 362 } 363 364 /** 365 * Set active sheet index 366 * 367 * @param int $pIndex Active sheet index 368 * @throws Exception 369 * @return PHPExcel_Worksheet 370 */ 371 public function setActiveSheetIndex($pIndex = 0) 372 { 373 if ($pIndex > count($this->_workSheetCollection) - 1) { 374 throw new Exception("Active sheet index is out of bounds."); 375 } else { 376 $this->_activeSheetIndex = $pIndex; 377 } 378 return $this->getActiveSheet(); 379 } 380 381 /** 382 * Set active sheet index by name 383 * 384 * @param string $pValue Sheet title 385 * @return PHPExcel_Worksheet 386 * @throws Exception 387 */ 388 public function setActiveSheetIndexByName($pValue = '') 389 { 390 if (($worksheet = $this->getSheetByName($pValue)) instanceof PHPExcel_Worksheet) { 391 $this->setActiveSheetIndex($this->getIndex($worksheet)); 392 return $worksheet; 393 } 394 395 throw new Exception('Workbook does not contain sheet:' . $pValue); 396 } 397 398 /** 399 * Get sheet names 400 * 401 * @return string[] 402 */ 403 public function getSheetNames() 404 { 405 $returnValue = array(); 406 $worksheetCount = $this->getSheetCount(); 407 for ($i = 0; $i < $worksheetCount; ++$i) { 408 $returnValue[] = $this->getSheet($i)->getTitle(); 409 } 410 411 return $returnValue; 412 } 413 414 /** 415 * Add external sheet 416 * 417 * @param PHPExcel_Worksheet $pSheet External sheet to add 418 * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last) 419 * @throws Exception 420 * @return PHPExcel_Worksheet 421 */ 422 public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null) { 423 if ($this->getSheetByName($pSheet->getTitle()) !== NULL) { 424 throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first."); 425 } 426 427 // count how many cellXfs there are in this workbook currently, we will need this below 428 $countCellXfs = count($this->_cellXfCollection); 429 430 // copy all the shared cellXfs from the external workbook and append them to the current 431 foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) { 432 $this->addCellXf(clone $cellXf); 433 } 434 435 // move sheet to this workbook 436 $pSheet->rebindParent($this); 437 438 // update the cellXfs 439 foreach ($pSheet->getCellCollection(false) as $cellID) { 440 $cell = $pSheet->getCell($cellID); 441 $cell->setXfIndex( $cell->getXfIndex() + $countCellXfs ); 442 } 443 444 return $this->addSheet($pSheet, $iSheetIndex); 445 } 446 447 /** 448 * Get named ranges 449 * 450 * @return PHPExcel_NamedRange[] 451 */ 452 public function getNamedRanges() { 453 return $this->_namedRanges; 454 } 455 456 /** 457 * Add named range 458 * 459 * @param PHPExcel_NamedRange $namedRange 460 * @return PHPExcel 461 */ 462 public function addNamedRange(PHPExcel_NamedRange $namedRange) { 463 if ($namedRange->getScope() == null) { 464 // global scope 465 $this->_namedRanges[$namedRange->getName()] = $namedRange; 466 } else { 467 // local scope 468 $this->_namedRanges[$namedRange->getScope()->getTitle().'!'.$namedRange->getName()] = $namedRange; 469 } 470 return true; 471 } 472 473 /** 474 * Get named range 475 * 476 * @param string $namedRange 477 * @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope 478 * @return PHPExcel_NamedRange|null 479 */ 480 public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) { 481 $returnValue = null; 482 483 if ($namedRange != '' && ($namedRange !== NULL)) { 484 // first look for global defined name 485 if (isset($this->_namedRanges[$namedRange])) { 486 $returnValue = $this->_namedRanges[$namedRange]; 487 } 488 489 // then look for local defined name (has priority over global defined name if both names exist) 490 if (($pSheet !== NULL) && isset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange])) { 491 $returnValue = $this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange]; 492 } 493 } 494 495 return $returnValue; 496 } 497 498 /** 499 * Remove named range 500 * 501 * @param string $namedRange 502 * @param PHPExcel_Worksheet|null $pSheet Scope: use null for global scope. 503 * @return PHPExcel 504 */ 505 public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) { 506 if ($pSheet === NULL) { 507 if (isset($this->_namedRanges[$namedRange])) { 508 unset($this->_namedRanges[$namedRange]); 509 } 510 } else { 511 if (isset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange])) { 512 unset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange]); 513 } 514 } 515 return $this; 516 } 517 518 /** 519 * Get worksheet iterator 520 * 521 * @return PHPExcel_WorksheetIterator 522 */ 523 public function getWorksheetIterator() { 524 return new PHPExcel_WorksheetIterator($this); 525 } 526 527 /** 528 * Copy workbook (!= clone!) 529 * 530 * @return PHPExcel 531 */ 532 public function copy() { 533 $copied = clone $this; 534 535 $worksheetCount = count($this->_workSheetCollection); 536 for ($i = 0; $i < $worksheetCount; ++$i) { 537 $this->_workSheetCollection[$i] = $this->_workSheetCollection[$i]->copy(); 538 $this->_workSheetCollection[$i]->rebindParent($this); 539 } 540 541 return $copied; 542 } 543 544 /** 545 * Implement PHP __clone to create a deep clone, not just a shallow copy. 546 */ 547 public function __clone() { 548 foreach($this as $key => $val) { 549 if (is_object($val) || (is_array($val))) { 550 $this->{$key} = unserialize(serialize($val)); 551 } 552 } 553 } 554 555 /** 556 * Get the workbook collection of cellXfs 557 * 558 * @return PHPExcel_Style[] 559 */ 560 public function getCellXfCollection() 561 { 562 return $this->_cellXfCollection; 563 } 564 565 /** 566 * Get cellXf by index 567 * 568 * @param int $pIndex 569 * @return PHPExcel_Style 570 */ 571 public function getCellXfByIndex($pIndex = 0) 572 { 573 return $this->_cellXfCollection[$pIndex]; 574 } 575 576 /** 577 * Get cellXf by hash code 578 * 579 * @param string $pValue 580 * @return PHPExcel_Style|false 581 */ 582 public function getCellXfByHashCode($pValue = '') 583 { 584 foreach ($this->_cellXfCollection as $cellXf) { 585 if ($cellXf->getHashCode() == $pValue) { 586 return $cellXf; 587 } 588 } 589 return false; 590 } 591 592 /** 593 * Get default style 594 * 595 * @return PHPExcel_Style 596 * @throws Exception 597 */ 598 public function getDefaultStyle() 599 { 600 if (isset($this->_cellXfCollection[0])) { 601 return $this->_cellXfCollection[0]; 602 } 603 throw new Exception('No default style found for this workbook'); 604 } 605 606 /** 607 * Add a cellXf to the workbook 608 * 609 * @param PHPExcel_Style $style 610 */ 611 public function addCellXf(PHPExcel_Style $style) 612 { 613 $this->_cellXfCollection[] = $style; 614 $style->setIndex(count($this->_cellXfCollection) - 1); 615 } 616 617 /** 618 * Remove cellXf by index. It is ensured that all cells get their xf index updated. 619 * 620 * @param int $pIndex Index to cellXf 621 * @throws Exception 622 */ 623 public function removeCellXfByIndex($pIndex = 0) 624 { 625 if ($pIndex > count($this->_cellXfCollection) - 1) { 626 throw new Exception("CellXf index is out of bounds."); 627 } else { 628 // first remove the cellXf 629 array_splice($this->_cellXfCollection, $pIndex, 1); 630 631 // then update cellXf indexes for cells 632 foreach ($this->_workSheetCollection as $worksheet) { 633 foreach ($worksheet->getCellCollection(false) as $cellID) { 634 $cell = $worksheet->getCell($cellID); 635 $xfIndex = $cell->getXfIndex(); 636 if ($xfIndex > $pIndex ) { 637 // decrease xf index by 1 638 $cell->setXfIndex($xfIndex - 1); 639 } else if ($xfIndex == $pIndex) { 640 // set to default xf index 0 641 $cell->setXfIndex(0); 642 } 643 } 644 } 645 } 646 } 647 648 /** 649 * Get the cellXf supervisor 650 * 651 * @return PHPExcel_Style 652 */ 653 public function getCellXfSupervisor() 654 { 655 return $this->_cellXfSupervisor; 656 } 657 658 /** 659 * Get the workbook collection of cellStyleXfs 660 * 661 * @return PHPExcel_Style[] 662 */ 663 public function getCellStyleXfCollection() 664 { 665 return $this->_cellStyleXfCollection; 666 } 667 668 /** 669 * Get cellStyleXf by index 670 * 671 * @param int $pIndex 672 * @return PHPExcel_Style 673 */ 674 public function getCellStyleXfByIndex($pIndex = 0) 675 { 676 return $this->_cellStyleXfCollection[$pIndex]; 677 } 678 679 /** 680 * Get cellStyleXf by hash code 681 * 682 * @param string $pValue 683 * @return PHPExcel_Style|false 684 */ 685 public function getCellStyleXfByHashCode($pValue = '') 686 { 687 foreach ($this->_cellXfStyleCollection as $cellStyleXf) { 688 if ($cellStyleXf->getHashCode() == $pValue) { 689 return $cellStyleXf; 690 } 691 } 692 return false; 693 } 694 695 /** 696 * Add a cellStyleXf to the workbook 697 * 698 * @param PHPExcel_Style $pStyle 699 */ 700 public function addCellStyleXf(PHPExcel_Style $pStyle) 701 { 702 $this->_cellStyleXfCollection[] = $pStyle; 703 $pStyle->setIndex(count($this->_cellStyleXfCollection) - 1); 704 } 705 706 /** 707 * Remove cellStyleXf by index 708 * 709 * @param int $pIndex 710 * @throws Exception 711 */ 712 public function removeCellStyleXfByIndex($pIndex = 0) 713 { 714 if ($pIndex > count($this->_cellStyleXfCollection) - 1) { 715 throw new Exception("CellStyleXf index is out of bounds."); 716 } else { 717 array_splice($this->_cellStyleXfCollection, $pIndex, 1); 718 } 719 } 720 721 /** 722 * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells 723 * and columns in the workbook 724 */ 725 public function garbageCollect() 726 { 727 // how many references are there to each cellXf ? 728 $countReferencesCellXf = array(); 729 foreach ($this->_cellXfCollection as $index => $cellXf) { 730 $countReferencesCellXf[$index] = 0; 731 } 732 733 foreach ($this->getWorksheetIterator() as $sheet) { 734 735 // from cells 736 foreach ($sheet->getCellCollection(false) as $cellID) { 737 $cell = $sheet->getCell($cellID); 738 ++$countReferencesCellXf[$cell->getXfIndex()]; 739 } 740 741 // from row dimensions 742 foreach ($sheet->getRowDimensions() as $rowDimension) { 743 if ($rowDimension->getXfIndex() !== null) { 744 ++$countReferencesCellXf[$rowDimension->getXfIndex()]; 745 } 746 } 747 748 // from column dimensions 749 foreach ($sheet->getColumnDimensions() as $columnDimension) { 750 ++$countReferencesCellXf[$columnDimension->getXfIndex()]; 751 } 752 } 753 754 // remove cellXfs without references and create mapping so we can update xfIndex 755 // for all cells and columns 756 $countNeededCellXfs = 0; 757 foreach ($this->_cellXfCollection as $index => $cellXf) { 758 if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf 759 ++$countNeededCellXfs; 760 } else { 761 unset($this->_cellXfCollection[$index]); 762 } 763 $map[$index] = $countNeededCellXfs - 1; 764 } 765 $this->_cellXfCollection = array_values($this->_cellXfCollection); 766 767 // update the index for all cellXfs 768 foreach ($this->_cellXfCollection as $i => $cellXf) { 769 $cellXf->setIndex($i); 770 } 771 772 // make sure there is always at least one cellXf (there should be) 773 if (empty($this->_cellXfCollection)) { 774 $this->_cellXfCollection[] = new PHPExcel_Style(); 775 } 776 777 // update the xfIndex for all cells, row dimensions, column dimensions 778 foreach ($this->getWorksheetIterator() as $sheet) { 779 780 // for all cells 781 foreach ($sheet->getCellCollection(false) as $cellID) { 782 $cell = $sheet->getCell($cellID); 783 $cell->setXfIndex( $map[$cell->getXfIndex()] ); 784 } 785 786 // for all row dimensions 787 foreach ($sheet->getRowDimensions() as $rowDimension) { 788 if ($rowDimension->getXfIndex() !== null) { 789 $rowDimension->setXfIndex( $map[$rowDimension->getXfIndex()] ); 790 } 791 } 792 793 // for all column dimensions 794 foreach ($sheet->getColumnDimensions() as $columnDimension) { 795 $columnDimension->setXfIndex( $map[$columnDimension->getXfIndex()] ); 796 } 797 } 798 799 // also do garbage collection for all the sheets 800 foreach ($this->getWorksheetIterator() as $sheet) { 801 $sheet->garbageCollect(); 802 } 803 } 804 805 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |