This commit is contained in:
Sergey Shuchkin 2022-06-01 12:54:28 +06:00
parent 199e5691e7
commit 1b5701a07f
3 changed files with 14 additions and 5 deletions

View File

@ -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) `<bottom>12345</bottom>`

View File

@ -74,11 +74,12 @@ $data = [
['Top', '<style height="50"><top>Top</top></style>'],
['Middle + Center', '<style height="50"><middle><center>Middle + Center</center></middle></style>'],
['Bottom + Right', '<style height="50"><bottom><right>Bottom + Right</right></bottom></style>'],
['<center>MERGE CELLS</center>', null],
['<center>MERGE CELLS MERGE CELLS MERGE CELLS MERGE CELLS MERGE CELLS</center>', null],
];
SimpleXLSXGen::fromArray( $data )
->setDefaultFont( 'Courier New' )
->setDefaultFontSize( 14 )
->setColWidth(1, 35)
->mergeCells('A20:B20')
->saveAs('styles_and_tags.xlsx');
```

View File

@ -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 r="'.$CUR_ROW.'"'.($RH ? ' customHeight="1" ht="'.$RH.'"' : '').'>'.$row . "</row>";
}
foreach ( $COL as $k => $max ) {
$COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.min( $max+1, 60).'" />';
$w = isset($this->sheets[$idx]['colwidth'][$k]) ? $this->sheets[$idx]['colwidth'][$k] : min( $max+1, 60);
$COLS[] = '<col min="'.$k.'" max="'.$k.'" width="'.$w.'" />';
}
$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