Phpspreadsheet: merge cell problem

Created on 28 Sep 2017  路  4Comments  路  Source: PHPOffice/PhpSpreadsheet

Hello

When I use the example below to merge cells by giving variables to the range I want to merge, php reach out of memory ... I tried to increase the memory of php to 8Gb (which is a lot), no help.
If I use values, i.e $spreadsheet->getActiveSheet()->mergeCells('B2:B4');, there is no problem. I need to merge cells based on calculated values, so I don't know beforehand which cells need to be merged. It is possible to do this with variables?
Thank you
Bogdan

require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// Create new Spreadsheet object
//echo $workshop->title;

$spreadsheet = new Spreadsheet();



// Add some data
$spreadsheet->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Workshop id')
        ->setCellValue('B1', 'Workshop title')
        ->setCellValue('C1', 'Management')
        ->setCellValue('D1', 'Location')
        ->setCellValue('E1', 'Organisers')
        ->setCellValue('F1', 'Affiliation')
        ->setCellValue('G1', 'Start')
        ->setCellValue('H1', 'End');

    $index1='2';
    $index2='4';
    $range1='B'.$index1;
    $range2='B'.$index2;
    $spreadsheet->getActiveSheet()->mergeCells('$range1:$range2');

$spreadsheet->getActiveSheet()->setTitle('Workshops lists');


$spreadsheet->setActiveSheetIndex(0);
$writer = new Xlsx($spreadsheet);
$filename="workshops_lists.xlsx";
$writer->save($filename);
$fileloc="/www/".$filename;

Utils::download_file($fileloc);
unlink($fileloc);

Which versions of PhpSpreadsheet and PHP are affected?

I use php5.6.30 and latest develop phpspreadsheet.

Most helpful comment

This is not a PHPSpreadsheet error, it's just you write the wrong php code.
See your code at

$spreadsheet->getActiveSheet()->mergeCells('$range1:$range2');

In PHP, when you want parse $rang1 into variable, you should use "$range1" instead of '$range1',
use single quote would just translate as string, so it can't read by phpspreadsheet.
It would be better to add {} to include the variable, like "{$range1}:{$range2}"

All 4 comments

This is not a PHPSpreadsheet error, it's just you write the wrong php code.
See your code at

$spreadsheet->getActiveSheet()->mergeCells('$range1:$range2');

In PHP, when you want parse $rang1 into variable, you should use "$range1" instead of '$range1',
use single quote would just translate as string, so it can't read by phpspreadsheet.
It would be better to add {} to include the variable, like "{$range1}:{$range2}"

Thanks, it worked!

how can merge more than 2 cells

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->mergeCells('A1:E1');
This should work

Was this page helpful?
0 / 5 - 0 ratings