Additional validation on Worksheet name when adding/creating a new worksheet
git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@90941 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
parent
167fef9d67
commit
5f301a72c1
|
@ -205,6 +205,17 @@ class PHPExcel
|
||||||
return $newSheet;
|
return $newSheet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chech if a sheet with a specified name already exists
|
||||||
|
*
|
||||||
|
* @param string $pSheetName Name of the worksheet to check
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function sheetNameExists($pSheetName)
|
||||||
|
{
|
||||||
|
return ($this->getSheetByName($pSheetName) !== NULL);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add sheet
|
* Add sheet
|
||||||
*
|
*
|
||||||
|
@ -213,8 +224,12 @@ class PHPExcel
|
||||||
* @return PHPExcel_Worksheet
|
* @return PHPExcel_Worksheet
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function addSheet(PHPExcel_Worksheet $pSheet = null, $iSheetIndex = null)
|
public function addSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null)
|
||||||
{
|
{
|
||||||
|
if ($this->sheetNameExists($pSheet->getTitle())) {
|
||||||
|
throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first.");
|
||||||
|
}
|
||||||
|
|
||||||
if($iSheetIndex === NULL) {
|
if($iSheetIndex === NULL) {
|
||||||
$this->_workSheetCollection[] = $pSheet;
|
$this->_workSheetCollection[] = $pSheet;
|
||||||
} else {
|
} else {
|
||||||
|
@ -420,7 +435,7 @@ class PHPExcel
|
||||||
* @return PHPExcel_Worksheet
|
* @return PHPExcel_Worksheet
|
||||||
*/
|
*/
|
||||||
public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null) {
|
public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null) {
|
||||||
if ($this->getSheetByName($pSheet->getTitle()) !== NULL) {
|
if ($this->sheetNameExists($pSheet->getTitle())) {
|
||||||
throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
|
throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
PHPExcel_Autoloader::Register();
|
PHPExcel_Autoloader::Register();
|
||||||
|
// As we always try to run the autoloader before anything else, we can use it to do a few
|
||||||
|
// simple checks and initialisations
|
||||||
PHPExcel_Shared_ZipStreamWrapper::register();
|
PHPExcel_Shared_ZipStreamWrapper::register();
|
||||||
// check mbstring.func_overload
|
// check mbstring.func_overload
|
||||||
if (ini_get('mbstring.func_overload') & 2) {
|
if (ini_get('mbstring.func_overload') & 2) {
|
||||||
|
@ -68,16 +70,16 @@ class PHPExcel_Autoloader
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
$pObjectFilePath = PHPEXCEL_ROOT .
|
$pClassFilePath = PHPEXCEL_ROOT .
|
||||||
str_replace('_',DIRECTORY_SEPARATOR,$pClassName) .
|
str_replace('_',DIRECTORY_SEPARATOR,$pClassName) .
|
||||||
'.php';
|
'.php';
|
||||||
|
|
||||||
if ((file_exists($pObjectFilePath) === false) || (is_readable($pObjectFilePath) === false)) {
|
if ((file_exists($pClassFilePath) === false) || (is_readable($pClassFilePath) === false)) {
|
||||||
// Can't load
|
// Can't load
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
require($pObjectFilePath);
|
require($pClassFilePath);
|
||||||
} // function Load()
|
} // function Load()
|
||||||
|
|
||||||
}
|
}
|
|
@ -70,6 +70,7 @@ class PHPExcel_IOFactory
|
||||||
'OOCalc',
|
'OOCalc',
|
||||||
'SYLK',
|
'SYLK',
|
||||||
'Gnumeric',
|
'Gnumeric',
|
||||||
|
'HTML',
|
||||||
'CSV',
|
'CSV',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -136,7 +137,6 @@ class PHPExcel_IOFactory
|
||||||
foreach (self::$_searchLocations as $searchLocation) {
|
foreach (self::$_searchLocations as $searchLocation) {
|
||||||
if ($searchLocation['type'] == $searchType) {
|
if ($searchLocation['type'] == $searchType) {
|
||||||
$className = str_replace('{0}', $writerType, $searchLocation['class']);
|
$className = str_replace('{0}', $writerType, $searchLocation['class']);
|
||||||
$classFile = str_replace('{0}', $writerType, $searchLocation['path']);
|
|
||||||
|
|
||||||
$instance = new $className($phpExcel);
|
$instance = new $className($phpExcel);
|
||||||
if ($instance !== NULL) {
|
if ($instance !== NULL) {
|
||||||
|
@ -166,7 +166,6 @@ class PHPExcel_IOFactory
|
||||||
foreach (self::$_searchLocations as $searchLocation) {
|
foreach (self::$_searchLocations as $searchLocation) {
|
||||||
if ($searchLocation['type'] == $searchType) {
|
if ($searchLocation['type'] == $searchType) {
|
||||||
$className = str_replace('{0}', $readerType, $searchLocation['class']);
|
$className = str_replace('{0}', $readerType, $searchLocation['class']);
|
||||||
$classFile = str_replace('{0}', $readerType, $searchLocation['path']);
|
|
||||||
|
|
||||||
$instance = new $className();
|
$instance = new $className();
|
||||||
if ($instance !== NULL) {
|
if ($instance !== NULL) {
|
||||||
|
@ -184,7 +183,7 @@ class PHPExcel_IOFactory
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $pFileName
|
* @param string $pFileName The name of the spreadsheet file
|
||||||
* @return PHPExcel
|
* @return PHPExcel
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
|
@ -198,7 +197,7 @@ class PHPExcel_IOFactory
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $pFileName
|
* @param string $pFileName The name of the spreadsheet file to identify
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
|
@ -215,7 +214,7 @@ class PHPExcel_IOFactory
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $pFileName
|
* @param string $pFileName The name of the spreadsheet file
|
||||||
* @return PHPExcel_Reader_IReader
|
* @return PHPExcel_Reader_IReader
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
|
@ -227,23 +226,27 @@ class PHPExcel_IOFactory
|
||||||
if (isset($pathinfo['extension'])) {
|
if (isset($pathinfo['extension'])) {
|
||||||
switch (strtolower($pathinfo['extension'])) {
|
switch (strtolower($pathinfo['extension'])) {
|
||||||
case 'xlsx':
|
case 'xlsx':
|
||||||
$reader = self::createReader('Excel2007');
|
$extensionType = 'Excel2007';
|
||||||
break;
|
break;
|
||||||
case 'xls':
|
case 'xls':
|
||||||
case 'xlsm':
|
case 'xlsm':
|
||||||
$reader = self::createReader('Excel5');
|
$extensionType = 'Excel5';
|
||||||
break;
|
break;
|
||||||
case 'ods':
|
case 'ods':
|
||||||
$reader = self::createReader('OOCalc');
|
$extensionType = 'OOCalc';
|
||||||
break;
|
break;
|
||||||
case 'slk':
|
case 'slk':
|
||||||
$reader = self::createReader('SYLK');
|
$extensionType = 'SYLK';
|
||||||
break;
|
break;
|
||||||
case 'xml':
|
case 'xml':
|
||||||
$reader = self::createReader('Excel2003XML');
|
$extensionType = 'Excel2003XML';
|
||||||
break;
|
break;
|
||||||
case 'gnumeric':
|
case 'gnumeric':
|
||||||
$reader = self::createReader('Gnumeric');
|
$extensionType = 'Gnumeric';
|
||||||
|
break;
|
||||||
|
case 'htm':
|
||||||
|
case 'html':
|
||||||
|
$extensionType = 'HTML';
|
||||||
break;
|
break;
|
||||||
case 'csv':
|
case 'csv':
|
||||||
// Do nothing
|
// Do nothing
|
||||||
|
@ -254,6 +257,7 @@ class PHPExcel_IOFactory
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$reader = self::createReader($extensionType);
|
||||||
// Let's see if we are lucky
|
// Let's see if we are lucky
|
||||||
if (isset($reader) && $reader->canRead($pFilename)) {
|
if (isset($reader) && $reader->canRead($pFilename)) {
|
||||||
return $reader;
|
return $reader;
|
||||||
|
@ -262,14 +266,17 @@ class PHPExcel_IOFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we reach here then "lucky guess" didn't give any result
|
// If we reach here then "lucky guess" didn't give any result
|
||||||
|
// Try walking through all the options in self::$_autoResolveClasses
|
||||||
// Try loading using self::$_autoResolveClasses
|
|
||||||
foreach (self::$_autoResolveClasses as $autoResolveClass) {
|
foreach (self::$_autoResolveClasses as $autoResolveClass) {
|
||||||
$reader = self::createReader($autoResolveClass);
|
// Ignore our original guess, we know that won't work
|
||||||
if ($reader->canRead($pFilename)) {
|
if ($reader !== $extensionType) {
|
||||||
return $reader;
|
$reader = self::createReader($autoResolveClass);
|
||||||
|
if ($reader->canRead($pFilename)) {
|
||||||
|
return $reader;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new Exception('Unable to identify a reader for this file');
|
||||||
} // function createReaderForFile()
|
} // function createReaderForFile()
|
||||||
}
|
}
|
||||||
|
|
|
@ -790,39 +790,43 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
|
||||||
// Old title
|
// Old title
|
||||||
$oldTitle = $this->getTitle();
|
$oldTitle = $this->getTitle();
|
||||||
|
|
||||||
// Is there already such sheet name?
|
if ($this->getParent()) {
|
||||||
if ($this->getParent()->getSheetByName($pValue)) {
|
// Is there already such sheet name?
|
||||||
// Use name, but append with lowest possible integer
|
if ($this->getParent()->sheetNameExists($pValue)) {
|
||||||
|
// Use name, but append with lowest possible integer
|
||||||
|
|
||||||
if (PHPExcel_Shared_String::CountCharacters($pValue) > 29) {
|
if (PHPExcel_Shared_String::CountCharacters($pValue) > 29) {
|
||||||
$pValue = PHPExcel_Shared_String::Substring($pValue,0,29);
|
$pValue = PHPExcel_Shared_String::Substring($pValue,0,29);
|
||||||
}
|
}
|
||||||
$i = 1;
|
$i = 1;
|
||||||
while ($this->getParent()->getSheetByName($pValue . ' ' . $i)) {
|
while ($this->getParent()->sheetNameExists($pValue . ' ' . $i)) {
|
||||||
++$i;
|
++$i;
|
||||||
if ($i == 10) {
|
if ($i == 10) {
|
||||||
if (PHPExcel_Shared_String::CountCharacters($pValue) > 28) {
|
if (PHPExcel_Shared_String::CountCharacters($pValue) > 28) {
|
||||||
$pValue = PHPExcel_Shared_String::Substring($pValue,0,28);
|
$pValue = PHPExcel_Shared_String::Substring($pValue,0,28);
|
||||||
}
|
}
|
||||||
} elseif ($i == 100) {
|
} elseif ($i == 100) {
|
||||||
if (PHPExcel_Shared_String::CountCharacters($pValue) > 27) {
|
if (PHPExcel_Shared_String::CountCharacters($pValue) > 27) {
|
||||||
$pValue = PHPExcel_Shared_String::Substring($pValue,0,27);
|
$pValue = PHPExcel_Shared_String::Substring($pValue,0,27);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$altTitle = $pValue . ' ' . $i;
|
$altTitle = $pValue . ' ' . $i;
|
||||||
return $this->setTitle($altTitle,$updateFormulaCellReferences);
|
return $this->setTitle($altTitle,$updateFormulaCellReferences);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set title
|
// Set title
|
||||||
$this->_title = $pValue;
|
$this->_title = $pValue;
|
||||||
$this->_dirty = true;
|
$this->_dirty = true;
|
||||||
|
|
||||||
// New title
|
if ($this->getParent()) {
|
||||||
$newTitle = $this->getTitle();
|
// New title
|
||||||
if ($updateFormulaCellReferences)
|
$newTitle = $this->getTitle();
|
||||||
PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->getParent(), $oldTitle, $newTitle);
|
if ($updateFormulaCellReferences)
|
||||||
|
PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->getParent(), $oldTitle, $newTitle);
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue