From 7f5c5847724a63c2b9f9f04333e7c2c8ae7d654d Mon Sep 17 00:00:00 2001 From: Sergey Shuchkin Date: Thu, 29 Jul 2021 12:16:10 +0600 Subject: [PATCH] 1.0.20 --- CHANGELOG.md | 4 ++++ src/SimpleXLSXGen.php | 19 ++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ff791a..8da8420 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/SimpleXLSXGen.php b/src/SimpleXLSXGen.php index 67d81b2..f653862 100644 --- a/src/SimpleXLSXGen.php +++ b/src/SimpleXLSXGen.php @@ -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;