Improve Coverage for Sylk (#1514)
* Improve Coverage for Sylk I believe that both BaseReader and Sylk Reader are now 100% covered. Documentation available for this format is sparse. It was always incomplete, and in some cases inaccurate. My goal was to use PhpSpreadsheet to load the test file, save it as Xlsx, and visually compare the two, then add a test loaded with assertions. Cell values and calculated values, and border styles were generally handled pretty well without changes. Other types of styling were not handled so well. I added a few cells to exercise some previously uncovered code. Sylk files must be ASCII. I have deprecated the use of the setEncoding and getEncoding functions, which had no test cases.
This commit is contained in:
parent
73379cdfb1
commit
262896086a
|
@ -52,7 +52,7 @@ P;EArial;M200
|
|||
P;EArial;M200;SI
|
||||
P;EArial;M200;SBI
|
||||
P;EArial;M200;SBU
|
||||
P;EArial;M200;SBIU
|
||||
P;EArial;M220;SBIU
|
||||
P;EArial;M200
|
||||
P;EArial;M200;SI
|
||||
F;P0;DG0G8;M255
|
||||
|
@ -115,6 +115,7 @@ F;P19;FG0G;X4
|
|||
C;Y7;X2;K2.34
|
||||
C;X3;KFALSE
|
||||
C;Y8;X2;K3.45
|
||||
C;Y9;X2;K2.34;EMEDIAN(R[-3]C:R[-1]C)
|
||||
F;Y9;X1
|
||||
F;X2
|
||||
F;X3
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
|
||||
namespace PhpOffice\PhpSpreadsheet\Reader;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Border;
|
||||
|
@ -38,6 +40,20 @@ class Slk extends BaseReader
|
|||
*/
|
||||
private $format = 0;
|
||||
|
||||
/**
|
||||
* Fonts.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $fonts = [];
|
||||
|
||||
/**
|
||||
* Font Count.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $fontcount = 0;
|
||||
|
||||
/**
|
||||
* Create a new SYLK Reader instance.
|
||||
*/
|
||||
|
@ -55,10 +71,9 @@ class Slk extends BaseReader
|
|||
*/
|
||||
public function canRead($pFilename)
|
||||
{
|
||||
// Check if file exists
|
||||
try {
|
||||
$this->openFile($pFilename);
|
||||
} catch (Exception $e) {
|
||||
} catch (InvalidArgumentException $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -78,12 +93,24 @@ class Slk extends BaseReader
|
|||
return $hasDelimiter && $hasId;
|
||||
}
|
||||
|
||||
private function canReadOrBust(string $pFilename): void
|
||||
{
|
||||
if (!$this->canRead($pFilename)) {
|
||||
throw new ReaderException($pFilename . ' is an Invalid SYLK file.');
|
||||
}
|
||||
$this->openFile($pFilename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set input encoding.
|
||||
*
|
||||
* @deprecated no use is made of this property
|
||||
*
|
||||
* @param string $pValue Input encoding, eg: 'ANSI'
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function setInputEncoding($pValue)
|
||||
{
|
||||
|
@ -95,7 +122,11 @@ class Slk extends BaseReader
|
|||
/**
|
||||
* Get input encoding.
|
||||
*
|
||||
* @deprecated no use is made of this property
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function getInputEncoding()
|
||||
{
|
||||
|
@ -112,22 +143,16 @@ class Slk extends BaseReader
|
|||
public function listWorksheetInfo($pFilename)
|
||||
{
|
||||
// Open file
|
||||
if (!$this->canRead($pFilename)) {
|
||||
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
|
||||
}
|
||||
$this->openFile($pFilename);
|
||||
$this->canReadOrBust($pFilename);
|
||||
$fileHandle = $this->fileHandle;
|
||||
rewind($fileHandle);
|
||||
|
||||
$worksheetInfo = [];
|
||||
$worksheetInfo[0]['worksheetName'] = 'Worksheet';
|
||||
$worksheetInfo[0]['lastColumnLetter'] = 'A';
|
||||
$worksheetInfo[0]['lastColumnIndex'] = 0;
|
||||
$worksheetInfo[0]['totalRows'] = 0;
|
||||
$worksheetInfo[0]['totalColumns'] = 0;
|
||||
$worksheetInfo[0]['worksheetName'] = basename($pFilename, '.slk');
|
||||
|
||||
// loop through one row (line) at a time in the file
|
||||
$rowIndex = 0;
|
||||
$columnIndex = 0;
|
||||
while (($rowData = fgets($fileHandle)) !== false) {
|
||||
$columnIndex = 0;
|
||||
|
||||
|
@ -139,28 +164,26 @@ class Slk extends BaseReader
|
|||
$rowData = explode("\t", str_replace('¤', ';', str_replace(';', "\t", str_replace(';;', '¤', rtrim($rowData)))));
|
||||
|
||||
$dataType = array_shift($rowData);
|
||||
if ($dataType == 'C') {
|
||||
// Read cell value data
|
||||
if ($dataType == 'B') {
|
||||
foreach ($rowData as $rowDatum) {
|
||||
switch ($rowDatum[0]) {
|
||||
case 'C':
|
||||
case 'X':
|
||||
$columnIndex = substr($rowDatum, 1) - 1;
|
||||
|
||||
break;
|
||||
case 'R':
|
||||
case 'Y':
|
||||
$rowIndex = substr($rowDatum, 1);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$worksheetInfo[0]['totalRows'] = max($worksheetInfo[0]['totalRows'], $rowIndex);
|
||||
$worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], $columnIndex);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$worksheetInfo[0]['lastColumnIndex'] = $columnIndex;
|
||||
$worksheetInfo[0]['totalRows'] = $rowIndex;
|
||||
$worksheetInfo[0]['lastColumnLetter'] = Coordinate::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex'] + 1);
|
||||
$worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
|
||||
|
||||
|
@ -186,120 +209,25 @@ class Slk extends BaseReader
|
|||
return $this->loadIntoExisting($pFilename, $spreadsheet);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
|
||||
*
|
||||
* @param string $pFilename
|
||||
*
|
||||
* @return Spreadsheet
|
||||
*/
|
||||
public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
|
||||
private $colorArray = [
|
||||
'FF00FFFF', // 0 - cyan
|
||||
'FF000000', // 1 - black
|
||||
'FFFFFFFF', // 2 - white
|
||||
'FFFF0000', // 3 - red
|
||||
'FF00FF00', // 4 - green
|
||||
'FF0000FF', // 5 - blue
|
||||
'FFFFFF00', // 6 - yellow
|
||||
'FFFF00FF', // 7 - magenta
|
||||
];
|
||||
|
||||
private $fontStyleMappings = [
|
||||
'B' => 'bold',
|
||||
'I' => 'italic',
|
||||
'U' => 'underline',
|
||||
];
|
||||
|
||||
private function processFormula(string $rowDatum, bool &$hasCalculatedValue, string &$cellDataFormula, string $row, string $column): void
|
||||
{
|
||||
// Open file
|
||||
if (!$this->canRead($pFilename)) {
|
||||
throw new Exception($pFilename . ' is an Invalid Spreadsheet file.');
|
||||
}
|
||||
$this->openFile($pFilename);
|
||||
$fileHandle = $this->fileHandle;
|
||||
rewind($fileHandle);
|
||||
|
||||
// Create new Worksheets
|
||||
while ($spreadsheet->getSheetCount() <= $this->sheetIndex) {
|
||||
$spreadsheet->createSheet();
|
||||
}
|
||||
$spreadsheet->setActiveSheetIndex($this->sheetIndex);
|
||||
|
||||
$fromFormats = ['\-', '\ '];
|
||||
$toFormats = ['-', ' '];
|
||||
|
||||
// Loop through file
|
||||
$column = $row = '';
|
||||
|
||||
// loop through one row (line) at a time in the file
|
||||
while (($rowData = fgets($fileHandle)) !== false) {
|
||||
// convert SYLK encoded $rowData to UTF-8
|
||||
$rowData = StringHelper::SYLKtoUTF8($rowData);
|
||||
|
||||
// explode each row at semicolons while taking into account that literal semicolon (;)
|
||||
// is escaped like this (;;)
|
||||
$rowData = explode("\t", str_replace('¤', ';', str_replace(';', "\t", str_replace(';;', '¤', rtrim($rowData)))));
|
||||
|
||||
$dataType = array_shift($rowData);
|
||||
// Read shared styles
|
||||
if ($dataType == 'P') {
|
||||
$formatArray = [];
|
||||
foreach ($rowData as $rowDatum) {
|
||||
switch ($rowDatum[0]) {
|
||||
case 'P':
|
||||
$formatArray['numberFormat']['formatCode'] = str_replace($fromFormats, $toFormats, substr($rowDatum, 1));
|
||||
|
||||
break;
|
||||
case 'E':
|
||||
case 'F':
|
||||
$formatArray['font']['name'] = substr($rowDatum, 1);
|
||||
|
||||
break;
|
||||
case 'L':
|
||||
$formatArray['font']['size'] = substr($rowDatum, 1);
|
||||
|
||||
break;
|
||||
case 'S':
|
||||
$styleSettings = substr($rowDatum, 1);
|
||||
$iMax = strlen($styleSettings);
|
||||
for ($i = 0; $i < $iMax; ++$i) {
|
||||
switch ($styleSettings[$i]) {
|
||||
case 'I':
|
||||
$formatArray['font']['italic'] = true;
|
||||
|
||||
break;
|
||||
case 'D':
|
||||
$formatArray['font']['bold'] = true;
|
||||
|
||||
break;
|
||||
case 'T':
|
||||
$formatArray['borders']['top']['borderStyle'] = Border::BORDER_THIN;
|
||||
|
||||
break;
|
||||
case 'B':
|
||||
$formatArray['borders']['bottom']['borderStyle'] = Border::BORDER_THIN;
|
||||
|
||||
break;
|
||||
case 'L':
|
||||
$formatArray['borders']['left']['borderStyle'] = Border::BORDER_THIN;
|
||||
|
||||
break;
|
||||
case 'R':
|
||||
$formatArray['borders']['right']['borderStyle'] = Border::BORDER_THIN;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->formats['P' . $this->format++] = $formatArray;
|
||||
// Read cell value data
|
||||
} elseif ($dataType == 'C') {
|
||||
$hasCalculatedValue = false;
|
||||
$cellData = $cellDataFormula = '';
|
||||
foreach ($rowData as $rowDatum) {
|
||||
switch ($rowDatum[0]) {
|
||||
case 'C':
|
||||
case 'X':
|
||||
$column = substr($rowDatum, 1);
|
||||
|
||||
break;
|
||||
case 'R':
|
||||
case 'Y':
|
||||
$row = substr($rowDatum, 1);
|
||||
|
||||
break;
|
||||
case 'K':
|
||||
$cellData = substr($rowDatum, 1);
|
||||
|
||||
break;
|
||||
case 'E':
|
||||
$cellDataFormula = '=' . substr($rowDatum, 1);
|
||||
// Convert R1C1 style references to A1 style references (but only when not quoted)
|
||||
$temp = explode('"', $cellDataFormula);
|
||||
|
@ -343,22 +271,58 @@ class Slk extends BaseReader
|
|||
// Then rebuild the formula string
|
||||
$cellDataFormula = implode('"', $temp);
|
||||
$hasCalculatedValue = true;
|
||||
}
|
||||
|
||||
private function processCRecord(array $rowData, Spreadsheet &$spreadsheet, string &$row, string &$column): void
|
||||
{
|
||||
// Read cell value data
|
||||
$hasCalculatedValue = false;
|
||||
$cellDataFormula = $cellData = '';
|
||||
foreach ($rowData as $rowDatum) {
|
||||
switch ($rowDatum[0]) {
|
||||
case 'C':
|
||||
case 'X':
|
||||
$column = substr($rowDatum, 1);
|
||||
|
||||
break;
|
||||
case 'R':
|
||||
case 'Y':
|
||||
$row = substr($rowDatum, 1);
|
||||
|
||||
break;
|
||||
case 'K':
|
||||
$cellData = substr($rowDatum, 1);
|
||||
|
||||
break;
|
||||
case 'E':
|
||||
$this->processFormula($rowDatum, $hasCalculatedValue, $cellDataFormula, $row, $column);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
$columnLetter = Coordinate::stringFromColumnIndex($column);
|
||||
$columnLetter = Coordinate::stringFromColumnIndex((int) $column);
|
||||
$cellData = Calculation::unwrapResult($cellData);
|
||||
|
||||
// Set cell value
|
||||
$spreadsheet->getActiveSheet()->getCell($columnLetter . $row)->setValue(($hasCalculatedValue) ? $cellDataFormula : $cellData);
|
||||
$this->processCFinal($spreadsheet, $hasCalculatedValue, $cellDataFormula, $cellData, "$columnLetter$row");
|
||||
}
|
||||
|
||||
private function processCFinal(Spreadsheet &$spreadsheet, bool $hasCalculatedValue, string $cellDataFormula, string $cellData, string $coordinate): void
|
||||
{
|
||||
// Set cell value
|
||||
$spreadsheet->getActiveSheet()->getCell($coordinate)->setValue(($hasCalculatedValue) ? $cellDataFormula : $cellData);
|
||||
if ($hasCalculatedValue) {
|
||||
$cellData = Calculation::unwrapResult($cellData);
|
||||
$spreadsheet->getActiveSheet()->getCell($columnLetter . $row)->setCalculatedValue($cellData);
|
||||
$spreadsheet->getActiveSheet()->getCell($coordinate)->setCalculatedValue($cellData);
|
||||
}
|
||||
}
|
||||
|
||||
private function processFRecord(array $rowData, Spreadsheet &$spreadsheet, string &$row, string &$column): void
|
||||
{
|
||||
// Read cell formatting
|
||||
} elseif ($dataType == 'F') {
|
||||
$formatStyle = $columnWidth = $styleSettings = '';
|
||||
$formatStyle = $columnWidth = '';
|
||||
$startCol = $endCol = '';
|
||||
$fontStyle = '';
|
||||
$styleData = [];
|
||||
foreach ($rowData as $rowDatum) {
|
||||
switch ($rowDatum[0]) {
|
||||
|
@ -381,78 +345,203 @@ class Slk extends BaseReader
|
|||
|
||||
break;
|
||||
case 'S':
|
||||
$this->styleSettings($rowDatum, $styleData, $fontStyle);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->addFormats($spreadsheet, $formatStyle, $row, $column);
|
||||
$this->addFonts($spreadsheet, $fontStyle, $row, $column);
|
||||
$this->addStyle($spreadsheet, $styleData, $row, $column);
|
||||
$this->addWidth($spreadsheet, $columnWidth, $startCol, $endCol);
|
||||
}
|
||||
|
||||
private $styleSettingsFont = ['D' => 'bold', 'I' => 'italic'];
|
||||
|
||||
private $styleSettingsBorder = [
|
||||
'B' => 'bottom',
|
||||
'L' => 'left',
|
||||
'R' => 'right',
|
||||
'T' => 'top',
|
||||
];
|
||||
|
||||
private function styleSettings(string $rowDatum, array &$styleData, string &$fontStyle): void
|
||||
{
|
||||
$styleSettings = substr($rowDatum, 1);
|
||||
$iMax = strlen($styleSettings);
|
||||
for ($i = 0; $i < $iMax; ++$i) {
|
||||
switch ($styleSettings[$i]) {
|
||||
case 'I':
|
||||
$styleData['font']['italic'] = true;
|
||||
|
||||
break;
|
||||
case 'D':
|
||||
$styleData['font']['bold'] = true;
|
||||
|
||||
break;
|
||||
case 'T':
|
||||
$styleData['borders']['top']['borderStyle'] = Border::BORDER_THIN;
|
||||
|
||||
break;
|
||||
case 'B':
|
||||
$styleData['borders']['bottom']['borderStyle'] = Border::BORDER_THIN;
|
||||
|
||||
break;
|
||||
case 'L':
|
||||
$styleData['borders']['left']['borderStyle'] = Border::BORDER_THIN;
|
||||
|
||||
break;
|
||||
case 'R':
|
||||
$styleData['borders']['right']['borderStyle'] = Border::BORDER_THIN;
|
||||
|
||||
break;
|
||||
$char = $styleSettings[$i];
|
||||
if (array_key_exists($char, $this->styleSettingsFont)) {
|
||||
$styleData['font'][$this->styleSettingsFont[$char]] = true;
|
||||
} elseif (array_key_exists($char, $this->styleSettingsBorder)) {
|
||||
$styleData['borders'][$this->styleSettingsBorder[$char]]['borderStyle'] = Border::BORDER_THIN;
|
||||
} elseif ($char == 'S') {
|
||||
$styleData['fill']['fillType'] = \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_GRAY125;
|
||||
} elseif ($char == 'M') {
|
||||
if (preg_match('/M([1-9]\\d*)/', $styleSettings, $matches)) {
|
||||
$fontStyle = $matches[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (($formatStyle > '') && ($column > '') && ($row > '')) {
|
||||
$columnLetter = Coordinate::stringFromColumnIndex($column);
|
||||
private function addFormats(Spreadsheet &$spreadsheet, string $formatStyle, string $row, string $column): void
|
||||
{
|
||||
if ($formatStyle && $column > '' && $row > '') {
|
||||
$columnLetter = Coordinate::stringFromColumnIndex((int) $column);
|
||||
if (isset($this->formats[$formatStyle])) {
|
||||
$spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($this->formats[$formatStyle]);
|
||||
}
|
||||
}
|
||||
if ((!empty($styleData)) && ($column > '') && ($row > '')) {
|
||||
}
|
||||
|
||||
private function addFonts(Spreadsheet &$spreadsheet, string $fontStyle, string $row, string $column): void
|
||||
{
|
||||
if ($fontStyle && $column > '' && $row > '') {
|
||||
$columnLetter = Coordinate::stringFromColumnIndex((int) $column);
|
||||
if (isset($this->fonts[$fontStyle])) {
|
||||
$spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($this->fonts[$fontStyle]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function addStyle(Spreadsheet &$spreadsheet, array $styleData, string $row, string $column): void
|
||||
{
|
||||
if ((!empty($styleData)) && $column > '' && $row > '') {
|
||||
$columnLetter = Coordinate::stringFromColumnIndex($column);
|
||||
$spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($styleData);
|
||||
}
|
||||
}
|
||||
|
||||
private function addWidth(Spreadsheet $spreadsheet, string $columnWidth, string $startCol, string $endCol): void
|
||||
{
|
||||
if ($columnWidth > '') {
|
||||
if ($startCol == $endCol) {
|
||||
$startCol = Coordinate::stringFromColumnIndex($startCol);
|
||||
$startCol = Coordinate::stringFromColumnIndex((int) $startCol);
|
||||
$spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
|
||||
} else {
|
||||
$startCol = Coordinate::stringFromColumnIndex($startCol);
|
||||
$endCol = Coordinate::stringFromColumnIndex($endCol);
|
||||
$spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
|
||||
$spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth((float) $columnWidth);
|
||||
do {
|
||||
$spreadsheet->getActiveSheet()->getColumnDimension(++$startCol)->setWidth($columnWidth);
|
||||
} while ($startCol != $endCol);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
}
|
||||
|
||||
private function processPRecord(array $rowData, Spreadsheet &$spreadsheet): void
|
||||
{
|
||||
// Read shared styles
|
||||
$formatArray = [];
|
||||
$fromFormats = ['\-', '\ '];
|
||||
$toFormats = ['-', ' '];
|
||||
foreach ($rowData as $rowDatum) {
|
||||
switch ($rowDatum[0]) {
|
||||
case 'C':
|
||||
case 'X':
|
||||
$column = substr($rowDatum, 1);
|
||||
case 'P':
|
||||
$formatArray['numberFormat']['formatCode'] = str_replace($fromFormats, $toFormats, substr($rowDatum, 1));
|
||||
|
||||
break;
|
||||
case 'R':
|
||||
case 'Y':
|
||||
$row = substr($rowDatum, 1);
|
||||
case 'E':
|
||||
case 'F':
|
||||
$formatArray['font']['name'] = substr($rowDatum, 1);
|
||||
|
||||
break;
|
||||
case 'M':
|
||||
$formatArray['font']['size'] = substr($rowDatum, 1) / 20;
|
||||
|
||||
break;
|
||||
case 'L':
|
||||
$this->processPColors($rowDatum, $formatArray);
|
||||
|
||||
break;
|
||||
case 'S':
|
||||
$this->processPFontStyles($rowDatum, $formatArray);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->processPFinal($spreadsheet, $formatArray);
|
||||
}
|
||||
|
||||
private function processPColors(string $rowDatum, array &$formatArray): void
|
||||
{
|
||||
if (preg_match('/L([1-9]\\d*)/', $rowDatum, $matches)) {
|
||||
$fontColor = $matches[1] % 8;
|
||||
$formatArray['font']['color']['argb'] = $this->colorArray[$fontColor];
|
||||
}
|
||||
}
|
||||
|
||||
private function processPFontStyles(string $rowDatum, array &$formatArray): void
|
||||
{
|
||||
$styleSettings = substr($rowDatum, 1);
|
||||
$iMax = strlen($styleSettings);
|
||||
for ($i = 0; $i < $iMax; ++$i) {
|
||||
if (array_key_exists($styleSettings[$i], $this->fontStyleMappings)) {
|
||||
$formatArray['font'][$this->fontStyleMappings[$styleSettings[$i]]] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function processPFinal(Spreadsheet &$spreadsheet, array $formatArray): void
|
||||
{
|
||||
if (array_key_exists('numberFormat', $formatArray)) {
|
||||
$this->formats['P' . $this->format] = $formatArray;
|
||||
++$this->format;
|
||||
} elseif (array_key_exists('font', $formatArray)) {
|
||||
++$this->fontcount;
|
||||
$this->fonts[$this->fontcount] = $formatArray;
|
||||
if ($this->fontcount === 1) {
|
||||
$spreadsheet->getDefaultStyle()->applyFromArray($formatArray);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
|
||||
*
|
||||
* @param string $pFilename
|
||||
*
|
||||
* @return Spreadsheet
|
||||
*/
|
||||
public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
|
||||
{
|
||||
// Open file
|
||||
$this->canReadOrBust($pFilename);
|
||||
$fileHandle = $this->fileHandle;
|
||||
rewind($fileHandle);
|
||||
|
||||
// Create new Worksheets
|
||||
while ($spreadsheet->getSheetCount() <= $this->sheetIndex) {
|
||||
$spreadsheet->createSheet();
|
||||
}
|
||||
$spreadsheet->setActiveSheetIndex($this->sheetIndex);
|
||||
$spreadsheet->getActiveSheet()->setTitle(basename($pFilename, '.slk'));
|
||||
|
||||
// Loop through file
|
||||
$column = $row = '';
|
||||
|
||||
// loop through one row (line) at a time in the file
|
||||
while (($rowDataTxt = fgets($fileHandle)) !== false) {
|
||||
// convert SYLK encoded $rowData to UTF-8
|
||||
$rowDataTxt = StringHelper::SYLKtoUTF8($rowDataTxt);
|
||||
|
||||
// explode each row at semicolons while taking into account that literal semicolon (;)
|
||||
// is escaped like this (;;)
|
||||
$rowData = explode("\t", str_replace('¤', ';', str_replace(';', "\t", str_replace(';;', '¤', rtrim($rowDataTxt)))));
|
||||
|
||||
$dataType = array_shift($rowData);
|
||||
if ($dataType == 'P') {
|
||||
// Read shared styles
|
||||
$this->processPRecord($rowData, $spreadsheet);
|
||||
} elseif ($dataType == 'C') {
|
||||
// Read cell value data
|
||||
$this->processCRecord($rowData, $spreadsheet, $row, $column);
|
||||
} elseif ($dataType == 'F') {
|
||||
// Read cell formatting
|
||||
$this->processFRecord($rowData, $spreadsheet, $row, $column);
|
||||
} else {
|
||||
$this->columnRowFromRowData($rowData, $column, $row);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -463,6 +552,18 @@ class Slk extends BaseReader
|
|||
return $spreadsheet;
|
||||
}
|
||||
|
||||
private function columnRowFromRowData(array $rowData, string &$column, string &$row): void
|
||||
{
|
||||
foreach ($rowData as $rowDatum) {
|
||||
$char0 = $rowDatum[0];
|
||||
if ($char0 === 'X' || $char0 == 'C') {
|
||||
$column = substr($rowDatum, 1);
|
||||
} elseif ($char0 === 'Y' || $char0 == 'R') {
|
||||
$row = substr($rowDatum, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sheet index.
|
||||
*
|
||||
|
|
|
@ -255,6 +255,10 @@ EOF;
|
|||
self::assertEquals('\'', $reader->getEnclosure());
|
||||
$reader->setEnclosure('');
|
||||
self::assertEquals('"', $reader->getEnclosure());
|
||||
// following tests from BaseReader
|
||||
self::assertTrue($reader->getReadEmptyCells());
|
||||
self::assertFalse($reader->getIncludeCharts());
|
||||
self::assertNull($reader->getLoadSheetsOnly());
|
||||
}
|
||||
|
||||
public function testReadEmptyFileName(): void
|
||||
|
|
|
@ -0,0 +1,134 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Reader;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Slk;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Border;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Font;
|
||||
|
||||
class SlkTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
private static $testbook = __DIR__ . '/../../../samples/templates/SylkTest.slk';
|
||||
|
||||
public function testInfo(): void
|
||||
{
|
||||
$reader = new Slk();
|
||||
$workSheetInfo = $reader->listWorkSheetInfo(self::$testbook);
|
||||
$info0 = $workSheetInfo[0];
|
||||
self::assertEquals('SylkTest', $info0['worksheetName']);
|
||||
self::assertEquals('J', $info0['lastColumnLetter']);
|
||||
self::assertEquals(9, $info0['lastColumnIndex']);
|
||||
self::assertEquals(18, $info0['totalRows']);
|
||||
self::assertEquals(10, $info0['totalColumns']);
|
||||
}
|
||||
|
||||
public function testBadFileName(): void
|
||||
{
|
||||
$this->expectException(ReaderException::class);
|
||||
$reader = new Slk();
|
||||
self::assertNull($reader->setLoadSheetsOnly(null)->getLoadSheetsOnly());
|
||||
$reader->listWorkSheetInfo(self::$testbook . 'xxx');
|
||||
}
|
||||
|
||||
public function testBadFileName2(): void
|
||||
{
|
||||
$reader = new Slk();
|
||||
self::assertFalse($reader->canRead(self::$testbook . 'xxx'));
|
||||
}
|
||||
|
||||
public function testNotSylkFile(): void
|
||||
{
|
||||
$this->expectException(ReaderException::class);
|
||||
$reader = new Slk();
|
||||
$reader->listWorkSheetInfo(__FILE__);
|
||||
}
|
||||
|
||||
public function testLoadSlk(): void
|
||||
{
|
||||
$reader = new Slk();
|
||||
$spreadsheet = $reader->load(self::$testbook);
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
self::assertEquals('SylkTest', $sheet->getTitle());
|
||||
|
||||
self::assertEquals('FFFF0000', $sheet->getCell('A1')->getStyle()->getFont()->getColor()->getARGB());
|
||||
self::assertEquals(Fill::FILL_PATTERN_GRAY125, $sheet->getCell('A2')->getStyle()->getFill()->getFillType());
|
||||
self::assertEquals(Font::UNDERLINE_SINGLE, $sheet->getCell('A4')->getStyle()->getFont()->getUnderline());
|
||||
self::assertEquals('Test with (;) in string', $sheet->getCell('A4')->getValue());
|
||||
|
||||
self::assertEquals(22269, $sheet->getCell('A10')->getValue());
|
||||
self::assertEquals('dd/mm/yyyy', $sheet->getCell('A10')->getStyle()->getNumberFormat()->getFormatCode());
|
||||
self::assertEquals('19/12/1960', $sheet->getCell('A10')->getFormattedValue());
|
||||
self::assertEquals(1.5, $sheet->getCell('A11')->getValue());
|
||||
self::assertEquals('# ?/?', $sheet->getCell('A11')->getStyle()->getNumberFormat()->getFormatCode());
|
||||
self::assertEquals('1 1/2', $sheet->getCell('A11')->getFormattedValue());
|
||||
|
||||
self::assertEquals('=B1+C1', $sheet->getCell('H1')->getValue());
|
||||
self::assertEquals('=E2&F2', $sheet->getCell('J2')->getValue());
|
||||
self::assertEquals('=SUM(C1:C4)', $sheet->getCell('I5')->getValue());
|
||||
self::assertEquals('=MEDIAN(B6:B8)', $sheet->getCell('B9')->getValue());
|
||||
|
||||
self::assertEquals(11, $sheet->getCell('E1')->getStyle()->getFont()->getSize());
|
||||
self::assertTrue($sheet->getCell('E1')->getStyle()->getFont()->getBold());
|
||||
self::assertTrue($sheet->getCell('E1')->getStyle()->getFont()->getItalic());
|
||||
self::assertEquals(Font::UNDERLINE_SINGLE, $sheet->getCell('E1')->getStyle()->getFont()->getUnderline());
|
||||
self::assertFalse($sheet->getCell('E2')->getStyle()->getFont()->getBold());
|
||||
self::assertFalse($sheet->getCell('E2')->getStyle()->getFont()->getItalic());
|
||||
self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('E2')->getStyle()->getFont()->getUnderline());
|
||||
self::assertTrue($sheet->getCell('E3')->getStyle()->getFont()->getBold());
|
||||
self::assertFalse($sheet->getCell('E3')->getStyle()->getFont()->getItalic());
|
||||
self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('E3')->getStyle()->getFont()->getUnderline());
|
||||
self::assertFalse($sheet->getCell('E4')->getStyle()->getFont()->getBold());
|
||||
self::assertTrue($sheet->getCell('E4')->getStyle()->getFont()->getItalic());
|
||||
self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('E4')->getStyle()->getFont()->getUnderline());
|
||||
|
||||
self::assertTrue($sheet->getCell('F1')->getStyle()->getFont()->getBold());
|
||||
self::assertFalse($sheet->getCell('F1')->getStyle()->getFont()->getItalic());
|
||||
self::assertEquals(Font::UNDERLINE_SINGLE, $sheet->getCell('F1')->getStyle()->getFont()->getUnderline());
|
||||
self::assertFalse($sheet->getCell('F2')->getStyle()->getFont()->getBold());
|
||||
self::assertFalse($sheet->getCell('F2')->getStyle()->getFont()->getItalic());
|
||||
self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('F2')->getStyle()->getFont()->getUnderline());
|
||||
self::assertTrue($sheet->getCell('F3')->getStyle()->getFont()->getBold());
|
||||
self::assertTrue($sheet->getCell('F3')->getStyle()->getFont()->getItalic());
|
||||
self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('F3')->getStyle()->getFont()->getUnderline());
|
||||
self::assertFalse($sheet->getCell('F4')->getStyle()->getFont()->getBold());
|
||||
self::assertFalse($sheet->getCell('F4')->getStyle()->getFont()->getItalic());
|
||||
self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('F4')->getStyle()->getFont()->getUnderline());
|
||||
|
||||
self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C10')->getStyle()->getBorders()->getTop()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C10')->getStyle()->getBorders()->getRight()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C10')->getStyle()->getBorders()->getBottom()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C10')->getStyle()->getBorders()->getLeft()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C12')->getStyle()->getBorders()->getTop()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C12')->getStyle()->getBorders()->getRight()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C12')->getStyle()->getBorders()->getBottom()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C12')->getStyle()->getBorders()->getLeft()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C14')->getStyle()->getBorders()->getTop()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C14')->getStyle()->getBorders()->getRight()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C14')->getStyle()->getBorders()->getBottom()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C14')->getStyle()->getBorders()->getLeft()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C16')->getStyle()->getBorders()->getTop()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C16')->getStyle()->getBorders()->getRight()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C16')->getStyle()->getBorders()->getBottom()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C16')->getStyle()->getBorders()->getLeft()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getTop()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getRight()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getBottom()->getBorderStyle());
|
||||
self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getLeft()->getBorderStyle());
|
||||
// Have not yet figured out how C6/C7 are centred
|
||||
}
|
||||
|
||||
public function testSheetIndex(): void
|
||||
{
|
||||
$reader = new Slk();
|
||||
$sheetIndex = 2;
|
||||
$reader->setSheetIndex($sheetIndex);
|
||||
self::assertEquals($sheetIndex, $reader->getSheetIndex());
|
||||
$spreadsheet = $reader->load(self::$testbook);
|
||||
$sheet = $spreadsheet->setActiveSheetIndex($sheetIndex);
|
||||
self::assertEquals('SylkTest', $sheet->getTitle());
|
||||
|
||||
self::assertEquals('FFFF0000', $sheet->getCell('A1')->getStyle()->getFont()->getColor()->getARGB());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue