Skip to content

Commit

Permalink
Fix for #1612 - SLK Long File Name (#1706)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oleibman authored Dec 10, 2020
1 parent d90d050 commit e0feeca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/PhpSpreadsheet/Reader/Slk.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class Slk extends BaseReader
{
Expand Down Expand Up @@ -516,7 +517,7 @@ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
$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 = '';
Expand Down
24 changes: 24 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/SlkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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;
Expand All @@ -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();
Expand Down Expand Up @@ -131,4 +142,17 @@ public function testSheetIndex(): void

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());
}
}

0 comments on commit e0feeca

Please sign in to comment.