Reputation: 381
I´m new on Laravel and I´m trying to set an especific background color to an excel row if a condition is satisfied.
Now I´m able to create the excel without any problem, here is the code if you want to check:
Excel::create("Performance_Agentes_Report", function($excel) use ($salida)
{
$excel->sheet("Hoja 1", function($sheet) use ($salida)
{
$sheet->fromModel(@$salida, null, "", false, true);
});
})->store('xls', storage_path('exports'));
the array I´m printing is something like this:
Array
(
[0] => Array
(
[Agente] => GN Victor B
[Antigüedad] => 2015-10-02
[TeamLeader] => Juan Ramon Serrano Berenguer
[Horas Login] => 77.96
[Margen Bruto/Hora] => 221.42 €
[Margen Bruto] => 17262 €
[% Consecución de Obj] => 138.87 %
[Letra] => A
[Ventas] => 51
[Ingreso] => 17262 €
[Transacciones] => 347
[Horas Ausencia] => 0
[Horas Total] => 77.96
[%Ausencia] => 0 %
[Ingresos/Hora] => 221.42 €
[Reg tocados/Hora] => 4.45
[Costes] => 0
)
[1] => Array
(
[Agente] => JT Diego D
[Antigüedad] => 2016-11-28
[TeamLeader] => Eva González
[Horas Login] => 57.45
[Margen Bruto/Hora] => 178.22 €
[Margen Bruto] => 10239 €
[% Consecución de Obj] => 82.37 %
[Letra] => B
[Ventas] => 25
[Ingreso] => 10239 €
[Transacciones] => 499
[Horas Ausencia] => 0
[Horas Total] => 57.45
[%Ausencia] => 0 %
[Ingresos/Hora] => 178.22 €
[Reg tocados/Hora] => 8.69
[Costes] => 0
)
I would like to set conditions on the fields called [Letra]=>A, B, C and Z. For example (I´m doing this right now):
Excel::create("Ingresos Agentes", function($excel) use ($salida)
{
$excel->sheet("Hoja 1", function($sheet) use ($salida)
{
foreach ($salida as $sali)
{
if($sali['Letra']='A'){
$sheet->cells('A1:D1', function ($cells) {
$cells->setBackground('#00C851');
});
}
}
$sheet->fromModel(@$salida, null, "", false, true);
});
})->store('xls', storage_path('exports'));
But I can´t find the way to set the entire excel row with that color and the proper row I´m checking on the loop,
Please, any help or any idea?¿? I´m desperate and haven´t found an example with conditions,
Thank you so much.
Upvotes: 1
Views: 4760
Reputation: 381
I´ve just found a solution: here I post if someone has a similar problem on future
Excel::create("Ingresos Agentes", function($excel) use ($salida)
{
$excel->sheet("Hoja 1", function($sheet) use ($salida)
{
foreach ($salida as $key => $sali)
{
$sheet->row($key+2, ['Col 1', 'Col 2', 'Col 3',
'Col 4', 'Col 5', 'Col 6',
'Col 7', 'Col 8', 'Col 9',
'Col 10', 'Col 11', 'Col 12',
'Col 13', 'Col 14', 'Col 15',
'Col 16', 'Col 17']);
switch ($sali['Letra']) {
case 'A':
$sheet->row($key+2, function($row) { $row->setBackground('#00C851'); });
break;
case 'B':
$sheet->row($key+2, function($row) { $row->setBackground('#FFBB33'); });
break;
case 'C':
$sheet->row($key+2, function($row) { $row->setBackground('#33B5E5'); });
break;
case 'Z':
$sheet->row($key+2, function($row) { $row->setBackground('#FF4444'); });
break;
}
}
$sheet->fromModel(@$salida, null, "", false, true);
});
})->download('xls');
Upvotes: 3