I have a very confusing issue with PHPExcel. I have 800 students. I'm generated a spreadsheet which lists how much praise (on a daily basis for the current month) that the student has has.
For instance, it may look like this:
+---------------+-----+-----+-----+-----+
| Student Name | 1st | 2nd | 3rd | 4th | ...
+---------------+-----+-----+-----+-----+
| Test Student | 2 | 0 | 3 | 7 |
+---------------+-----+-----+-----+-----+
I want to change the background color of the cells which are greater (or equal to) 5. I use a loop to loop over the students, and days. This is my code:
for($d=1; $d<=$daysInCMonth; $d++)
{
$phpExcel
->getSheetByName('Monthly Leaderboard')
->setCellValue($alphabetArray[($d+7)] . ($recordCount+5), $record['monthlyReport'][$MonthlyReportKeys[($d-1)]]);
if($record['monthlyReport'][$MonthlyReportKeys[($d-1)]]>=5)
{
$cellId = $alphabetArray[($d+7)] . ($recordCount+5);
$phpExcel
->getSheetByName('Monthly Leaderboard')
->getStyle($cellId)
->applyFromArray(
array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => '000000'))
));
}
}
To help understand the code, the initial for loop loops through from 1 up until the number of days in the current month (IE 30 for June). It then sets cells value as the number of points for each given day.
This all works perfectly. Now, the if condition will catch cells which have a value of greater (or equal to) 5.
The code $alphabetArray[($d+7)] . ($recordCount+5) grabs the current cell ID in the iteration. I know this works fine as well, because if I echo it to the screen, the first output is T5 which is a cell greater than 5.
If I implicitly specify T5 as the cell to color, it works fine. However, if I try to use the value of $cellId to dynamically color all cells for my condition, none of the cells are colored.
I know the cell ID is 100% correct, I know the coloring statement is correct (as it does color cells if I refer to them specifically). It just doesn't want to play dynamically.
Any ideas?
Thanks
Phil
This is quite an old question now, but I found it after having the same problem. After digging into the code I found something that does work. So thought I would add it in here for any future finder.
For conditional coloring of the background the method of just setting the color of the fill doesn't seem to work. e.g.
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array(
'rgb' => 'FFC7CE'
),
)
The above works perfectly well when applied directly to a cell, but when used in a conditional styling. If just does nothing. However if you change it to
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'rgb' => 'FFC7CE'
),
'endcolor' => array(
'rgb' => 'FFC7CE'
),
)
The background colors as expected. It looks like the conditional coloring of a background needs the start and end colors specified.
$headerStyle = array(
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb'=>'00B4F2'),
),
'font' => array(
'bold' => true,
)
);
$borderStyle = array('borders' =>
array('outline' =>
array('style' => PHPExcel_Style_Border::BORDER_THICK,
'color' => array('argb' => '000000'), ),),);
//HEADER COLOR
$objPHPExcel->getActiveSheet()->getStyle('A1:'.'V1')->applyFromArray($headerStyle);
//SET ALIGN OF TEXT
$objPHPExcel->getActiveSheet()->getStyle('A1:V1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B2:V'.$row)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
//BORDER TO CELL
$objPHPExcel->getActiveSheet()->getStyle('A1:'.'V1')->applyFromArray($borderStyle);
$borderColumn = (intval($column) -1 );
$objPHPExcel->getActiveSheet()->getStyle('A1:'.'V'.$borderColumn)->applyFromArray($borderStyle);
Related
I would like to:
1) set min-width of the cell
On the internet there is some discussion about this , but I find the code not work:
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(100);
the column size is not expand according to the data size / preset width
2) underline the cell
I am making an account table, so I would like to underline some row
$styleArray = array(
'borders' => array(
'bottom' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
$sheet->getStyle('A1')->applyFromArray($styleArray);
However the underline is not exist, I wonder is it due to the border also count as 1 line? for example , if A1 is underlined, then I need to user A3 for the next row.
Thanks a lot for helping.
Update:
1) set min-width:
From the source code the column is a number instead of string , so I try like this but still no luck
$excel->getActiveSheet()->getColumnDimensionByColumn(0)->setAutoSize(false);
$excel->getActiveSheet()->getColumnDimensionByColumn(0)->setWidth('4.42');
2) underline is working now
$border_bottom = array(
'borders' => array(
'bottom' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
$excel->getActiveSheet()->getStyle("7")->applyFromArray($border_bottom);
it works, but just wondering can I underline when set cell instead of hardcode the position: A1:B2 etc...
In number 1, you can check this: PHPExcel auto size column width
With regards on your number 2, have you tried to use this? Instead of underlining the cell, you underline the value of the cell.
$styleArray = array(
'font' => array(
'underline' => PHPExcel_Style_Font::UNDERLINE_SINGLE
)
);
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
unset($styleArray);
If you want a range of cells to be underlined, instead of:
objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
use:
$objPHPExcel->getActiveSheet()->getStyle('A1:A3')->applyFromArray($styleArray);
I am setting a cell value in phpexcel using below method setCellValueByColumnAndRow()
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$xlsRow,$plan);
Now my requirment is to set background color for this.
I am not able to use this below method as I am aligned with rows and columns numbers.
$objPHPExcel->getActiveSheet()->getStyle("A1")->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setRGB($color);
I am searching a way to provide cols and rows as (2,3) not like ('A1:E1')
Please suggest an alternative way to set background color using column and row numbers.
PHPExcel_Cell::stringFromColumnIndex(0); worked perfectly.
$column = PHPExcel_Cell::stringFromColumnIndex(45);
$row = 1;
$cell = $column.$row;
The $cell will give you AT1
$range = 'A1:'.$cell;
So you can easily pass into the filling range like.
$objPHPExcel->getActiveSheet()->getStyle($range)->getFill()->applyFromArray(array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'rgb' => 'FFFF00' //Yellow
)
));
You cannot style a row in PHPExcel, only a cell or a range of cells
$objPHPExcel->getActiveSheet()
->getStyle('A1:E1')
->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()
->setARGB('FF808080');
or
$objPHPExcel->getActiveSheet()
->getStyle('A1:E1')
->applyFromArray(
array(
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'E05CC2')
)
)
);
Will set the background fill style for cells A1 to E1
I am trying to create a table with cells width a fixed width.
Let's say I have this code:
$table->addCell(300)->addText('first');
$table->addCell(300)->addText('text that is very looooooooong');
The first cell's 300 width is ignored and is crushed against the left side of the page like this:
first
My second cell is going to contain large texts, but I want that cell to maintain it's width.
I couldn't find it anywhere on the web so I am asking if somebody knows what I will have to do here.
Remeber that these values are TWIPS and 300 Twips are mere 5mm.
Not enough room to hold more than 1 character.
Here is a calculator that helps you to convert values:
Topgrapy Calculator
Also you might use the built in conversion functions in Shared\Font.php like this:
$helper= new PHPWord_Shared_Font();
//convert 5cm to Twips
$inTwips=$helper->centimeterSizeToTwips(5);
I have not tried this so far.
I just found out that you have to set style "layout" fixed on your table like below
$fancyTableStyle = [
'borderSize' => 6,
'borderColor' => '000000',
'cellMargin' => 80,
'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,
'layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED,
];
$table = $section->addTable($fancyTableStyle);
and then the long word will wrap itself inside the cell.
If you preset a table style on your phpword object, the layout style won't work.
$fancyTableStyleName = 'Fancy Table';
$fancyTableStyle = [
'borderSize' => 6,
'borderColor' => '000000',
'cellMargin' => 80,
'alignment' => \PhpOffice\PhpWord\SimpleType\JcTable::CENTER,
'layout' => \PhpOffice\PhpWord\Style\Table::LAYOUT_FIXED,
];
//table will not be fixed
$table = $section->addTable($fancyTableStyleName);
In ms word you also need to set the autofit option directly on each table, maybe that's why.
I have a relatively complex array in PHP that needs to be sorted in a specific way but the number of filters and sorts is a bit on the complex side that a simple solution seems to be escaping me.
The Problem: We have products in an order that need to be filtered into "shipto" groups based on the following criteria: Date the product can be shipped, if the product can be grouped with other products, and in which warehouse(s) the product can be shipped from. If 2 products have the same date, warehouse, and allow grouping then they are placed together in the same "shipto".
Current Solution (Not fully realized)
When I extract an order I traverse the items in it and build an array with the following information (FYI, this probably contains more info than I need to solve this problem)
$allProducts = array();
$today = date("Ymd");
/* If the product is being shipped now, put in the Today ShipTo dated 00000000 */
if (($today > $start) && ($today < $end)) {
array_push($allProducts, array('ship' => '00000000', 'defaultwarehouse' => $defaultwarehouse, 'warehouse' => $warehouse, 'group' => $group,'product' => $product, 'start_month' => $start_month, 'start_day' => $start_day, 'end_month' => $end_month, 'end_day' => $end_day, 'description' => $description, 'qty_ordered' => $qty_ordered, 'row_total' => $row_total, 'price' => $price, 'tax_amount' => $tax_amount, 'shipping_group' => $shippingGroup, 'today' => $today, 'end' => $end, 'start' => $start ) );
} else {
/* If the product is not being shipped today, put in a Group that matches the starts date for the ShipTo */
array_push($allProducts, array('ship' => $start, 'defaultwarehouse' => $defaultwarehouse, 'warehouse' => $warehouse, 'group' => $group, 'product' => $product, 'start_month' => $start_month, 'start_day' => $start_day, 'end_month' => $end_month, 'end_day' => $end_day, 'description' => $description, 'qty_ordered' => $qty_ordered, 'row_total' => $row_total, 'price' => $price, 'tax_amount' => $tax_amount, 'shipping_group' => $shippingGroup, 'today' => $today, 'end' => $end, 'start' => $start ));
}
endif;
Where $defaultwarehouse is a number representing the default warehouse (i.e. 1), $warehouse is all of the possible warehouses a product can be in (i.e. 1,2), $start is the date a product can first be shipped (i.e. 20121201 for Dec 1, 2012; Ymd, 00000000 represents today so they can be numerically sorted) and $group is a boolean saying if the product can or can not be shipped with other items (i.e. 1 for yes, 0 for no)
My original idea here was to sort the array based on group, then on warehouse, then on start date. I would then traverse the array to build the "shipto"s and when certain components of the array change (like Warehouse or Start) I would close the previous shipto and start a new one. If I take Group and Multiple Warehouses out of the equation I can get this logic to work. But my task requires I have them in so I am running into the following complications.
First, after I sort by Group, I really want to do 2 sub-sorts of Group=Yes and Group=No for the rest of the filters. This lead me to think that maybe I should be separating the array into 2 arrays and sorting them similarly but separately. It seems inefficient to do it this way but I am not sure.
Second, since warehouse can be a comma separated value, how can I filter on that and get the proper matches. i.e. if I have 3 products where their warehouse value is 1:1,2:3 this should group into 2 shiptos. One for 1:1,2 and a Second for 3.
Thoughts on the problem
My feeling is the way I am thinking about this problem with filtering the items into a traversable array may not be the best way to tackle it. It may be that I need to push the order items individually into a new "Shipments" array that checks each item against all current "Shipments". But I am also not exactly sure on how that would work. Or there may be a different way I am not thinking about at all about how to achieve this.
Example Array Data (Extra Data removed for simplicity):
[0]=>
'ship' => 00000000
'defaultwarehouse' => 1
'warehouse' => 1
'group' => 1
'sku' => 'ABC123'
[1]=>
'ship' => 00000000
'defaultwarehouse' => 1
'warehouse' => 1,2
'group' => 1
'sku' => 'DEF234'
[2]=>
'ship' => 00000000
'defaultwarehouse' => 2
'warehouse' => 1,2
'group' => 1
'sku' => 'GHI567'
[3]=>
'ship' => 20121220
'defaultwarehouse' => 1
'warehouse' => 1,2
'group' => 1
'sku' => 'JKL890'
[4]=>
'ship' => 20121220
'defaultwarehouse' => 1
'warehouse' => 1,2
'group' => 1
'sku' => 'MNO123'
[5]=>
'ship' => 20130401
'defaultwarehouse' => 1
'warehouse' => 1
'group' => 1
'sku' => 'PQR456'
[6]=>
'ship' => 20130401
'defaultwarehouse' => 1
'warehouse' => 1
'group' => 0
'sku' => 'STU789'
This should result in 5 "shipto" groups:
shipto[1] => ABC123, DEF234 (Base "group" for all other comparisons)
shipto[2] => GHI567 (Default warehouse does not match, previous shiptos)
shipto[3] => JKL890, MNO123 (Different shipping date)
shipto[4] => PRQ456 (Different shipping date from all others)
shipto[5] => STU789 (Can not be grouped with other shipments)
Is it possible for you to store the data in a database or somewhere similar? It sounds like your data is pretty complex, and storing it in a database would make the sorting you're looking to do a lot easier.
In the end this problem got even more complex and I generally threw away the concept of being able to sort logically and applying sub-sorts to a single array or SQL query. The business logic changed so much that keeping this all straight made no sense in one dimension.
The final logic applies 4 levels of filtering so what I did was isolate each step of logic and filter the previous array into a new array with the orders in their proper place. It is NOT the most efficient way to handle this but it is very readable and allows me to clearly see how each order is getting sorted at each step of the process. The "sorted" array is 4 levels deep but allows me to cherry pick each shipment for proper display.
I have the following values from a database call that I want to apply some logic to. I thought I could originally use PHP's max however this doesn't appear to be the case.
I have three suppliers of a product. They might not all stock the item I am displaying, and they all offer a different margin, on a product by product basis though, so that is why I can't just say generally supplier 1 is better than supplier 2 etc.
$supplier1Live = 1
$supplier2Live = 1
$supplier3Live = 0
$marginSupplier1 = 20
$marginSupplier2 = 40
$martinSupplier3 = 50
In this example I would want to use Supplier 2 as they stock the product supplier2Live = 1 and also have the better margin than the other supplier who stocks the product (supplier1)
My mind however is drawing a complete blank in how to code this?
I thought I could add it to an array giving:
$array = array(
"supplier1" => array(
"live" => 1,
"margin" => 20
),
"supplier2" => array(
"live" => 1,
"margin" => 40
),
"supplier3" => array(
"live" => 0,
"margin" => 50
)
);
And run something on that, but not sure what to.
Filter the array using array_filter (filter by live==1), and then find the maximum out of the resultant array (maximum on the "margin" value)
Like this, if I understand correctly
$array = array(
"supplier1" => array(
"live" => 1,
"margin" => 20
),
"supplier2" => array(
"live" => 1,
"margin" => 40
),
"supplier3" => array(
"live" => 0,
"margin" => 50
)
);
$res = array_filter($array,function($v){return $v["live"];});
$supplier = array_reduce($res, function($a, $b){
return $a["margin"]>$b["margin"]?$a:$b;
});
print_r($supplier);
Try something like this:
$best_supplier = null;
$best_supplier_margin = null;
foreach($array as $name => $supplier) {
if($supplier['live']) {
if($supplier['margin'] > $best_supplier_margin || is_null($best_supplier_margin)) {
$best_supplier = $name;
$best_supplier_margin = $supplier['margin'];
}
}
}
if(is_null($best_supplier)) throw new Exception('No suppliers are live!');
echo $best_supplier;
So you basically want to find the max of supplierXLive * marginSupplierX?
You can also implement a custom compare function and provide it to PHPs usort() function