PHPExcel format dates occuring this week - phpexcel

PHPExcel conditional formatting has some helpful conditions and operators...
I applied a condition like so:
$aCondition = new PHPExcel_Style_Conditional();
$aCondition->setConditionType(PHPExcel_Style_Conditional::CONDITION_EXPRESSION)
->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_EQUAL)
->addCondition('AND(($B2<>$B3),$B2<>"")');
$aCondition->getStyle()->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$aCondition->getStyle()->getNumberFormat()->setFormatCode($dateFormat);
$conditionalStyles = $sheet->getStyle('B2')->getConditionalStyles();
array_push($conditionalStyles, $aCondition);
$sheet->getStyle('$'.$letter.'2:$'.$letter.'10000')->setConditionalStyles($conditionalStyles);
However, when I get to the excel document... it marks the cell yellow even though it should not be... but if I then go to the cell and press enter... the cell then loses its yellow... and then the conditional formatting works correctly...
I apply formatting for that cell's rows like so:
$sheet->getStyle('M2:M9999')
->getNumberFormat()
->setFormatCode("dd/mm/yyyy");

I found this little doozy:
PHPExcel_Shared_Date::PHPToExcel( strtotime( "03/25/2014" ) )
As soon as you apply this, the issue goes away.. This is because the formatcode is just a mask... the underlying data has to be of a special Excel date type.

Related

Column width doesn't automatically adjust for certain types of data

I'm currently building a dashboard with flexdashboard and want to present one tab on my dashboard as showing the raw data organised into a data table using the datatable function in R.
When the dashboard generates, the rows have a huge height due to some large character entries in that string. E.g. certain ids in the format "xxxxxx-xxxx-xxxx-xxxxxx". It doesn't present them like this however, they are normally in the row presented as:
xxxxxx-
xxxx-
xxxx-
xxxxxx
creating the huge height.
A similar thing happens with date formats in the YYYY-MM-DD HH:MM:SS format
however the issue doesn't occur when using very long character strings without any "-" in it.
e.g. the column width will adjust properly for "xxxxxxxxxxxxxxxxxxxxxxx"
I had a look through stackoverflow and couldn't find anything similar, questions would mostly be around autoWidth not being set to true. And as mentioned column width adjusts automatically for certain types of character strings just not ones with "-" in it.
datatable(data1,
rownames=T,
filter ="top",
options=list(pageLength=100,
autoWidth=T,
scrollX=T))
You can disable the wrapping by using the class nowrap
DT::datatable(
class = "display nowrap",
extensions = c('Buttons','Scroller'),
...…

PHPExcel how to set date format in cell during export so filtration is available?

I would like to be able to filter my data like this:
Year ( + )
Month ( + )
Day ( + ).
At first i tought it's problem of cell formatting cause the format was set to General. So i did this:
$objPHPExcel->getActiveSheet()
->getStyle('G2:G256')
->getNumberFormat()
->setFormatCode(
PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2
);
And i worked. Now my cell G2 is formatted as date (at least excel says so). But i'm not able to sort this until i select cell and hit enter, after this ONLY this one cell is sortable. I have no idea why.
Thanks in advance for any help!
You need to set date values in cells as MS Excel timestamp values, not strings or unix timestamps.
Assuming from the way you're doing it at the moment that $row->user_created is a Unix timestamp value the, rather than
$formattedRow[] = date('Y-m-d',$row->user_created);
use
$formattedRow[] = PHPExcel_Shared_Date::PHPToExcel(
$row->user_created
);
which will set the value to a MS Excel serialized date/timestamp
You still want to set the number format mask, so that MS Excel knows how the date should be displayed; but as it's stored in Excel format rather than as a string, it will be sortable or filterable, or usable in formulae, etc

Excel sheet corrupted after conditional formatting

I am generating an excel sheet with phpexcel, when using conditional formatting with the condition being (search for a text or part of it), i get a validation error when trying to open the generated sheet. Works perfectly with numbers, Less so WITH TEXTS.
Here is my code :
//conditional formatting
$objConditional1 = new PHPExcel_Style_Conditional();
$objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT)
->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_CONTAINSTEXT)
->addCondition('X');
$objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_YELLOW);
$objConditional3 = new PHPExcel_Style_Conditional();
$objConditional3->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS)
->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_GREATERTHANOREQUAL)
->addCondition('0');
$objConditional3->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);
$conditionalStyles = $objPHPExcel->getActiveSheet()->getStyle('B2')->getConditionalStyles();
array_push($conditionalStyles, $objConditional3);
array_push($conditionalStyles, $objConditional1);
$objPHPExcel->getActiveSheet()->getStyle('B2')->setConditionalStyles($conditionalStyles);
Does any one know how to work around this ? am i doing anything wrong ?
Thanks.
As far as i know, though i have not tested with your case specifically, when you try to apply conditional formatting based on CONTAINSTEXT,
you shouldn't call
'->addCondition('X')'
(line 5 in your code), but instead call the method:
->setText('X')
On your PHPExcel_Style_Conditional objects. This is how I specify the text to compare to in my PHPExcel sheets.

No Way to Format Cell as Month-Date-Year in PHPExcel?

I've am trying to format cells in an Excel document I create with PHPExcel using the setFormatCode method. I've been going through the list of 'FORMAT_DATE_xxxxxxx' options associated with this method, but I've been unable to find one that lists the month, date, and year in that order. Did I miss something here?
You missed FORMAT_DATE_XLSX14 and FORMAT_DATE_XLSX22
You also missed the fact that these are simply predefined string values, and that you can use any valid Excel numberformat string value that you set yourself.
$objPHPExcel->getActiveSheet()->getStyle('C9')
->getNumberFormat()
->setFormatCode(
'mm-dd-yyyy' // my own personal preferred format that isn't predefined
);

PHPExcel Accounting Formats

I'm working with PHPExcel and I'm trying to format a cell using Excel's built-in "Accounting" format. I'm aware of the Format Code:
PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE
But this simply formats to two decimal places and adds a $ in front of the number. The result I'm looking for is the right aligned cell with the $ on the left. $0 values should be listed as "-" and negative values should be $ (1.11)
As far as I can tell there are no other currency formats in the documentation (though I may have missed it, this documentation is horrendous). Am I looking in the wrong place? Can this be achieved with regular cell formatting or is Excel doing something unique for Accounting?
I reverse engineered the format code from an existing spreadsheet using PHPExcel and got this:
_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(#_)
This is the code Excel places on the cell when you select the "Accounting" format... or click that "$" toolbar button in Excel 2007.
If you didn't need currency symbol :
->setFormatCode("_(* #,##0.00_);_(* \(#,##0.00\);_(* \"-\"??_);_(#_)");
$objPHPExcel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode("#,##0.00");
or use
$objPHPExcel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode("#.##0,00");
Set in getStyle CELL.
There are no other pre-defined formats beyond those listed in PHPExcel_Style_NumberFormat, but you should be able to set the format code to any string that you could use when setting an MS Excel custom format...
e.g.
[green]$#,##0.00;[red]$(-#,##0.00)
As regards cell alignment, set this to right yourself, or don't set it at all.

Resources