Skip to content

Commit

Permalink
Merge pull request svrnm#12 from mojoaxel/errorHandling
Browse files Browse the repository at this point in the history
use default sheetName instead of a hard-coded one
  • Loading branch information
svrnm authored Jan 5, 2017
2 parents a2682a2 + ad920a1 commit d3690af
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
10 changes: 6 additions & 4 deletions src/Svrnm/ExcelDataTables/ExcelDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* $excelDataTable->addRows($data)->attachToFile('./example.xlsx');
*
* @author Severin Neumann <severin.neumann@altmuehlnet.de>
* @license Apache-2.0
* @license Apache-2.0
*/
class ExcelDataTable
{
Expand Down Expand Up @@ -300,7 +300,7 @@ public function getHeaders() {
* @return this
*/
public function setColumnType($columnKey, $type) {

throw new \Exception('"setColumnType" is not yet implemented');
return $this;
}

Expand Down Expand Up @@ -462,8 +462,9 @@ public function fillXLSX($srcFilename) {
* @param string $table_name name of the excel table
* @return $this
*/
public function refreshTableRange($table_name = 'Data')
public function refreshTableRange($table_name = null)
{
$table_name = !is_null($table_name) ? $table_name : $this->sheetName;
$this->refreshTableRange = $table_name;
return $this;
}
Expand All @@ -474,8 +475,9 @@ public function refreshTableRange($table_name = 'Data')
* @param string $table_name name of the excel table
* @return $this
*/
public function preserveFormulas($table_name = 'Data')
public function preserveFormulas($table_name)
{
$table_name = !is_null($table_name) ? $table_name : $this->sheetName;
$this->preserveFormulas = $table_name;
return $this;
}
Expand Down
48 changes: 33 additions & 15 deletions src/Svrnm/ExcelDataTables/ExcelWorkbook.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
class ExcelWorkbook implements \Countable
{

/**
* The source filename
*
Expand Down Expand Up @@ -65,6 +64,13 @@ class ExcelWorkbook implements \Countable
*/
protected $autoCalculation = false;

/**
* The default name of the sheet when attachToFile is called
*
* @var string
*/
protected $sheetName = 'Data';

/**
* Instantiate a new object of the type ExcelWorkbook. Expects a filename which
* contains a spreadsheet of type xlsx.
Expand Down Expand Up @@ -317,30 +323,30 @@ protected function dateTimeFormatId() {

/**
* Add a worksheet into the workbook with id $id and name $name. If $id is null the last
* worksheet is replaced. If $name is empty, its default value is 'Data'.
* worksheet is replaced. If $name is empty, its default value is set to the default.
*
* Currently this replaces an existing worksheet. Adding new worksheets is not yet supported
*
* @param ExcelWorksheet $worksheet
* @param int $id
* @param string $name
*/
public function addWorksheet(ExcelWorksheet $worksheet, $id = null, $name = 'Data') {

if ($id === null) {
$id = $this->getSheetIdByName($name);
}
public function addWorksheet(ExcelWorksheet $worksheet, $id = null, $name = null) {
$name = !is_null($name) ? $name : $this->sheetName;
if ($id === null) $id = $this->getSheetIdByName($name);

if(is_null($id) || $id <= 0) {
$lastId = 0;
while($this->getXLSX()->statName('xl/worksheets/sheet'.($lastId+1).'.xml') !== false) {
$lastId++;
}
$id = $lastId + $id;
throw new \Exception('Sheet with name "'.$name.'" not found in file '.$this->targetFilename.'. Appending is not yet implemented.');
/*
// find a unused id in the worksheets
$id = 1;
while($this->getXLSX()->statName('xl/worksheets/sheet'.($id++).'.xml') !== false) {}
*/
}

$old = $this->getXLSX()->getFromName('xl/worksheets/sheet'.$id.'.xml');
if($old === false) {
throw new \Exception('Appending new sheets is not yet implemented: ' . $id .', '. $this->srcFilename.', '.$this->targetFilename);
throw new \Exception('Appending new sheets is not yet implemented: SheetId:' . $id .', SourceFile:'. $this->srcFilename.', TargetFile:'.$this->targetFilename);
} else {
$document = new \DOMDocument();
$document->loadXML($old);
Expand All @@ -367,10 +373,18 @@ public function addWorksheet(ExcelWorksheet $worksheet, $id = null, $name = 'Dat
public function refreshTableRange($tableName, $numRows)
{
$id = $this->getTableIdByName($tableName);
if (is_null($id)) {
throw new \Exception('table "' . $tableName . '" not found');
}

$document = new \DOMDocument();
$document->loadXML($this->getXLSX()->getFromName('xl/tables/table' . $id . '.xml'));

$table = $document->getElementsByTagName('table')->item(0);
if (is_null($table)) {
throw new \Exception('could not read "table" from document; '.$document);
}

$ref = $table->getAttribute('ref');

$nref = preg_replace('/^(\w+\:[A-Z]+)(\d+)$/', '${1}' . $numRows, $ref);
Expand Down Expand Up @@ -440,7 +454,7 @@ protected function openXLSX() {
}
$isOpen = $this->xlsx->open($this->targetFilename);
if($isOpen !== true) {
throw new \Exception('File not valid: '.$this->targetFilename);
throw new \Exception('Could not open file: '.$this->targetFilename.' [ZipArchive error code: '.$isOpen.']');
}
return $this;
}
Expand All @@ -466,7 +480,11 @@ public function save() {
public function getWorkbook() {
if(is_null($this->workbook)) {
$this->workbook = new \DOMDocument();
$this->workbook->loadXML($this->getXLSX()->getFromName('xl/workbook.xml'));
$workbookFile = $this->getXLSX()->getFromName('xl/workbook.xml');
if ($workbookFile === false) {
throw new \Exception('Could not find xl/workbook.xml in "'.$this->targetFilename.'"');
}
$this->workbook->loadXML($workbookFile);
}
return $this->workbook;
}
Expand Down

0 comments on commit d3690af

Please sign in to comment.