Skip to content

Commit f25a173

Browse files
committed
Fix column index in ActiveExcelSheet
1 parent 133df7a commit f25a173

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/ActiveExcelSheet.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
/**
88
* An excel sheet that is rendered with data from an `ActiveQuery`.
99
* A query must be set with `setQuery()`.
10+
*
11+
* Note that for backwards compatibility this class still uses 0-based column
12+
* indices for its configuration properties while PhpSpreadsheet now is 1-based.
1013
*/
1114
class ActiveExcelSheet extends ExcelSheet
1215
{
@@ -58,7 +61,7 @@ public function getData()
5861
}
5962

6063
/**
61-
* @return string[] list of attributes for the table columns. If no
64+
* @return string[] 0-based list of attributes for the table columns. If no
6265
* attributes are set, attributes are set to `ActiveRecord::attributes()`
6366
* for the main query record.
6467
*/
@@ -71,7 +74,7 @@ public function getAttributes()
7174
}
7275

7376
/**
74-
* @param string[] $value list of attributes for the table columns
77+
* @param string[] $value 0-based list of attributes for the table columns
7578
*/
7679
public function setAttributes($value)
7780
{
@@ -130,8 +133,8 @@ public function getFormats()
130133
{
131134
if ($this->_formats === null) {
132135
$this->_formats = [];
133-
$attrs = $this->normalizeIndex($this->getAttributes());
134-
$schemas = $this->normalizeIndex($this->getColumnSchemas());
136+
$attrs = $this->getAttributes();
137+
$schemas = $this->getColumnSchemas();
135138
foreach ($attrs as $c => $attr) {
136139
if (!isset($schemas[$c])) {
137140
continue;
@@ -176,15 +179,15 @@ public function setFormats($value)
176179

177180
/**
178181
* @return Callable[] the value formatters for the column cells indexed by
179-
* 0-based column index. If not set, the formatters are aut-generated from
182+
* 0-based column index. If not set, the formatters are auto-generated from
180183
* the DB column types.
181184
*/
182185
public function getFormatters()
183186
{
184187
if ($this->_formatters === null) {
185188
$this->_formatters = [];
186-
$attrs = $this->normalizeIndex($this->getAttributes());
187-
$schemas = $this->normalizeIndex($this->getColumnSchemas());
189+
$attrs = $this->getAttributes();
190+
$schemas = $this->getColumnSchemas();
188191
foreach ($attrs as $c => $attr) {
189192
if (!isset($schemas[$c])) {
190193
continue;

src/ExcelSheet.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
/**
77
* An excel worksheet
8+
*
9+
* Note that for backwards compatibility this class still uses 0-based column
10+
* indices for its configuration properties while PhpSpreadsheet now is 1-based.
811
*/
912
class ExcelSheet extends Component
1013
{
@@ -13,7 +16,7 @@ class ExcelSheet extends Component
1316

1417
/**
1518
* @var int|string the start column name or its 0-based index. When this is
16-
* set, the 0-based offset is added to all numeric keys used anywhere in
19+
* set, the offset is added to all numeric column keys used anywhere in
1720
* this class. Columns referenced by name will stay unchanged. Default is
1821
* 'A'.
1922
*/
@@ -290,7 +293,8 @@ protected function renderRow($data, $row, $formats, $formatters, $callbacks, $ty
290293
/**
291294
* @param array $data any data indexed by 0-based colum index or by column name.
292295
* @return array the array with alphanumeric column keys (A, B, C, ...)
293-
* converted to numeric indices
296+
* converted to numeric indices and numeric indices converted to 1-based
297+
* and startColumn added
294298
*/
295299
protected function normalizeIndex($data)
296300
{
@@ -305,7 +309,7 @@ protected function normalizeIndex($data)
305309
}
306310

307311
/**
308-
* @param int|string $column the column either as int or as string. If
312+
* @param int|string $column the column either as 0-based index or as string. If
309313
* numeric, the startColumn offset will be added.
310314
* @return int the normalized numeric column index (1-based).
311315
*/
@@ -314,7 +318,10 @@ public function normalizeColumn($column)
314318
if (is_string($column)) {
315319
return \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($column);
316320
} else {
317-
return $column + self::normalizeColumn($this->startColumn);
321+
$startIndex = is_string($this->startColumn) ?
322+
self::normalizeColumn($this->startColumn) :
323+
($this->startColumn + 1);
324+
return $column + $startIndex;
318325
}
319326
}
320327
}

0 commit comments

Comments
 (0)