From ecfc3f908b4e37fe6a5396f390f4372b2fa95c4d Mon Sep 17 00:00:00 2001 From: aVadim Date: Sat, 17 Jun 2023 20:05:35 +0300 Subject: [PATCH] add notes (old style comments) --- README.md | 18 ++++++++++++++++++ docs/04-styles.md | 2 +- src/FastExcelWriter/Sheet.php | 25 ++++++++++++++++++------- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b579c6f..4ea9be1 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Jump To: * [Advanced Example](#advanced-example) * [Height And Width](#height-and-width) * [Define Named Ranges](#define-named-ranges) +* [Define Named Ranges](#define-named-ranges) * [Workbook](/docs/01-workbook.md) * [Workbook settings](/docs/01-workbook.md#workbook-settings) * [Sets metadata of workbook](/docs/01-workbook.md#sets-metadata-of-workbook) @@ -256,6 +257,23 @@ $excel->addNamedRange('Sheet1!A1:F5', 'A1_F5'); ``` +### Adding Notes + +Notes are old style comments in Excel (notes on a light yellow background). You can add notes to any cells + +```php + +$sheet->writeCell('Text to A1'); +$sheet->addNote('A1', 'This is a note for cell A1'); + +$sheet->writeCell('Text to B1')->addNote('This is a note for B1'); +$sheet->writeTo('C4', 'Text to C4')->addNote('Note for C1'); + + +$sheet->addNote('E4:F8', 'This note will added to E4'); + +``` + ## Do you want to support FastExcelWriter? if you find this package useful you can support and donate to me for a cup of coffee: diff --git a/docs/04-styles.md b/docs/04-styles.md index a204871..f831b63 100644 --- a/docs/04-styles.md +++ b/docs/04-styles.md @@ -160,6 +160,6 @@ $sheet->withRange('B4:D5')->applyBgColor('#cff')->applyBorderOuter(Style::BORDER * applyVerticalAlign(string $verticalAlign) * applyTextCenter() * applyTextWrap(bool $textWrap) -* applyTextRotation(int $degrees) +* applyTextRotation(int $degrees) (thanks to @jarrod-colluco) Returns to [README.md](/README.md) diff --git a/src/FastExcelWriter/Sheet.php b/src/FastExcelWriter/Sheet.php index 9c86f11..a667f90 100644 --- a/src/FastExcelWriter/Sheet.php +++ b/src/FastExcelWriter/Sheet.php @@ -2316,18 +2316,29 @@ public function getNamedRanges(): array /** * @param string $cell - * @param string $comment + * @param string|null $comment * * @return $this */ - public function addNote(string $cell, string $comment): Sheet + public function addNote(string $cell, ?string $comment = null): Sheet { - $dimension = self::_rangeDimension($cell); - if (isset($dimension['cell1'])) { + if (func_num_args() === 1) { + $comment = $cell; + $rowIdx = $this->lastTouch['cell']['row_idx']; + $colIdx = $this->lastTouch['cell']['col_idx']; + $cell = Excel::cellAddress($rowIdx + 1, $colIdx + 1); + } + else { + $dimension = self::_rangeDimension($cell); + $cell = $dimension['cell1']; + $rowIdx = $dimension['rowIndex']; + $colIdx = $dimension['colIndex']; + } + if ($cell) { $this->notes[] = [ - 'cell' => $dimension['cell1'], - 'row_index' => $dimension['rowIndex'], - 'col_index' => $dimension['colIndex'], + 'cell' => $cell, + 'row_index' => $rowIdx, + 'col_index' => $colIdx, 'text' => $comment, 'style' => [ 'width' => '96pt',