This is:
- [x ] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)
I'm using PHPSpreadSheet to generate an Excel file custom with a report for my company.
The file consists in a header (Row 1). Then several products, And the first column contains the product image.
Since some product have attributes (options/sizes M/L/XL...) I only include the image the first time it shows the product. Then omit for the following options for the product.
I set the height of row with the image higher than the image itself.
The first image top-left corner is correct to the position selected.
But the following images start to fall out of positioning.
I've made a script to test this, and is not as severe as in my usage, but it stll happens. The images are not aligned with the top left corner.
All images top-left corner positioned exactly at top-left cell selected.
Create an excel file with the provided code below (maybe some code could be omitted but could be relevant) and scroll it to find several images not positioned correctly.
Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:
```php
require __DIR__ . '/vendor/autoload.php';
$obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$obj->setActiveSheetIndex(0)
->setCellValue('B1', 'REFERENCE' )
->setCellValue('C1', 'NAME')
;
$lastcolumn = 'D';
$obj->getActiveSheet()->getRowDimension(1)->setRowHeight(40);
$obj->getActiveSheet()->getColumnDimension('A')->setWidth(12);
$obj->getActiveSheet()->getColumnDimension('B')->setWidth(11);
$obj->getActiveSheet()->getStyle('A1:'.$lastcolumn.'1')->getAlignment()->setWrapText(true);
$obj->getActiveSheet()->getStyle('A1:'.$lastcolumn.'1')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$obj->getActiveSheet()->getPageSetup()->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
$obj->getActiveSheet()->getPageSetup()->setPaperSize(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::PAPERSIZE_A4);
//$obj->getActiveSheet()->getPageSetup()->setFitToPage(true);
$obj->getActiveSheet()->getPageSetup()->setFitToWidth(1);
$obj->getActiveSheet()->getPageSetup()->setFitToHeight(0);
$margin = 0.5 / 2.54;
$pageMargins = $obj->getActiveSheet()->getPageMargins();
$pageMargins->setTop($margin);
$pageMargins->setBottom($margin);
$pageMargins->setLeft($margin);
$pageMargins->setRight($margin);
$styleThickBrownBorderOutline = array(
'borders' => array(
'top' => array(
'style' => PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => array('argb' => 'B0B0B0'),
),
),
);
$obj->getActiveSheet()->getStyle('A2:'.$lastcolumn.'2')->applyFromArray($styleThickBrownBorderOutline);
$rowNumber = 2;
for($i = 2; $i <=500; $i++) {
// Add image
$obj->getActiveSheet()->getStyle('A'.$rowNumber.':'.$lastcolumn.$rowNumber)->applyFromArray($styleThickBrownBorderOutline);
$imagelink = 'image_64.png';
$obj->getActiveSheet()->getRowDimension($rowNumber)->setRowHeight(80);
$objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$objDrawing->setPath($imagelink);
$objDrawing->setCoordinates('A'.$rowNumber);
$objDrawing->setWorksheet($obj->getActiveSheet());
$repeat = rand(1,5);
for($j = 1; $j <= $repeat; $j++){
$obj->setActiveSheetIndex(0)->setCellValue('B'.$rowNumber, 'REF'.$i);
$obj->setActiveSheetIndex(0)->setCellValue('C'.$rowNumber, 'VERYVERYVERYVERY VERYVERY VERY LONG SUBJECT NAME'.$i);
$obj->setActiveSheetIndex(0)->setCellValue('D'.$rowNumber, 'D '.$j);
$rowNumber++;
}
}
echo 'i:'.$i.' rownumber:'.$rowNumber;
$obj->getActiveSheet()->getStyle('B2:B'.$rowNumber)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$obj->getActiveSheet()->getStyle('D2:'.$lastcolumn.$rowNumber)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$obj->getActiveSheet()->getStyle('A2:'.$lastcolumn.$rowNumber)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($obj, 'Xlsx');
$filename = 'teste.xlsx';
$objWriter->save($filename);
1.4.1.0
Done a bit more testing, And if I set all row to the same height, by using inside the foreach:
$obj->getActiveSheet()->getRowDimension($rowNumber)->setRowHeight(80);
The image position is set correctly.
So the problem is only when rows have different height.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.
I don't like this bot, it closes issues in an attempt to "clean the issue tracker", as a result, this repo seems to have far fewer issues, and the number of closed issues looks too good to be true.
For example, I'm now having this issue, instead of closing this issue by solving it, it was closed by a bot and the issue remains. Admins are more concerned about having fewer open issues even if it means creating a robot to close them.
Most helpful comment
I don't like this bot, it closes issues in an attempt to "clean the issue tracker", as a result, this repo seems to have far fewer issues, and the number of closed issues looks too good to be true.
For example, I'm now having this issue, instead of closing this issue by solving it, it was closed by a bot and the issue remains. Admins are more concerned about having fewer open issues even if it means creating a robot to close them.