From d4c7d5363f2fdf630d4fb8a19467bc9206bf996b Mon Sep 17 00:00:00 2001 From: devOp1 Date: Wed, 7 Dec 2022 17:05:45 +0100 Subject: [PATCH 1/2] Fixed col-numbering on setCellValue to 1-based. --- src/ExcelSheet.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ExcelSheet.php b/src/ExcelSheet.php index 3c997a6..6e9f72d 100644 --- a/src/ExcelSheet.php +++ b/src/ExcelSheet.php @@ -229,7 +229,7 @@ protected function renderTitle() $titles = $this->normalizeIndex($this->getTitles()); if ($titles) { $keys = array_keys($titles); - $col = array_shift($keys); + $col = array_shift($keys) + 1; // Add 1 because PhpSpreadsheet is 1-based foreach ($titles as $title) { $this->_sheet->setCellValueByColumnAndRow($col++, $this->_row, $title); } @@ -265,7 +265,7 @@ protected function renderRows() protected function renderRow($data, $row, $formats, $formatters, $callbacks, $types) { foreach (array_values($data) as $i => $value) { - $col = $i + self::normalizeColumn($this->startColumn); + $col = $i + self::normalizeColumn($this->startColumn) + 1; // Add 1 because PhpSpreadsheet is 1-based if (isset($formatters[$col]) && is_callable($formatters[$col])) { $value = call_user_func($formatters[$col], $value, $row, $data); } From 69e67d990ac499cf4a35a0ea7d452393190f0ba2 Mon Sep 17 00:00:00 2001 From: devOp1 Date: Thu, 8 Dec 2022 10:48:06 +0100 Subject: [PATCH 2/2] Changed normalizeColumn to 1-based column index --- src/ExcelSheet.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ExcelSheet.php b/src/ExcelSheet.php index 6e9f72d..8d9cae7 100644 --- a/src/ExcelSheet.php +++ b/src/ExcelSheet.php @@ -229,7 +229,7 @@ protected function renderTitle() $titles = $this->normalizeIndex($this->getTitles()); if ($titles) { $keys = array_keys($titles); - $col = array_shift($keys) + 1; // Add 1 because PhpSpreadsheet is 1-based + $col = array_shift($keys); foreach ($titles as $title) { $this->_sheet->setCellValueByColumnAndRow($col++, $this->_row, $title); } @@ -265,7 +265,7 @@ protected function renderRows() protected function renderRow($data, $row, $formats, $formatters, $callbacks, $types) { foreach (array_values($data) as $i => $value) { - $col = $i + self::normalizeColumn($this->startColumn) + 1; // Add 1 because PhpSpreadsheet is 1-based + $col = $i + self::normalizeColumn($this->startColumn); if (isset($formatters[$col]) && is_callable($formatters[$col])) { $value = call_user_func($formatters[$col], $value, $row, $data); } @@ -307,12 +307,12 @@ protected function normalizeIndex($data) /** * @param int|string $column the column either as int or as string. If * numeric, the startColumn offset will be added. - * @return int the normalized numeric column index (0-based). + * @return int the normalized numeric column index (1-based). */ public function normalizeColumn($column) { if (is_string($column)) { - return \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($column) - 1; + return \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($column); } else { return $column + self::normalizeColumn($this->startColumn); }