3

we can apply a style on a range of cells like this

$objPHPExcel->getActiveSheet()->duplicateStyleArray($array_of_style,"A1:D1");

But I want to apply same style to a range of cells on their column and row reference like

(3,4,7,7); 

Please help me on this. I am not a newbie on phpexcel but could not find any method to apply a style on range given in col & row index.

Eonasdan
  • 7,563
  • 8
  • 55
  • 82
Pravin
  • 31
  • 1
  • 1
  • 3
  • You could write a simple wrapper function that converted (3,4,7,7) to the appropriate cell range using the PHPExcel_Cell::stringFromColumnIndex() to convert the column index – Mark Baker Oct 25 '11 at 12:51

1 Answers1

6
function duplicateStyleArrayByColumnAndRow( PHPExcel $objPHPExcel, 
                                            $styleArray = array(), 
                                            $fromRow = 1, 
                                            $fromCol = 0, 
                                            $toRow = 1, 
                                            $toCol = 0
                                          )
{
    if ($fromRow > $toRow) {
        $r = $fromRow; $fromRow = $toRow; $toRow = $r;
    }
    if ($fromCol > $toCol) {
        $c = $fromCol; $fromCol = $toCol; $toCol = $c;
    }

    $fromCell = PHPExcel_Cell::stringFromColumnIndex($fromCol) . $fromRow;
    $toCell = PHPExcel_Cell::stringFromColumnIndex($toCol) . $toRow;

    $cellRange = $fromCell . ':' . $toCell;
    if ($fromCell === $toCell) {
        $cellRange = $fromCell;
    }

    return $objPHPExcel->getActiveSheet()->duplicateStyleArray($styleArray,$cellRange);
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • 1
    `$r = $fromRow; $fromRow = $toRow; $toRow = $r;` could be better written as `list($fromRow,$toRow) = array($toRow,$fromRow);` I personally always like this to be better handled as a function to make it more obvious what's happening: `function transpose_vars(&$a,&$b){list($b,$a)=array($a,$b);}` – scragar Nov 12 '12 at 14:28