forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add editAs Property for 2-cell Anchor Drawings
This change builds on PR PHPOffice#2532 (@naotake51 as PR PHPOffice#2532), using ideas from PR PHPOffice#2237 (@AdamGaskins), which has had changes requested for several months. It covers a lot of the same ground as 2532. In Excel, two-cell anchor drawings can be edited as "twocell", "onecell", or "absolute". This PR adds support for those options, with a sample file that demonstrates the difference in addition to unit tests. Several other tests are added to improve the spotty coverage for Drawings. There have been several other tickets referencing two cell anchors, including issue PHPOffice#1159 and PR PHPOffice#1160 (@sgarwood, who also added support for editAs), PR PHPOffice#643, and issue PHPOffice#126, all now closed but not necessarily entirely resolved. I will try to ensure that those tickets are addressed with this one. And, in trying to make sure 1160 is covered, I stumbled upon a bug. If you use the same image resource to create two+ memory drawings, the MemoryDrawing destructor for the first will cause the rest to generate a very long warning message. This is not a problem for Php8+, only for Php7-. I have suppressed the message in the MemoryDrawing constructor. 1160 went stale due to an unresolved test error, but I don't think this was the problem. At any rate, its test works now.
- Loading branch information
Showing
8 changed files
with
468 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
// Create new Spreadsheet object | ||
use PhpOffice\PhpSpreadsheet\Helper\Dimension; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; | ||
|
||
require __DIR__ . '/../Header.php'; | ||
|
||
$helper->log('Create new Spreadsheet object'); | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$sheet->getCell('A1')->setValue('twocell'); | ||
$sheet->getCell('A2')->setValue('twocell'); | ||
$sheet->getCell('A3')->setValue('onecell'); | ||
$sheet->getCell('A6')->setValue('absolute'); | ||
|
||
// Add a drawing to the worksheet | ||
$helper->log('Add a drawing to the worksheet two-cell anchor not resized'); | ||
$drawing = new Drawing(); | ||
$drawing->setName('PhpSpreadsheet'); | ||
$drawing->setDescription('PhpSpreadsheet'); | ||
$drawing->setPath(__DIR__ . '/../images/PhpSpreadsheet_logo.png'); | ||
// anchor type will be two-cell because Coordinates2 is set | ||
//$drawing->setAnchorType(Drawing::ANCHORTYPE_TWOCELL); | ||
$drawing->setCoordinates('B1'); | ||
$drawing->setCoordinates2('B1'); | ||
$drawing->setOffsetX2($drawing->getImageWidth()); | ||
$drawing->setOffsetY2($drawing->getImageHeight()); | ||
$drawing->setWorksheet($spreadsheet->getActiveSheet()); | ||
|
||
// Add a drawing to the worksheet | ||
$helper->log('Add a drawing to the worksheet two-cell anchor resized'); | ||
$drawing2 = new Drawing(); | ||
$drawing2->setName('PhpSpreadsheet'); | ||
$drawing2->setDescription('PhpSpreadsheet'); | ||
$drawing2->setPath(__DIR__ . '/../images/PhpSpreadsheet_logo.png'); | ||
// anchor type will be two-cell because Coordinates2 is set | ||
//$drawing->setAnchorType(Drawing::ANCHORTYPE_TWOCELL); | ||
$drawing2->setCoordinates('C2'); | ||
$drawing2->setCoordinates2('C2'); | ||
$drawing2->setOffsetX2($drawing->getImageWidth()); | ||
$drawing2->setOffsetY2($drawing->getImageHeight()); | ||
$drawing2->setWorksheet($spreadsheet->getActiveSheet()); | ||
|
||
$spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth($drawing->getImageWidth(), Dimension::UOM_PIXELS); | ||
$spreadsheet->getActiveSheet()->getRowDimension(2)->setRowHeight($drawing->getImageHeight(), Dimension::UOM_PIXELS); | ||
|
||
// Add a drawing to the worksheet one cell anchor | ||
$helper->log('Add a drawing to the worksheet one-cell anchor'); | ||
$drawing3 = new Drawing(); | ||
$drawing3->setName('PhpSpreadsheet'); | ||
$drawing3->setDescription('PhpSpreadsheet'); | ||
$drawing3->setPath(__DIR__ . '/../images/PhpSpreadsheet_logo.png'); | ||
// anchor type will be one-cell because Coordinates2 is not set | ||
//$drawing->setAnchorType(Drawing::ANCHORTYPE_ONECELL); | ||
$drawing3->setCoordinates('D3'); | ||
$drawing3->setWorksheet($spreadsheet->getActiveSheet()); | ||
|
||
// Add a drawing to the worksheet | ||
$helper->log('Add a drawing to the worksheet two-cell anchor resized absolute'); | ||
$drawing4 = new Drawing(); | ||
$drawing4->setName('PhpSpreadsheet'); | ||
$drawing4->setDescription('PhpSpreadsheet'); | ||
$drawing4->setPath(__DIR__ . '/../images/PhpSpreadsheet_logo.png'); | ||
// anchor type will be two-cell because Coordinates2 is set | ||
//$drawing->setAnchorType(Drawing::ANCHORTYPE_TWOCELL); | ||
$drawing4->setCoordinates('C6'); | ||
$drawing4->setCoordinates2('C6'); | ||
$drawing4->setOffsetX2($drawing->getImageWidth()); | ||
$drawing4->setOffsetY2($drawing->getImageHeight()); | ||
$drawing4->setWorksheet($spreadsheet->getActiveSheet()); | ||
$drawing4->setEditAs(Drawing::EDIT_AS_ABSOLUTE); | ||
|
||
//$spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth($drawing->getImageWidth(), Dimension::UOM_PIXELS); | ||
$spreadsheet->getActiveSheet()->getRowDimension(6)->setRowHeight($drawing->getImageHeight(), Dimension::UOM_PIXELS); | ||
|
||
$helper->write($spreadsheet, __FILE__, ['Xlsx']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.