How to use applyFromArray() to style individual cells on range of cells?

Topics: User Forum
Mar 22, 2010 at 7:15 PM

Hello everyone who read this!

I'm trying to apply this style to a range of cells, but I'm not getting the result I expected:

 

  $styleArray = array(
      'font' => array(
        'name' => 'Arial',
        'size' => '8',
      ),
      'borders' => array(
        'left' => array(
          'style' => PHPExcel_Style_Border::BORDER_THIN,
        ),
        'right' => array(
          'style' => PHPExcel_Style_Border::BORDER_THIN,
        ),
        'bottom' => array(
          'style' => PHPExcel_Style_Border::BORDER_NONE,
        ), 
      ),
      'fill' => array(
        'type' => PHPExcel_Style_Fill::FILL_SOLID,
        'startcolor' => array(
          'argb' => 'FFFFFFCC',
        ),
      ),
    );

$worksheet->getStyle('A13:J'.$row)->applyFromArray($styleArray);


And it works, except for the borders. What I want is to format all cells in the specified range with a left and right border,
but what I get instead is the whole range of cells treated as a single cell, therefore the borders apply only to the border
of the range of cells.

How can PHPExcel accomplish this? Has anyone done this before? I haven'f found such an option on the API so far...

 

 

 

$styleArray = array(
//      'font' => array(
//        'name' => 'Arial',
//        'size' => '8',
//      ),
//      'borders' => array(
//        'left' => array(
//          'style' => PHPExcel_Style_Border::BORDER_THICK,
//        ),
//        'right' => array(
//          'style' => PHPExcel_Style_Border::BORDER_THICK,
//        ),
//        'bottom' => array(
//          'style' => PHPExcel_Style_Border::BORDER_THICK,
//        ),
//      ),
//      'fill' => array(
//        'type' => PHPExcel_Style_Fill::FILL_SOLID,
//        'startcolor' => array(
//          'argb' => 'FFFFFFCC',
//        ),
//      ),
//    );
Coordinator
Mar 22, 2010 at 9:26 PM

Take a look at the $pAdvanced parameter for the applyFromArray() method, e.g.

$worksheet->getStyle('A13:J'.$row)->applyFromArray($styleArray, False);

 

Mar 23, 2010 at 5:50 PM

Thanks a lot MarkBaker, the first time around I read the method, I did not quite get what the $pAdvanced parameter was about, but know takin a second look at it, it does make more sense.

Really appreciated!

Developer
Mar 26, 2010 at 4:43 AM

Just want to say that you can also use the 'vertical' parameter

$styleArray = array(
'font' => array(
'name' => 'Arial',
'size' => '8',
),
'borders' => array(
'left' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
),
'right' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
),
'vertical' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
),
),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'argb' => 'FFFFFFCC',
),
),
);
$worksheet->getStyle('A13:J'.$row)->applyFromArray($styleArray);