Skip to content

Commit 88c517f

Browse files
authored
Merge pull request #4151 from oleibman/issue1412
Option to Write Hyperlink Rather than Label to Csv
2 parents 8a57259 + 7176dcf commit 88c517f

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3030
- Check strictNullComparison outside of loops. [PR #3347](https://github.com/PHPOffice/PhpSpreadsheet/pull/3347)
3131
- SUMIFS Does Not Require xlfn. [Issue #4182](https://github.com/PHPOffice/PhpSpreadsheet/issues/4182) [PR #4186](https://github.com/PHPOffice/PhpSpreadsheet/pull/4186)
3232
- 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)
33+
- 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)
3334

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

src/PhpSpreadsheet/Writer/Csv.php

+28
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class Csv extends BaseWriter
6262
*/
6363
private bool $variableColumns = false;
6464

65+
private bool $preferHyperlinkToLabel = false;
66+
6567
/**
6668
* Create a new CSV.
6769
*/
@@ -123,6 +125,14 @@ public function save($filename, int $flags = 0): void
123125
array_splice($cellsArray, Coordinate::columnIndexFromString($column));
124126
}
125127
}
128+
if ($this->preferHyperlinkToLabel) {
129+
foreach ($cellsArray as $key => $value) {
130+
$url = $sheet->getCell([$key + 1, $row])->getHyperlink()->getUrl();
131+
if ($url !== '') {
132+
$cellsArray[$key] = $url;
133+
}
134+
}
135+
}
126136
$this->writeLine($this->fileHandle, $cellsArray);
127137
}
128138

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

342352
return $this;
343353
}
354+
355+
/**
356+
* Get whether hyperlink or label should be output.
357+
*/
358+
public function getPreferHyperlinkToLabel(): bool
359+
{
360+
return $this->preferHyperlinkToLabel;
361+
}
362+
363+
/**
364+
* Set whether hyperlink or label should be output.
365+
*/
366+
public function setPreferHyperlinkToLabel(bool $preferHyperlinkToLabel): self
367+
{
368+
$this->preferHyperlinkToLabel = $preferHyperlinkToLabel;
369+
370+
return $this;
371+
}
344372
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Csv;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PhpOffice\PhpSpreadsheet\Writer\Csv;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class HyperlinkTest extends TestCase
12+
{
13+
public function testVariableColumns(): void
14+
{
15+
$spreadsheet = new Spreadsheet();
16+
$sheet = $spreadsheet->getActiveSheet();
17+
$sheet->setCellValue('A1', 3);
18+
$sheet->setCellValue('B1', 4);
19+
$sheet->setCellValue('C1', 5);
20+
$sheet->setCellValue('A2', 6);
21+
$sheet->setCellValue('B2', 'hyperlink');
22+
$sheet->getCell('B2')->getHyperlink()
23+
->setUrl('http://www.example.com');
24+
$sheet->setCellValue('C2', 8);
25+
26+
$fh = fopen('php://memory', 'r+b');
27+
self::assertNotFalse($fh);
28+
$writer = new Csv($spreadsheet);
29+
self::assertFalse($writer->getPreferHyperlinkToLabel());
30+
$writer->setEnclosureRequired(false)->setLineEnding("\n");
31+
$writer->save($fh);
32+
rewind($fh);
33+
self::assertSame(
34+
"3,4,5\n6,hyperlink,8\n",
35+
stream_get_contents($fh)
36+
);
37+
38+
rewind($fh);
39+
$writer->setPreferHyperlinkToLabel(true);
40+
$writer->save($fh);
41+
rewind($fh);
42+
self::assertSame(
43+
"3,4,5\n6,http://www.example.com,8\n",
44+
stream_get_contents($fh)
45+
);
46+
fclose($fh);
47+
$spreadsheet->disconnectWorksheets();
48+
}
49+
}

0 commit comments

Comments
 (0)