I'm trying to add some tables to a document, which after lot of work, it's showing right. Generated with PHPWord. The issue is that whatever I add in the table,row or cell style, it doesn't modify the table.
I've already tried: $table->addRow(15), $table->addRow(array('height'=>15)) and as is shown in the code below.
Also, there is no documentation on how to add a cell with two lines of text, because below 'Sign' I have to add 'Name'.
//TABLE1
$table = $section->addTable(array('width'=>100, 'borderSize'=> 1, 'borderColor'=>'000000'));
$table->addRow(15,array('height'=>15));
$table->addCell(array('width'=>150))->addText('COMPANY', $smallBoldFont);
$table->addRow();
$table->addCell()->addText('');
$table->addRow();
$table->addCell()->addText('');
//TABLE1
$section->addTextBreak(2);
//TABLE2
$table2 = $section->addTable(array('borderSize'=> 1, 'borderColor'=>'000000'));
$table2->addRow(100,array('height'=>80));
$table2->addCell(150,array('width'=>800))->addText('Recibed', $smallFont8);
$table2->addRow();
$table2->addCell()->addText('Time:', $smallFont8);
$table2->addCell()->addText('Sign:', $smallFont8);
//TABLE2
Is there a reason why PHPWord ignores my format style to tables? Is there any other way of doing it better?
Desired output:
Actual output:
In documentation is all. Just to read it deeply. Here is section how to create tables, including creation of tables with multicolumn cells
I don't know if PHPWord has some pre-defined units for case if you miss to use them. But it seems you need to set units for width. But you should test units and numbers as you need.
Order to create multicolumn cell is gridSpan(). So line for creation of the first row of the second table (probably) should be:
$table2->addCell(150,array('width'=>800))->gridSpan(2) ->addText('Recibed', $smallFont8);
PHPWord uses twip as default unit. And all cells have to match its width.
The smallest cell is taken for entire column.
For convertion from twips to other units may be used EndMemo (Topography Conversion Online).
250 pixels are 3750 twips.
Related
Problem:
What I am trying to achieve is to copy a selected range of cells from one excel spreadsheet and insert it into a newly generated one using laravel-excel and phpspreadsheet libraries. So far, the code that I've got, sort of does that but not idealy.
Excel::load($file, function($reader)
{
$activeSheet = $reader->getActiveSheet();
$this->data = $activeSheet->rangeToArray(
'A1:A27', // The worksheet range that we want to retrieve
NULL, // Value that should be returned for empty cells
true, // Should formulas be calculated (the equivalent of getCalculatedValue() for each cell)
true, // Should values be formatted (the equivalent of getFormattedValue() for each cell)
true // Should the array be indexed by cell row and cell column
);
});
// Create new file.
$newExport = Excel::create('Filename', function($excel) {
$excel->sheet('Sheetname', function($sheet) {
$sheet->fromArray($this->data, null, 'B1', true);
});
});
// Export newly created file.
$newExport->export('xlsx');
The problem is that it also inserts column name into a first cell (0 on screenshot, as I had indexing turned off, with indexing on, it would insert A) , as you can see on a screenshot bellow.
Actual result:
Expected result:
Stuff I have tried:
Tried to turn off indexing inside rangeToArray method.
Add column name into ignore list (2nd parameted in fromArray()), but that's not pragmatic, as I would end up adding every single column name into ignore list, moreover, it inserts blank value into first column and starts from B2 cell.
If anyone could give me some ideas how to resolve this case, that would be great.
Thanks!
The answer to the case is following...
Read the documentation for the library that you're actually using, rather than the one it's based on.
The reason why it was adding column headings, was due to laravel-excel library fromArray method default property values, which has heading generation enabled by default.
$sheet->fromArray($this->data, null, 'B1', true);
change to
$sheet->fromArray($this->data, null, 'B1', true, false);
Accepted parameters:
fromArray($source, $nullValue, $startCell, $strictNullComparison, $headingGeneration).
I export data from database with dynamical columns and rows so I use R1R1 / $excel->setCellValueByColumnAndRow($col, $row, $value);
Row 2 is my title including autofilter and line 1 on some columns i want a =subtotal().
I tried as =SUBTOTAL() and =TEILERGEBNIS() as well as C1R1 and Standard:
$excel->setCellValueByColumnAndRow(12, 1, "=SUBTOTAL(9;C(2)S:R(".$i.")S)")");
$excel->setCellValue("M1", "=TEILERGEBNIS(9;M3:M".$i.")");
Excel will ask to restore and will set "M1" to 0.
If i copy this value of an echo to M1 it does what it should an is what the formula is as i do it in Excel itself: =TEILERGEBNIS(9;M3:M2315)
There's a couple of things wrong here
Commas (,), not semi-colons (;) for function argument separators (unless you've explicitly set a different locale for the calculation engine)
PHPExcel's calculation engine doesn't accept Column/Row addresses (like R1C1), just cell A1-style addresses
English names for functions (unless you've explicitly set a different locale for the calculation engine)
I have a table on phpGrid, there is function for adding conditional format on the cells but i need more adaptive option... when in any rows tow defined cells equals each others i want to add format
For example when in any row n cell nR == nT i want to make them red colors
$dg = new C_DataGrid("SELECT * FROM orders", "orderNumber", "orders");
$dg->set_conditional_format("orderNumber","CELL",array( "condition"=>"eq","value"=>"10107","css"=> array("color"=>"#ffffff","background-color"=>"green")));
// that is in documentation and i have tried this
$dg->set_conditional_format("orderNumber","CELL",array( "condition"=>"eq","value"=>$dg->columns['orderNumber'] ,"css"=> array("color"=>"#ffffff","background-color"=>"green")));
What is $dg->columns['orderNumber']?? I could not find it mentioned anywhere in their online documentation.
My guess is that you are trying to compare values among columns. You can take a look at their OnGridLoadComplete event handler examples:
https://phpgrid.uservoice.com/knowledgebase/articles/909546-conditional-format-compare-two-cells
https://phpgrid.com/example/row-level-permission-edit-condtion/
In an excel sheet where I previousely formatted the whole first colum as a date, I changed afterwards the format of the used cells to General (cells A1 to A11). So from cell A12, this first column still has the previous date format.
When I try to use the values of the first column (A1, A2, ..., A11) it identifies their format as a date, and gives a wrong result.
For example, this retruns a date format (d/mm/yyyy), which is not the case (see picture)
$sheet->getStyle('A3')->getNumberFormat()->getFormatCode();
Also the whole column has this format :
$sheet->getStyle('A')->getNumberFormat()->getFormatCode();
Is there a way to make sure the format of the cell itself is considered?
(when I indicated the format of this cells explicitely, the right format was used)
After some investigation, found that this problem occurs only for the .xlsx files (using reader PHPExcel_Reader_Excel2007) and not for the .xls files (reader PHPExcel_Reader_Excel5).
You can set the whole row/col format:
$sheet->getStyle('A')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
Because you have changed only the format of A1 to A11, the rest are still with the old format. So setting format of the Whole Column/Row will fix this problem
After some more invetigation found a possible cause in reader Excel2007.php.
There is a problem with the condition When setting the style (line 933):
if ($c["s"] && !$this->_readDataOnly) {
$cell->setXfIndex(isset($styles[intval($c["s"])]) ? intval($c["s"]) : 0);
}
In case of General cell format, $c["s"] is not defined, and the _xfIndex of the cell is not set (not passing the if statement). This gives (I didn't found why) a problem with the stored _xfIndex of the cell.
When removing the $c["s"] condition, the right style is set.
if (/*$c["s"] &&*/ !$this->_readDataOnly) {
$cell->setXfIndex(isset($styles[intval($c["s"])]) ? intval($c["s"]) : 0);
}
Hoping there are no hidden consequences.
I have a cell and I need to add the sign "%" at the end of it, just for display. That cell is linked to other cells that have a formula that uses its value.
I have tried adding the "%" in the cell and using ABS in the formula. e.g. 100 - ABS(H23)This would have worked if after I use concat in php, the output would not have added an extra single column. ...->setCellValue('H23', $number.'%');
Does this to the cell '33%
1.. Any other solution for my title question?
2.. How do I make the output excel not have the single quote when I concatenate strings to it?
Thanks plenty!
If this is just for display, then set the cell value correctly, and give the cell a numberFormat mask as you would in MS Excel itself.
$objPHPExcel->getActiveSheet()->setCellValue('A5', 0.33);
$objPHPExcel->getActiveSheet()->getStyle('A5')
->getNumberFormat()->setFormatCode(
PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE
);
EDIT
To avoid MS Excel automatically multiplying by 100 when using a percent-based format mask: using a string format mask:
'0 "%"'
$objPHPExcel->getActiveSheet()->setCellValue('A5', 0.33);
$objPHPExcel->getActiveSheet()->getStyle('A5')
->getNumberFormat()->setFormatCode(
'0 "%"'
);