Repository: PHPOffice/PhpSpreadsheet (php)

Issue:
Export to csv from excel including hyperlink
Hi PHPOffice,

I am looking for a way to export my XLSX file to CSV where some columns have a hyperlink and I could not figure out how to export the file with a hyperlink instead of the text on which hyperlink is applied.

here is my code to export to CSV:

```
require constant('EO_ECIE_DIR').'vendor/autoload.php';				
 $xl_reader = new Xlsx();
 $sheets = $xl_reader->load(self::$_file);

 $sheet_names = $sheets->getSheetNames();

 $csv_writer = new Csv($sheets);
 if(is_array($sheet_names) and !empty($sheet_names[0])){

	$csv_writer->setSheetIndex(0);					
       $csv_writer->save(self::$_file.'.csv');
       self::$_file = self::$_file.'.csv';
}

```
Help is appreciated as I have already googled a lot before writing a query here.

Thanks in advance.



Pull request description:
Option to Write Hyperlink Rather than Label to Csv
Fix #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.

This is:

- [ ] a bugfix
- [x] a new feature
- [ ] refactoring
- [ ] additional unit tests

Checklist:

- [ ] Changes are covered by unit tests
  - [ ] Changes are covered by existing unit tests
  - [x] New unit tests have been added
- [x] Code style is respected
- [x] Commit message explains **why** the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
- [ ] CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
- [ ] Documentation is updated as necessary

### Why this change is needed?

Provide an explanation of why this change is needed, with links to any Issues (if appropriate).
If this is a bugfix or a new feature, and there are no existing Issues, then please also create an issue that will make it easier to track progress with this PR.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 56e6c957c..69d4c9ef0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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.\*)
 
diff --git a/src/PhpSpreadsheet/Writer/Csv.php b/src/PhpSpreadsheet/Writer/Csv.php
index d15bc654c..19da56848 100644
--- a/src/PhpSpreadsheet/Writer/Csv.php
+++ b/src/PhpSpreadsheet/Writer/Csv.php
@@ -62,6 +62,8 @@ class Csv extends BaseWriter
      */
     private bool $variableColumns = false;
 
+    private bool $preferHyperlinkToLabel = false;
+
     /**
      * Create a new CSV.
      */
@@ -123,6 +125,14 @@ class Csv extends BaseWriter
                     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);
         }
 
@@ -341,4 +351,22 @@ class Csv extends BaseWriter
 
         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;
+    }
 }


Test patch:
diff --git a/tests/PhpSpreadsheetTests/Writer/Csv/HyperlinkTest.php b/tests/PhpSpreadsheetTests/Writer/Csv/HyperlinkTest.php
new file mode 100644
index 000000000..b1e1bbfde
--- /dev/null
+++ b/tests/PhpSpreadsheetTests/Writer/Csv/HyperlinkTest.php
@@ -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();
+    }
+}