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.
This commit is contained in:
oleibman 2020-12-10 13:02:36 -08:00 committed by GitHub
parent d90d05077f
commit e0feeca555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -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 = '';

View File

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