Minor tweaks

This commit is contained in:
MarkBaker 2015-12-06 16:08:37 +00:00
parent 6dc0c1f1b6
commit 0025318d7e
19 changed files with 103 additions and 77 deletions

4
Examples/.gitignore vendored
View File

@ -1,3 +1,5 @@
*.xls *.xls
*.xlsx *.xlsx
*.csv
*.jpg
*.pdf

View File

@ -78,6 +78,21 @@ $objPHPExcel->getActiveSheet()
->setWrapText(true); ->setWrapText(true);
$value = "-ValueA\n-Value B\n-Value C";
$objPHPExcel->getActiveSheet()
->setCellValue('A10', $value);
$objPHPExcel->getActiveSheet()
->getRowDimension(10)
->setRowHeight(-1);
$objPHPExcel->getActiveSheet()
->getStyle('A10')
->getAlignment()
->setWrapText(true);
$objPHPExcel->getActiveSheet()
->getStyle('A10')
->setQuotePrefix(true);
// Rename worksheet // Rename worksheet
echo date('H:i:s') , " Rename worksheet" , EOL; echo date('H:i:s') , " Rename worksheet" , EOL;
$objPHPExcel->getActiveSheet() $objPHPExcel->getActiveSheet()

View File

@ -22,7 +22,7 @@
} }
], ],
"require": { "require": {
"php": ">=5.4.0", "php": "^5.4|^7.0",
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-xml": "*", "ext-xml": "*",
"ext-xmlwriter": "*" "ext-xmlwriter": "*"

View File

@ -203,10 +203,10 @@ class Calculation
/** /**
* Epsilon Precision used for comparisons in calculations * Epsilon Precision used for comparisons in calculations
* *
* @var integer * @var float
* *
*/ */
private $delta = 0.0000000000001; private $delta = 0.1e-12;
/** /**
@ -1490,7 +1490,7 @@ class Calculation
'OFFSET' => array( 'OFFSET' => array(
'category' => Calculation\Categories::CATEGORY_LOOKUP_AND_REFERENCE, 'category' => Calculation\Categories::CATEGORY_LOOKUP_AND_REFERENCE,
'functionCall' => '\\PHPExcel\\Calculation\\LookupRef::OFFSET', 'functionCall' => '\\PHPExcel\\Calculation\\LookupRef::OFFSET',
'argumentCount' => '3,5', 'argumentCount' => '3-5',
'passCellReference' => true, 'passCellReference' => true,
'passByReference' => array(true) 'passByReference' => array(true)
), ),
@ -2061,14 +2061,10 @@ class Calculation
); );
private function __construct(Spreadsheet $spreadsheet = null) public function __construct(Spreadsheet $spreadsheet = null)
{ {
$this->delta = 1 * pow(10, 0 - ini_get('precision')); $this->delta = 1 * pow(10, 0 - ini_get('precision'));
if ($spreadsheet !== null) {
self::$spreadsheetSets[$spreadsheet->getID()] = $this;
}
$this->spreadsheet = $spreadsheet; $this->spreadsheet = $spreadsheet;
$this->cyclicReferenceStack = new CalcEngine\CyclicReferenceStack(); $this->cyclicReferenceStack = new CalcEngine\CyclicReferenceStack();
$this->_debugLog = new CalcEngine\Logger($this->cyclicReferenceStack); $this->_debugLog = new CalcEngine\Logger($this->cyclicReferenceStack);
@ -2097,10 +2093,10 @@ class Calculation
public static function getInstance(Spreadsheet $spreadsheet = null) public static function getInstance(Spreadsheet $spreadsheet = null)
{ {
if ($spreadsheet !== null) { if ($spreadsheet !== null) {
if (isset(self::$spreadsheetSets[$spreadsheet->getID()])) { $instance = $workbook->getCalculationEngine();
return self::$spreadsheetSets[$spreadsheet->getID()]; if (isset($instance)) {
return $instance;
} }
return new Calculation($spreadsheet);
} }
if (!isset(self::$instance) || (self::$instance === null)) { if (!isset(self::$instance) || (self::$instance === null)) {
@ -2116,13 +2112,9 @@ class Calculation
* @access public * @access public
* @param Spreadsheet $spreadsheet Injected spreadsheet identifying the instance to unset * @param Spreadsheet $spreadsheet Injected spreadsheet identifying the instance to unset
*/ */
public static function unsetInstance(Spreadsheet $spreadsheet = null) public function __destruct()
{ {
if ($spreadsheet !== null) { $this->workbook = null;
if (isset(self::$spreadsheetSets[$spreadsheet->getID()])) {
unset(self::$spreadsheetSets[$spreadsheet->getID()]);
}
}
} }
/** /**

View File

@ -546,10 +546,8 @@ class Functions
case 'float': case 'float':
case 'integer': case 'integer':
return $value; return $value;
break;
case 'boolean': case 'boolean':
return (integer) $value; return (integer) $value;
break;
case 'string': case 'string':
// Errors // Errors
if ((strlen($value) > 0) && ($value{0} == '#')) { if ((strlen($value) > 0) && ($value{0} == '#')) {

View File

@ -406,11 +406,7 @@ class Excel2003XML extends BaseReader implements IReader
$style_ss = $style->attributes($namespaces['ss']); $style_ss = $style->attributes($namespaces['ss']);
$styleID = (string) $style_ss['ID']; $styleID = (string) $style_ss['ID'];
// echo 'Style ID = '.$styleID.'<br />'; // echo 'Style ID = '.$styleID.'<br />';
if ($styleID == 'Default') { $this->styles[$styleID] = (isset($this->styles['Default'])) ? $this->styles['Default'] : array();
$this->styles['Default'] = array();
} else {
$this->styles[$styleID] = $this->styles['Default'];
}
foreach ($style as $styleType => $styleData) { foreach ($style as $styleType => $styleData) {
$styleAttributes = $styleData->attributes($namespaces['ss']); $styleAttributes = $styleData->attributes($namespaces['ss']);
// echo $styleType.'<br />'; // echo $styleType.'<br />';

View File

@ -339,10 +339,17 @@ class Excel2007 extends BaseReader implements IReader
} }
$fileName = \PHPExcel\Shared\File::realpath($fileName); $fileName = \PHPExcel\Shared\File::realpath($fileName);
// Sadly, some 3rd party xlsx generators don't use consistent case for filenaming
// so we need to load case-insensitively from the zip file
// Apache POI fixes // Apache POI fixes
$contents = $archive->getFromName($fileName); $contents = $archive->getFromIndex(
$archive->locateName($fileName, ZIPARCHIVE::FL_NOCASE)
);
if ($contents === false) { if ($contents === false) {
$contents = $archive->getFromName(substr($fileName, 1)); $contents = $archive->getFromIndex(
$archive->locateName(substr($fileName, 1), ZIPARCHIVE::FL_NOCASE)
);
} }
return $contents; return $contents;

View File

@ -346,10 +346,6 @@ class Chart
} }
} }
if (empty($seriesVal)) {
$seriesVal = null;
}
return array( return array(
'formatCode' => $formatCode, 'formatCode' => $formatCode,
'pointCount' => $pointCount, 'pointCount' => $pointCount,

View File

@ -486,7 +486,7 @@ class HTML extends BaseReader implements IReader
// Create a new DOM object // Create a new DOM object
$dom = new domDocument; $dom = new domDocument;
// Reload the HTML file into the DOM object // Reload the HTML file into the DOM object
$loaded = $dom->loadHTML($this->securityScanFile($pFilename)); $loaded = $dom->loadHTML(mb_convert_encoding($this->securityScanFile($pFilename), 'HTML-ENTITIES', 'UTF-8'));
if ($loaded === false) { if ($loaded === false) {
throw new Exception('Failed to load ', $pFilename, ' as a DOM Document'); throw new Exception('Failed to load ', $pFilename, ' as a DOM Document');
} }

View File

@ -110,17 +110,19 @@ class ZipArchive
*/ */
public function locateName($fileName) public function locateName($fileName)
{ {
$fileName = strtolower($fileName);
$list = $this->zip->listContent(); $list = $this->zip->listContent();
$listCount = count($list); $listCount = count($list);
$list_index = -1; $index = -1;
for ($i = 0; $i < $listCount; ++$i) { for ($i = 0; $i < $listCount; ++$i) {
if (strtolower($list[$i]["filename"]) == strtolower($fileName) || if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
$list_index = $i; $index = $i;
break; break;
} }
} }
return ($list_index > -1); return ($index > -1) ? $index : false;
} }
/** /**
@ -131,36 +133,32 @@ class ZipArchive
*/ */
public function getFromName($fileName) public function getFromName($fileName)
{ {
$list = $this->zip->listContent(); $index = $this->locateName($fileName);
$listCount = count($list);
$list_index = -1; if ($index !== false) {
for ($i = 0; $i < $listCount; ++$i) { $extracted = $this->getFromIndex($index);
if (strtolower($list[$i]["filename"]) == strtolower($fileName) || } else {
strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { $fileName = substr($fileName, 1);
$list_index = $i; $index = $this->locateName($fileName);
break; if ($index === false) {
return false;
} }
$extracted = $this->zip->getFromIndex($index);
} }
$extracted = ""; $contents = $extracted;
if ($list_index != -1) {
$extracted = $this->zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
} else {
$filename = substr($fileName, 1);
$list_index = -1;
for ($i = 0; $i < $listCount; ++$i) {
if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
$list_index = $i;
break;
}
}
$extracted = $this->zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
}
if ((is_array($extracted)) && ($extracted != 0)) { if ((is_array($extracted)) && ($extracted != 0)) {
$contents = $extracted[0]["content"]; $contents = $extracted[0]["content"];
} }
return $contents; return $contents;
} }
public function getFromIndex($index) {
$extracted = $this->zip->extractByIndex($index, PCLZIP_OPT_EXTRACT_AS_STRING);
$contents = '';
if ((is_array($extracted)) && ($extracted != 0)) {
$contents = $extracted[0]["content"];
}
}
} }

