From e0feeca555a07c79e34a8a14d9149194f4053c01 Mon Sep 17 00:00:00 2001 From: oleibman Date: Thu, 10 Dec 2020 13:02:36 -0800 Subject: [PATCH] Fix for #1612 - SLK Long File Name (#1706) Issue has been marked stale, but ... Sylk read sets worksheet title to filename (minus .slk). If that is >31 characters, PhpSpreadsheet throws Exception. This change truncates sheet title, as Excel does, to 31 characters. --- src/PhpSpreadsheet/Reader/Slk.php | 3 ++- tests/PhpSpreadsheetTests/Reader/SlkTest.php | 24 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/PhpSpreadsheet/Reader/Slk.php b/src/PhpSpreadsheet/Reader/Slk.php index 0e147376..e58ff2f6 100644 --- a/src/PhpSpreadsheet/Reader/Slk.php +++ b/src/PhpSpreadsheet/Reader/Slk.php @@ -9,6 +9,7 @@ use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Border; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; class Slk extends BaseReader { @@ -516,7 +517,7 @@ class Slk extends BaseReader $spreadsheet->createSheet(); } $spreadsheet->setActiveSheetIndex($this->sheetIndex); - $spreadsheet->getActiveSheet()->setTitle(basename($pFilename, '.slk')); + $spreadsheet->getActiveSheet()->setTitle(substr(basename($pFilename, '.slk'), 0, Worksheet::SHEET_TITLE_MAXIMUM_LENGTH)); // Loop through file $column = $row = ''; diff --git a/tests/PhpSpreadsheetTests/Reader/SlkTest.php b/tests/PhpSpreadsheetTests/Reader/SlkTest.php index 4c7cc513..e461557e 100644 --- a/tests/PhpSpreadsheetTests/Reader/SlkTest.php +++ b/tests/PhpSpreadsheetTests/Reader/SlkTest.php @@ -4,6 +4,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Reader; use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; use PhpOffice\PhpSpreadsheet\Reader\Slk; +use PhpOffice\PhpSpreadsheet\Shared\File; use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\Font; @@ -12,6 +13,16 @@ class SlkTest extends \PHPUnit\Framework\TestCase { private static $testbook = __DIR__ . '/../../../samples/templates/SylkTest.slk'; + private $filename = ''; + + protected function teardown(): void + { + if ($this->filename) { + unlink($this->filename); + $this->filename = ''; + } + } + public function testInfo(): void { $reader = new Slk(); @@ -131,4 +142,17 @@ class SlkTest extends \PHPUnit\Framework\TestCase self::assertEquals('FFFF0000', $sheet->getCell('A1')->getStyle()->getFont()->getColor()->getARGB()); } + + public function testLongName(): void + { + $contents = file_get_contents(self::$testbook); + $this->filename = File::sysGetTempDir() + . '/123456789a123456789b123456789c12345.slk'; + file_put_contents($this->filename, $contents); + $reader = new Slk(); + $spreadsheet = $reader->load($this->filename); + $sheet = $spreadsheet->getActiveSheet(); + self::assertEquals('123456789a123456789b123456789c1', $sheet->getTitle()); + self::assertEquals('FFFF0000', $sheet->getCell('A1')->getStyle()->getFont()->getColor()->getARGB()); + } }