Skip to content

Commit

Permalink
XLSX-Image-Background-In-Comments (#1547)
Browse files Browse the repository at this point in the history
  • Loading branch information
Burkov Sergey committed Dec 4, 2021
1 parent 50592b4 commit d90d319
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
22 changes: 20 additions & 2 deletions docs/references/features-cross-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@
<td></td>
</tr>
<tr>
<td style="padding-left: 1em;">Rich Text</td>
<td style="padding-left: 2em;">Rich Text</td>
<td style="text-align: center; color: red;">✖ <sup>2</sup></td>
<td style="text-align: center; color: green;">✔</td>
<td style="text-align: center; color: red;">✖</td>
Expand All @@ -1273,7 +1273,7 @@
<td></td>
</tr>
<tr>
<td style="padding-left: 1em;">Alignment</td>
<td style="padding-left: 2em;">Alignment</td>
<td style="text-align: center; color: red;">✖ <sup>3</sup></td>
<td style="text-align: center; color: red;">✖</td>
<td style="text-align: center; color: red;">✖</td>
Expand All @@ -1290,6 +1290,24 @@
<td></td>
<td></td>
</tr>
<tr>
<td style="padding-left: 2em;">Background Image</td>
<td style="text-align: center; color: red;">✖</td>
<td style="text-align: center; color: green;">✔</td>
<td style="text-align: center; color: red;">✖</td>
<td style="text-align: center; color: red;">✖</td>
<td style="text-align: center; color: red;">✖</td>
<td style="text-align: center; color: red;">✖</td>
<td style="text-align: center; color: red;">✖</td>
<td style="text-align: center; color: red;">✖</td>
<td style="text-align: center; color: green;">✔</td>
<td style="text-align: center; color: red;">✖</td>
<td style="text-align: center; color: red;">✖</td>
<td style="text-align: center; color: red;">✖</td>
<td style="text-align: center; color: red;">✖</td>
<td>$comment->getBackgroundImage()</td>
<td>$comment->setBackgroundImage()</td>
</tr>
<tr>
<td><strong>Cell Validation</strong></td>
<td style="text-align: center; color: green;">✔</td>
Expand Down
Binary file added docs/topics/images/08-cell-comment-with-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 18 additions & 1 deletion docs/topics/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -957,9 +957,26 @@ $spreadsheet->getActiveSheet()
->getComment('E11')
->getText()->createTextRun('Total amount on the current invoice, excluding VAT.');
```

![08-cell-comment.png](./images/08-cell-comment.png)

## Add a comment with background image to a cell

To add a comment with background image to a cell, use the following code:

```php
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('B5', 'Gibli Chromo');
// Add png image to comment background
$drawing = new Drawing();
$drawing->setName('Gibli Chromo');
$drawing->setPath('/tmp/gibli_chromo.png');
$comment = $sheet->getComment('B5');
$comment->setBackgroundImage($drawing);
// Set the size of the comment equal to the size of the image
$comment->setSizeAsBackgroundImage();
```
![08-cell-comment-with-image.png](./images/08-cell-comment-with-image.png)

## Apply autofilter to a range of cells

To apply an autofilter to a range of cells, use the following code:
Expand Down
15 changes: 7 additions & 8 deletions src/PhpSpreadsheet/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public function getHashCode(): string
($this->visible ? 1 : 0) .
$this->fillColor->getHashCode() .
$this->alignment .
($this->hasBackgroundImage() ? $this->backgroundImage->getHashCode() : '') .
__CLASS__
);
}
Expand Down Expand Up @@ -311,8 +312,6 @@ public function __toString(): string

/**
* Check is background image exists.
*
* @return bool
*/
public function hasBackgroundImage(): bool
{
Expand All @@ -321,8 +320,6 @@ public function hasBackgroundImage(): bool

/**
* Returns background image.
*
* @return Drawing
*/
public function getBackgroundImage(): Drawing
{
Expand All @@ -331,22 +328,24 @@ public function getBackgroundImage(): Drawing

/**
* Sets background image.
*
* @param Drawing $objDrawing
*/
public function setBackgroundImage(Drawing $objDrawing): void
public function setBackgroundImage(Drawing $objDrawing): self
{
$this->backgroundImage = $objDrawing;

return $this;
}

/**
* Sets size of comment as size of background image.
*/
public function setSizeAsBackgroundImage(): void
public function setSizeAsBackgroundImage(): self
{
if ($this->hasBackgroundImage()) {
$this->setWidth((string) $this->backgroundImage->getWidth());
$this->setHeight((string) $this->backgroundImage->getHeight());
}

return $this;
}
}

0 comments on commit d90d319

Please sign in to comment.