Skip to content
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 @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Check strictNullComparison outside of loops. [PR #3347](https://github.com/PHPOffice/PhpSpreadsheet/pull/3347)
- SUMIFS Does Not Require xlfn. [Issue #4182](https://github.com/PHPOffice/PhpSpreadsheet/issues/4182) [PR #4186](https://github.com/PHPOffice/PhpSpreadsheet/pull/4186)
- Image Transparency/Opacity with Html Reader Changes. [Discussion #4117](https://github.com/PHPOffice/PhpSpreadsheet/discussions/4117) [PR #4142](https://github.com/PHPOffice/PhpSpreadsheet/pull/4142)
- Option to Write Hyperlink Rather Than Label to Csv. [Issue #1412](https://github.com/PHPOffice/PhpSpreadsheet/issues/1412) [PR #4151](https://github.com/PHPOffice/PhpSpreadsheet/pull/4151)

## 2024-09-29 - 3.3.0 (no 3.0.\*, 3.1.\*, 3.2.\*)

Expand Down
28 changes: 28 additions & 0 deletions src/PhpSpreadsheet/Writer/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Csv extends BaseWriter
*/
private bool $variableColumns = false;

private bool $preferHyperlinkToLabel = false;

/**
* Create a new CSV.
*/
Expand Down Expand Up @@ -123,6 +125,14 @@ public function save($filename, int $flags = 0): void
array_splice($cellsArray, Coordinate::columnIndexFromString($column));
}
}
if ($this->preferHyperlinkToLabel) {
foreach ($cellsArray as $key => $value) {
$url = $sheet->getCell([$key + 1, $row])->getHyperlink()->getUrl();
if ($url !== '') {
$cellsArray[$key] = $url;
}
}
}
$this->writeLine($this->fileHandle, $cellsArray);
}

Expand Down Expand Up @@ -341,4 +351,22 @@ public function setVariableColumns(bool $pValue): self

return $this;
}

/**
* Get whether hyperlink or label should be output.
*/
public function getPreferHyperlinkToLabel(): bool
{
return $this->preferHyperlinkToLabel;
}

/**
* Set whether hyperlink or label should be output.
*/
public function setPreferHyperlinkToLabel(bool $preferHyperlinkToLabel): self
{
$this->preferHyperlinkToLabel = $preferHyperlinkToLabel;

return $this;
}
}
49 changes: 49 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Csv/HyperlinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Writer\Csv;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Csv;
use PHPUnit\Framework\TestCase;

class HyperlinkTest extends TestCase
{
public function testVariableColumns(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 3);
$sheet->setCellValue('B1', 4);
$sheet->setCellValue('C1', 5);
$sheet->setCellValue('A2', 6);
$sheet->setCellValue('B2', 'hyperlink');
$sheet->getCell('B2')->getHyperlink()
->setUrl('http://www.example.com');
$sheet->setCellValue('C2', 8);

$fh = fopen('php://memory', 'r+b');
self::assertNotFalse($fh);
$writer = new Csv($spreadsheet);
self::assertFalse($writer->getPreferHyperlinkToLabel());
$writer->setEnclosureRequired(false)->setLineEnding("\n");
$writer->save($fh);
rewind($fh);
self::assertSame(
"3,4,5\n6,hyperlink,8\n",
stream_get_contents($fh)
);

rewind($fh);
$writer->setPreferHyperlinkToLabel(true);
$writer->save($fh);
rewind($fh);
self::assertSame(
"3,4,5\n6,http://www.example.com,8\n",
stream_get_contents($fh)
);
fclose($fh);
$spreadsheet->disconnectWorksheets();
}
}
Loading