Skip to content

Commit

Permalink
Option to Write Hyperlink Rather than Label to Csv
Browse files Browse the repository at this point in the history
Fix PHPOffice#1412 which had gone stale and which I've reopened. Add boolean property `preferHyperlinkToLabel` with setter and getter to Csv Writer. Default is false, so no BC problems; in addition, true will probably add quite a bit of overhead.
  • Loading branch information
Owen Leibman committed Aug 26, 2024
1 parent e1dae99 commit 1618607
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
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();
}
}

0 comments on commit 1618607

Please sign in to comment.