forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Option to Write Hyperlink Rather than Label to Csv
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
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |