From 1b5701a07fcf75d1de183a73bddff08e509c353a Mon Sep 17 00:00:00 2001 From: Sergey Shuchkin Date: Wed, 1 Jun 2022 12:54:28 +0600 Subject: [PATCH] 1.2.13 --- CHANGELOG.md | 3 +++ README.md | 3 ++- src/SimpleXLSXGen.php | 13 +++++++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b615cbc..10e6613 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 1.2.13 (2022-06-01) +* setColWidth(num_col_started_1, size_in_chars) - set column width + ## 1.2.12 (2022-05-17) * Vertical align (tags top,middle,bottom) `12345` diff --git a/README.md b/README.md index 2a62d35..0ca4201 100644 --- a/README.md +++ b/README.md @@ -74,11 +74,12 @@ $data = [ ['Top', ''], ['Middle + Center', ''], ['Bottom + Right', ''], - ['
MERGE CELLS
', null], + ['
MERGE CELLS MERGE CELLS MERGE CELLS MERGE CELLS MERGE CELLS
', null], ]; SimpleXLSXGen::fromArray( $data ) ->setDefaultFont( 'Courier New' ) ->setDefaultFontSize( 14 ) + ->setColWidth(1, 35) ->mergeCells('A20:B20') ->saveAs('styles_and_tags.xlsx'); ``` diff --git a/src/SimpleXLSXGen.php b/src/SimpleXLSXGen.php index 7be540d..7a3b271 100644 --- a/src/SimpleXLSXGen.php +++ b/src/SimpleXLSXGen.php @@ -54,7 +54,7 @@ class SimpleXLSXGen { public function __construct() { $this->curSheet = -1; $this->defaultFont = 'Calibri'; - $this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [] ] ]; + $this->sheets = [ ['name' => 'Sheet1', 'rows' => [], 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => [] ] ]; $this->SI = []; // sharedStrings index $this->SI_KEYS = []; // & keys $this->XF = [ // styles @@ -156,7 +156,7 @@ class SimpleXLSXGen { } } - $this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => [], 'mergecells' => []]; + $this->sheets[$this->curSheet] = ['name' => $name, 'hyperlinks' => [], 'mergecells' => [], 'colwidth' => []]; if ( isset( $rows[0] ) && is_array($rows[0]) ) { $this->sheets[$this->curSheet]['rows'] = $rows; @@ -601,7 +601,7 @@ class SimpleXLSXGen { $cv = $this->date2excel( $m[3], $m[2], $m[1], $m[4], $m[5], $m[6] ); $N = self::N_DATETIME; // [22] m/d/yy h:mm } elseif ( preg_match( '/^[0-9+-.]+$/', $v ) ) { // Long ? - $A += self::A_RIGHT; + $A += ($A & (self::A_LEFT | self::A_CENTER)) ? 0 : self::A_RIGHT; } elseif ( preg_match( '/^https?:\/\/\S+$/i', $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] : '']; @@ -682,7 +682,8 @@ class SimpleXLSXGen { $ROWS[] = ''.$row . ""; } foreach ( $COL as $k => $max ) { - $COLS[] = ''; + $w = isset($this->sheets[$idx]['colwidth'][$k]) ? $this->sheets[$idx]['colwidth'][$k] : min( $max+1, 60); + $COLS[] = ''; } $COLS[] = ''; $REF = 'A1:'.$this->num2name(count($COL)) . $CUR_ROW; @@ -766,6 +767,10 @@ class SimpleXLSXGen { $this->sheets[$this->curSheet]['mergecells'][] = $range; return $this; } + public function setColWidth($col, $width) { + $this->sheets[$this->curSheet]['colwidth'][$col] = $width; + return $this; + } public function esc( $str ) { // XML UTF-8: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] // but we use fast version