This commit is contained in:
Sergey Shuchkin 2021-07-29 12:16:10 +06:00
parent 5b58103e3b
commit 7f5c584772
2 changed files with 18 additions and 5 deletions

View File

@ -1,5 +1,9 @@
# Changelog
## 1.0.20 (2021-07-29)
* Fixed sheet names duplicates (Page, Page (1), Page (2)...)
## 1.0.19 (2021-07-28)
* Fixed sheet names duplicates

View File

@ -115,17 +115,26 @@ class SimpleXLSXGen {
}
public function addSheet( array $rows, $name = null ) {
$this->curSheet++;
if ( $name !== null ) {
$this->curSheet++;
if ( $name === null ) { // autogenerated sheet names
$name = 'Sheet'.($this->curSheet+1);
} else {
$names = [];
foreach( $this->sheets as $sh ) {
if ( $name === $sh['name'] ) {
$name .= ' ' . mt_rand( 100000, 999999 );
$names[ strtoupper( $sh['name']) ] = 1;
}
for( $i = 0; $i < 100; $i++ ) {
$new_name = ($i === 0) ? $name : $name .' ('.$i.')';
$NEW_NAME = strtoupper( $new_name );
if ( !isset( $names[ $NEW_NAME ]) ) {
$name = $new_name;
break;
}
}
}
$this->sheets[$this->curSheet] = ['name' => $name ?: 'Sheet'.($this->curSheet+1), 'hyperlinks' => []];
$this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => []];
if ( is_array( $rows ) && isset( $rows[0] ) && is_array($rows[0]) ) {
$this->sheets[$this->curSheet]['rows'] = $rows;