Skip to content
This repository has been archived by the owner on Jan 2, 2019. It is now read-only.

Search style by identity in PHPExcel_Worksheet::duplicateStyle() #84

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Classes/PHPExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,17 @@ public function getCellXfByHashCode($pValue = '')
return false;
}

/**
* Check if style exists in style collection
*
* @param PHPExcel_Style $style
* @return boolean
*/
public function cellXfExists($pCellStyle = null)
{
return in_array($pCellStyle, $this->_cellXfCollection, true);
}

/**
* Get default style
*
Expand Down
6 changes: 3 additions & 3 deletions Classes/PHPExcel/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1454,9 +1454,9 @@ public function duplicateStyle(PHPExcel_Style $pCellStyle = null, $pRange = '')

// Add the style to the workbook if necessary
$workbook = $this->_parent;
if ($existingStyle = $this->_parent->getCellXfByHashCode($pCellStyle->getHashCode())) {
// there is already such cell Xf in our collection
$xfIndex = $existingStyle->getIndex();
if ($this->_parent->cellXfExists($pCellStyle)) {
// there is already this cell Xf in our collection
$xfIndex = $pCellStyle->getIndex();
} else {
// we don't have such a cell Xf, need to add
$workbook->addCellXf($pCellStyle);
Expand Down
51 changes: 51 additions & 0 deletions Examples/40duplicateStyle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

date_default_timezone_set('Europe/London');

/** Include PHPExcel */
require_once '../Classes/PHPExcel.php';

echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel();
$worksheet = $objPHPExcel->getActiveSheet();

echo date('H:i:s') , " Create styles array" , EOL;
$styles = array();
for ($i = 0; $i < 10; $i++) {
$style = new PHPExcel_Style();
$style->getFont()->setSize($i + 4);
$styles[] = $style;
}

echo date('H:i:s') , " Add data (begin)" , EOL;
$t = microtime(true);
for ($col = 0; $col < 50; $col++) {
for ($row = 0; $row < 100; $row++) {
$str = ($row + $col);
$style = $styles[$row % 10];
$coord = PHPExcel_Cell::stringFromColumnIndex($col) . ($row + 1);
$worksheet->setCellValue($coord, $str);
$worksheet->duplicateStyle($style, $coord);
}
}
$d = microtime(true) - $t;
echo date('H:i:s') , " Add data (end), time: " . round($d, 2) . " s", EOL;


echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;


echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;

echo date('H:i:s') , " Done writing file" , EOL;
echo 'File has been created in ' , getcwd() , EOL;