View File

@ -360,7 +360,7 @@ class Spreadsheet
public function __construct() public function __construct()
{ {
$this->uniqueID = uniqid(); $this->uniqueID = uniqid();
$this->calculationEngine = Calculation::getInstance($this); $this->calculationEngine = new Calculation($this);
// Initialise worksheet collection and add one worksheet // Initialise worksheet collection and add one worksheet
$this->workSheetCollection = array(); $this->workSheetCollection = array();
@ -391,7 +391,7 @@ class Spreadsheet
*/ */
public function __destruct() public function __destruct()
{ {
Calculation::unsetInstance($this); $this->calculationEngine = null;
$this->disconnectWorksheets(); $this->disconnectWorksheets();
} }

View File

@ -863,10 +863,10 @@ class Worksheet implements IComparable
$this->title = $pValue; $this->title = $pValue;
$this->dirty = true; $this->dirty = true;
if ($this->parent) { if ($this->parent && $this->parent->getCalculationEngine()) {
// New title // New title
$newTitle = $this->getTitle(); $newTitle = $this->getTitle();
Calculation::getInstance($this->parent) $this->parent->getCalculationEngine()
->renameCalculationCacheForWorksheet($oldTitle, $newTitle); ->renameCalculationCacheForWorksheet($oldTitle, $newTitle);
if ($updateFormulaCellReferences) { if ($updateFormulaCellReferences) {
ReferenceHelper::getInstance()->updateNamedFormulas($this->parent, $oldTitle, $newTitle); ReferenceHelper::getInstance()->updateNamedFormulas($this->parent, $oldTitle, $newTitle);

View File

@ -66,7 +66,7 @@ class ColumnIterator implements \Iterator
* @param string $startColumn The column address at which to start iterating * @param string $startColumn The column address at which to start iterating
* @param string $endColumn Optionally, the column address at which to stop iterating * @param string $endColumn Optionally, the column address at which to stop iterating
*/ */
public function __construct(\PHPExcel\Worksheet $subject = null, $startColumn = 'A', $endColumn = null) public function __construct(\PHPExcel\Worksheet $subject, $startColumn = 'A', $endColumn = null)
{ {
// Set subject // Set subject
$this->subject = $subject; $this->subject = $subject;
@ -87,11 +87,19 @@ class ColumnIterator implements \Iterator
* *
* @param integer $startColumn The column address at which to start iterating * @param integer $startColumn The column address at which to start iterating
* @return ColumnIterator * @return ColumnIterator
* @throws PHPExcel_Exception
*/ */
public function resetStart($startColumn = 'A') public function resetStart($startColumn = 'A')
{ {
$startColumnIndex = \PHPExcel\Cell::columnIndexFromString($startColumn) - 1; $startColumnIndex = \PHPExcel\Cell::columnIndexFromString($startColumn) - 1;
if ($startColumnIndex > PHPExcel_Cell::columnIndexFromString($this->subject->getHighestColumn()) - 1) {
throw new PHPExcel_Exception("Start column ({$startColumn}) is beyond highest column ({$this->subject->getHighestColumn()})");
}
$this->startColumn = $startColumnIndex; $this->startColumn = $startColumnIndex;
if ($this->endColumn < $this->startColumn) {
$this->endColumn = $this->startColumn;
}
$this->seek($startColumn); $this->seek($startColumn);
return $this; return $this;

View File

@ -66,7 +66,7 @@ class RowIterator implements \Iterator
* @param integer $startRow The row number at which to start iterating * @param integer $startRow The row number at which to start iterating
* @param integer $endRow Optionally, the row number at which to stop iterating * @param integer $endRow Optionally, the row number at which to stop iterating
*/ */
public function __construct(\PHPExcel\Worksheet $subject = null, $startRow = 1, $endRow = null) public function __construct(\PHPExcel\Worksheet $subject, $startRow = 1, $endRow = null)
{ {
// Set subject // Set subject
$this->subject = $subject; $this->subject = $subject;
@ -87,10 +87,18 @@ class RowIterator implements \Iterator
* *
* @param integer $startRow The row number at which to start iterating * @param integer $startRow The row number at which to start iterating
* @return RowIterator * @return RowIterator
* @throws PHPExcel_Exception
*/ */
public function resetStart($startRow = 1) public function resetStart($startRow = 1)
{ {
if ($startRow > $this->subject->getHighestRow()) {
throw new PHPExcel_Exception("Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})");
}
$this->startRow = $startRow; $this->startRow = $startRow;
if ($this->endRow < $this->startRow) {
$this->endRow = $this->startRow;
}
$this->seek($startRow); $this->seek($startRow);
return $this; return $this;

View File

@ -290,7 +290,7 @@ class Excel2007 extends BaseWriter implements IWriter
$charts = $this->spreadSheet->getSheet($i)->getChartCollection(); $charts = $this->spreadSheet->getSheet($i)->getChartCollection();
if (count($charts) > 0) { if (count($charts) > 0) {
foreach ($charts as $chart) { foreach ($charts as $chart) {
$objZip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart)); $objZip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart, $this->preCalculateFormulas));
$chartCount++; $chartCount++;
} }
} }

View File

@ -28,6 +28,8 @@ namespace PHPExcel\Writer\Excel2007;
*/ */
class Chart extends WriterPart class Chart extends WriterPart
{ {
protected $calculateCellValues;
/** /**
* Write charts to XML format * Write charts to XML format
* *
@ -36,8 +38,10 @@ class Chart extends WriterPart
* @return string XML Output * @return string XML Output
* @throws \PHPExcel\Writer\Exception * @throws \PHPExcel\Writer\Exception
*/ */
public function writeChart(PHPExcel_Chart $pChart = null) public function writeChart(PHPExcel_Chart $pChart = null, $calculateCellValues = true)
{ {
$this->calculateCellValues = $calculateCellValues;
// Create XML writer // Create XML writer
$objWriter = null; $objWriter = null;
if ($this->getParentWriter()->getUseDiskCaching()) { if ($this->getParentWriter()->getUseDiskCaching()) {
@ -46,7 +50,9 @@ class Chart extends WriterPart
$objWriter = new \PHPExcel\Shared\XMLWriter(\PHPExcel\Shared\XMLWriter::STORAGE_MEMORY); $objWriter = new \PHPExcel\Shared\XMLWriter(\PHPExcel\Shared\XMLWriter::STORAGE_MEMORY);
} }
// Ensure that data series values are up-to-date before we save // Ensure that data series values are up-to-date before we save
if ($this->calculateCellValues) {
$pChart->refresh(); $pChart->refresh();
}
// XML header // XML header
$objWriter->startDocument('1.0', 'UTF-8', 'yes'); $objWriter->startDocument('1.0', 'UTF-8', 'yes');

View File

@ -89,7 +89,7 @@ class Rels extends WriterPart
); );
// a custom UI in workbook ? // a custom UI in workbook ?
if ($pPHPExcel->hasRibbon()) { if ($pPHPExcel->hasRibbon()) {
$this->_writeRelationShip( $this->writeRelationShip(
$objWriter, $objWriter,
5, 5,
'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility', 'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility',
@ -163,7 +163,7 @@ class Rels extends WriterPart
// Relationships for vbaProject if needed // Relationships for vbaProject if needed
// id : just after the last sheet // id : just after the last sheet
if ($pPHPExcel->hasMacros()) { if ($pPHPExcel->hasMacros()) {
$this->_writeRelationShip( $this->writeRelationShip(
$objWriter, $objWriter,
($i + 1 + 3), ($i + 1 + 3),
'http://schemas.microsoft.com/office/2006/relationships/vbaProject', 'http://schemas.microsoft.com/office/2006/relationships/vbaProject',