Skip to content

Ods Comments With Newlines #4086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Mpdf and Tcpdf Borders on Merged Cells. [Issue #3557](https://github.com/PHPOffice/PhpSpreadsheet/issues/3557) [PR #4047](https://github.com/PHPOffice/PhpSpreadsheet/pull/4047)
- Xls Conditional Format Improvements. [PR #4030](https://github.com/PHPOffice/PhpSpreadsheet/pull/4030) [PR #4033](https://github.com/PHPOffice/PhpSpreadsheet/pull/4033)
- Conditional Range Unions and Intersections [Issue #4039](https://github.com/PHPOffice/PhpSpreadsheet/issues/4039) [PR #4042](https://github.com/PHPOffice/PhpSpreadsheet/pull/4042)
- Ods comments with newlines. [Issue #4081](https://github.com/PHPOffice/PhpSpreadsheet/issues/4081) [PR #4086](https://github.com/PHPOffice/PhpSpreadsheet/pull/4086)
- Propagate errors in Text functions. [Issue #2581](https://github.com/PHPOffice/PhpSpreadsheet/issues/2581) [PR #4080](https://github.com/PHPOffice/PhpSpreadsheet/pull/4080)
- Csv Reader allow use of html mimetype. [Issue #4036](https://github.com/PHPOffice/PhpSpreadsheet/issues/4036) [PR #4040](https://github.com/PHPOffice/PhpSpreadsheet/pull/4040)
- Problem rendering line chart with missing plot label. [PR #4074](https://github.com/PHPOffice/PhpSpreadsheet/pull/4074)
Expand Down
27 changes: 20 additions & 7 deletions src/PhpSpreadsheet/Reader/Ods.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,25 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Sp

if ($annotation->length > 0 && $annotation->item(0) !== null) {
$textNode = $annotation->item(0)->getElementsByTagNameNS($textNs, 'p');
$textNodeLength = $textNode->length;
$newLineOwed = false;
for ($textNodeIndex = 0; $textNodeIndex < $textNodeLength; ++$textNodeIndex) {
$textNodeItem = $textNode->item($textNodeIndex);
if ($textNodeItem !== null) {
$text = $this->scanElementForText($textNodeItem);
if ($newLineOwed) {
$spreadsheet->getActiveSheet()
->getComment($columnID . $rowID)
->getText()
->createText("\n");
}
$newLineOwed = true;

if ($textNode->length > 0 && $textNode->item(0) !== null) {
$text = $this->scanElementForText($textNode->item(0));

$spreadsheet->getActiveSheet()
->getComment($columnID . $rowID)
->setText($this->parseRichText($text));
// ->setAuthor( $author )
$spreadsheet->getActiveSheet()
->getComment($columnID . $rowID)
->getText()
->createText($this->parseRichText($text));
}
}
}

Expand Down Expand Up @@ -731,6 +742,8 @@ protected function scanElementForText(DOMNode $element): string
/** @var DOMNode $child */
if ($child->nodeType == XML_TEXT_NODE) {
$str .= $child->nodeValue;
} elseif ($child->nodeType == XML_ELEMENT_NODE && $child->nodeName == 'text:line-break') {
$str .= "\n";
} elseif ($child->nodeType == XML_ELEMENT_NODE && $child->nodeName == 'text:s') {
// It's a space

Expand Down
17 changes: 16 additions & 1 deletion src/PhpSpreadsheet/Writer/Ods/Cell/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,22 @@ public static function write(XMLWriter $objWriter, Cell $cell): void
$objWriter->writeAttribute('svg:x', $comment->getMarginLeft());
$objWriter->writeAttribute('svg:y', $comment->getMarginTop());
$objWriter->writeElement('dc:creator', $comment->getAuthor());
$objWriter->writeElement('text:p', $comment->getText()->getPlainText());

$objWriter->startElement('text:p');
$text = $comment->getText()->getPlainText();
$textElements = explode("\n", $text);
$newLineOwed = false;
foreach ($textElements as $textSegment) {
if ($newLineOwed) {
$objWriter->writeElement('text:line-break');
}
$newLineOwed = true;
if ($textSegment !== '') {
$objWriter->writeElement('text:span', $textSegment);
}
}
$objWriter->endElement(); // text:p

$objWriter->endElement();
}
}
41 changes: 41 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Ods/MultiLineCommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;

use PhpOffice\PhpSpreadsheet\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class MultiLineCommentTest extends AbstractFunctional
{
public function testMultipleParagraphs(): void
{
$filename = 'tests/data/Reader/Ods/issue.4081.ods';
$reader = new Ods();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame("First line.\n\nSecond line.", $sheet->getComment('A1')->getText()->getPlainText());
$spreadsheet->disconnectWorksheets();
}

public function testOneParagraphMultipleSpans(): void
{
$spreadsheetOld = new Spreadsheet();
$sheetOld = $spreadsheetOld->getActiveSheet();
$sheetOld->getCell('A1')->setValue('Hello');
$text = $sheetOld->getComment('A1')->getText();
$text->createText('First');
$text->createText(' line.');
$text->createText("\n");
$text->createText("\n");
$text->createText("Second line.\nThird line.");
$spreadsheet = $this->writeAndReload($spreadsheetOld, 'Ods');
$spreadsheetOld->disconnectWorksheets();

$sheet = $spreadsheet->getActiveSheet();
self::assertSame("First line.\n\nSecond line.\nThird line.", $sheet->getComment('A1')->getText()->getPlainText());
$spreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/Ods/issue.4081.ods
Binary file not shown.
Loading