5

I'm trying to read a merged cell in phpexcel, my problem is that the cell is merged on A:B:C:D:E:F ( can't argue with what they want )

$i= 0;
foreach ($worksheet->getRowIterator() as $row) {
    if ($i > 10) break ;
    $i ++;
    $cellIterator = $row->getCellIterator();
    foreach ($cellIterator as $cell) {
        if($cell->getColumn()== 'A:B:C:D:E:F'){
            $specification=$cell->getCalculatedValue();
            var_dump($specification);die();
// some other code

always dumps null.

I've tried $cell->getColumn()== 'A' since that the cell starts at A, but dumps null as well.

I would appreciate any help.

Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
Seyes Radhouene
  • 143
  • 1
  • 10

1 Answers1

7

I don't understand exactly what you're trying to do here, because getColumn() will only ever return a single column address like A or B and never anything like 'A:B:C:D:E:F'

It may be sensible to iterate only existing cells using

$cellIterator->setIterateOnlyExistingCells(true);

but there's a couple of functions that may help you with merged cells:

$cell->isInMergeRange()

will return a Boolean true/false, indicating if the cell is part of a merge range

and

$cell->isMergeRangeValueCell()

can be used to test whether a cell within a merge range is the top-left (primary) cell of that range, and will return a Boolean true/false indicating whether it holds the actual data value/type etc for that range... Note that it will return a false if the cell isn't part of any merge range

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • thanks, but the file is an xls template downloaded, filled and then uploaded to my site, therefore my main problem is how to read the merged cell ? Mr @Mark Baker – Seyes Radhouene Jul 14 '15 at 11:41
  • 2
    You need to identify the top-left cell of a merge range.... that's the only cell that actually exists normally, that's the cell which holds the data value: if you have a merge range A1:B2, then only cell A1 actually exists, A2, B1 and B2 simply don't exist, and while PHPExcel will let you access cells A2, B1 and B2 (if you absolutely insist on doing so), they don't hold any value – Mark Baker Jul 14 '15 at 11:42
  • thank you, i understand now what I need to do, I appreciate it – Seyes Radhouene Jul 14 '15 at 11:46
  • @Mark Baker: Could you tell me which version of PhpExcel got this function? My version is 1.8, but when I call the function, it cannot be foundd – Hoàng Long Jan 05 '17 at 07:17
  • 1
    These methods have been part of PHPExcel for two years now – Mark Baker Jan 05 '17 at 07:58