1
0
mirror of https://github.com/shuchkin/simplexlsxgen.git synced 2023-08-10 21:12:59 +03:00

7 Commits

Author SHA1 Message Date
868cf8edc0 1.0.21 2021-09-03 22:12:48 +06:00
7f5c584772 1.0.20 2021-07-29 12:16:10 +06:00
5b58103e3b 1.0.19 2021-07-28 22:01:02 +06:00
6d49a90fe7 1.0.18 2021-07-28 05:06:23 +06:00
c8a94d552a 1.0.17 2021-07-28 01:45:25 +06:00
c9fa7da689 1.0.16 2021-07-01 03:03:57 +06:00
0d059fdf6e 1.0.16 2021-07-01 03:01:35 +06:00
2 changed files with 48 additions and 7 deletions

View File

@ -1,5 +1,29 @@
# Changelog # Changelog
## 1.0.21 (2021-09-03)
* Fixed saveAs / downloadAs / etc methods more than once
## 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
## 1.0.18 (2021-07-28)
* Fixed email regex
## 1.0.17 (2021-07-28)
* Fixed " and & in sheets names
## 1.0.16 (2021-07-01)
* Fixed "&" in hyperlinks
## 1.0.15 (2021-06-22) ## 1.0.15 (2021-06-22)
* Fixed *mailto* hyperlinks detection * Fixed *mailto* hyperlinks detection

View File

@ -115,9 +115,26 @@ class SimpleXLSXGen {
} }
public function addSheet( array $rows, $name = null ) { public function addSheet( array $rows, $name = null ) {
$this->curSheet++;
$this->sheets[$this->curSheet] = ['name' => $name ?: 'Sheet'.($this->curSheet+1), 'hyperlinks' => []]; $this->curSheet++;
if ( $name === null ) { // autogenerated sheet names
$name = 'Sheet'.($this->curSheet+1);
} else {
$names = [];
foreach( $this->sheets as $sh ) {
$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, 'hyperlinks' => []];
if ( is_array( $rows ) && isset( $rows[0] ) && is_array($rows[0]) ) { if ( is_array( $rows ) && isset( $rows[0] ) && is_array($rows[0]) ) {
$this->sheets[$this->curSheet]['rows'] = $rows; $this->sheets[$this->curSheet]['rows'] = $rows;
@ -217,7 +234,7 @@ class SimpleXLSXGen {
} elseif ( $cfilename === 'xl/workbook.xml' ) { } elseif ( $cfilename === 'xl/workbook.xml' ) {
$s = ''; $s = '';
foreach ( $this->sheets as $k => $v ) { foreach ( $this->sheets as $k => $v ) {
$s .= '<sheet name="' . $v['name'] . '" sheetId="' . ( $k + 1) . '" state="visible" r:id="rId' . ( $k + 2) . '"/>'; $s .= '<sheet name="' . $this->esc( $v['name'] ) . '" sheetId="' . ( $k + 1) . '" state="visible" r:id="rId' . ( $k + 2) . '"/>';
} }
$template = str_replace('{SHEETS}', $s, $template); $template = str_replace('{SHEETS}', $s, $template);
$this->_writeEntry($fh, $cdrec, $cfilename, $template); $this->_writeEntry($fh, $cdrec, $cfilename, $template);
@ -232,8 +249,8 @@ class SimpleXLSXGen {
$this->SI[] = 'No Data'; $this->SI[] = 'No Data';
} }
$si_cnt = count($this->SI); $si_cnt = count($this->SI);
$this->SI = '<si><t>'.implode("</t></si>\r\n<si><t>", $this->SI).'</t></si>'; $si = '<si><t>'.implode("</t></si>\r\n<si><t>", $this->SI).'</t></si>';
$template = str_replace(['{CNT}', '{STRINGS}'], [ $si_cnt, $this->SI ], $template ); $template = str_replace(['{CNT}', '{STRINGS}'], [ $si_cnt, $si ], $template );
$this->_writeEntry($fh, $cdrec, $cfilename, $template); $this->_writeEntry($fh, $cdrec, $cfilename, $template);
$entries++; $entries++;
} elseif ( $cfilename === 'xl/worksheets/sheet1.xml' ) { } elseif ( $cfilename === 'xl/worksheets/sheet1.xml' ) {
@ -250,7 +267,7 @@ class SimpleXLSXGen {
$RH = []; $RH = [];
$filename = 'xl/worksheets/_rels/sheet' . ( $k + 1 ) . '.xml.rels'; $filename = 'xl/worksheets/_rels/sheet' . ( $k + 1 ) . '.xml.rels';
foreach ( $v['hyperlinks'] as $h ) { foreach ( $v['hyperlinks'] as $h ) {
$RH[] = '<Relationship Id="' . $h['ID'] . '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="' . $h['H'] . '" TargetMode="External"/>'; $RH[] = '<Relationship Id="' . $h['ID'] . '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="' . $this->esc($h['H']) . '" TargetMode="External"/>';
} }
$xml = str_replace( '{HYPERLINKS}', implode( "\r\n", $RH ), $template ); $xml = str_replace( '{HYPERLINKS}', implode( "\r\n", $RH ), $template );
$this->_writeEntry( $fh, $cdrec, $filename, $xml ); $this->_writeEntry( $fh, $cdrec, $filename, $xml );
@ -504,7 +521,7 @@ class SimpleXLSXGen {
$h = explode( '#', $v ); $h = explode( '#', $v );
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : '']; $this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => $h[0], 'L' => isset( $h[1] ) ? $h[1] : ''];
$F = self::F_HYPERLINK; // Hyperlink $F = self::F_HYPERLINK; // Hyperlink
} elseif ( preg_match( "/^([a-zA-Z0-9_\.\-]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)$/i", $v ) ) { } elseif ( preg_match( "/^[a-zA-Z0-9_\.\-]+@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/", $v ) ) {
$this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => 'mailto:' . $v, 'L' => '']; $this->sheets[ $idx ]['hyperlinks'][] = ['ID' => 'rId' . ( count( $this->sheets[ $idx ]['hyperlinks'] ) + 1 ), 'R' => $cname, 'H' => 'mailto:' . $v, 'L' => ''];
$F = self::F_HYPERLINK; // Hyperlink $F = self::F_HYPERLINK; // Hyperlink
} }